blob: 007137c3b5c9d2e672638eca20661ea3667470ff [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 Slabye129def2008-07-22 11:20:34 +0100159#define PCI_DEVICE_ID_POS104UL 0x1044
Jiri Slaby1c456072008-02-07 00:16:46 -0800160#define PCI_DEVICE_ID_CB108 0x1080
Jiri Slabye129def2008-07-22 11:20:34 +0100161#define PCI_DEVICE_ID_CP102UF 0x1023
Jiri Slaby502f2952009-09-10 12:20:07 +0200162#define PCI_DEVICE_ID_CP112UL 0x1120
Jiri Slaby1c456072008-02-07 00:16:46 -0800163#define PCI_DEVICE_ID_CB114 0x1142
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800164#define PCI_DEVICE_ID_CP114UL 0x1143
Jiri Slaby1c456072008-02-07 00:16:46 -0800165#define PCI_DEVICE_ID_CB134I 0x1341
166#define PCI_DEVICE_ID_CP138U 0x1380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Jiri Slaby1c456072008-02-07 00:16:46 -0800168#define MXSER_HIGHBAUD 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Jiri Slabye4558362021-06-18 08:14:14 +0200170enum mxser_must_hwid {
171 MOXA_OTHER_UART = 0x00,
172 MOXA_MUST_MU150_HWID = 0x01,
173 MOXA_MUST_MU860_HWID = 0x02,
174};
175
Jiri Slaby1c456072008-02-07 00:16:46 -0800176static const struct {
Jiri Slabydc33f642021-06-18 08:14:11 +0200177 u8 type;
178 u8 fifo_size;
179 u8 rx_high_water;
180 u8 rx_low_water;
181 speed_t max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800182} Gpci_uart_info[] = {
Jiri Slabydc33f642021-06-18 08:14:11 +0200183 { MOXA_OTHER_UART, 16, 14, 1, 921600 },
184 { MOXA_MUST_MU150_HWID, 64, 48, 16, 230400 },
185 { MOXA_MUST_MU860_HWID, 128, 96, 32, 921600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
Jiri Slaby1c456072008-02-07 00:16:46 -0800187#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Jiri Slaby1c456072008-02-07 00:16:46 -0800189struct mxser_cardinfo {
190 char *name;
191 unsigned int nports;
192 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
194
Jiri Slaby1c456072008-02-07 00:16:46 -0800195static const struct mxser_cardinfo mxser_cards[] = {
Jiri Slaby15254902021-06-18 08:14:08 +0200196/* 0*/ { "C168H/PCI series", 8, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800197 { "C104H/PCI series", 4, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800198 { "CP-132 series", 2, },
199 { "CP-114 series", 4, },
Jiri Slaby15254902021-06-18 08:14:08 +0200200 { "CT-114 series", 4, },
201/* 5*/ { "CP-102 series", 2, MXSER_HIGHBAUD },
Jiri Slaby1c456072008-02-07 00:16:46 -0800202 { "CP-104U series", 4, },
203 { "CP-168U series", 8, },
204 { "CP-132U series", 2, },
Jiri Slaby15254902021-06-18 08:14:08 +0200205 { "CP-134U series", 4, },
206/*10*/ { "CP-104JU series", 4, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800207 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
208 { "CP-118U series", 8, },
209 { "CP-102UL series", 2, },
Jiri Slaby15254902021-06-18 08:14:08 +0200210 { "CP-102U series", 2, },
211/*15*/ { "CP-118EL series", 8, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800212 { "CP-168EL series", 8, },
213 { "CP-104EL series", 4, },
214 { "CB-108 series", 8, },
Jiri Slaby15254902021-06-18 08:14:08 +0200215 { "CB-114 series", 4, },
216/*20*/ { "CB-134I series", 4, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800217 { "CP-138U series", 8, },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800218 { "POS-104UL series", 4, },
Jiri Slabye129def2008-07-22 11:20:34 +0100219 { "CP-114UL series", 4, },
Jiri Slaby15254902021-06-18 08:14:08 +0200220 { "CP-102UF series", 2, },
221/*25*/ { "CP-112UL series", 2, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800222};
223
224/* driver_data correspond to the lines in the structure above
225 see also ISA probe function before you change something */
Arvind Yadav3385ecf2017-07-23 15:31:01 +0530226static const struct pci_device_id mxser_pcibrds[] = {
Jiri Slaby15254902021-06-18 08:14:08 +0200227 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 0 },
228 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 1 },
229 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 2 },
230 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 3 },
231 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 4 },
232 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 5 },
233 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 6 },
234 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 7 },
235 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 8 },
236 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 9 },
237 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 10 },
238 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 11 },
239 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 12 },
240 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 13 },
241 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 14 },
242 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 15 },
243 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 16 },
244 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 17 },
245 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 18 },
246 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 19 },
247 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 20 },
248 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 21 },
249 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 22 },
250 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 23 },
251 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 24 },
252 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP112UL), .driver_data = 25 },
Jiri Slaby1c456072008-02-07 00:16:46 -0800253 { }
254};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257static int ttymajor = MXSERMAJOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259/* Variables for insmod */
260
261MODULE_AUTHOR("Casper Yang");
262MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800263module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264MODULE_LICENSE("GPL");
265
Jiri Slaby1c456072008-02-07 00:16:46 -0800266struct mxser_board;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Jiri Slaby1c456072008-02-07 00:16:46 -0800268struct mxser_port {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100269 struct tty_port port;
Jiri Slaby1c456072008-02-07 00:16:46 -0800270 struct mxser_board *board;
Jiri Slaby1c456072008-02-07 00:16:46 -0800271
272 unsigned long ioaddr;
273 unsigned long opmode_ioaddr;
Jiri Slaby1c456072008-02-07 00:16:46 -0800274
Jiri Slabydc33f642021-06-18 08:14:11 +0200275 u8 rx_high_water;
276 u8 rx_low_water;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int baud_base; /* max. speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int type; /* UART type */
Jiri Slaby1c456072008-02-07 00:16:46 -0800279
Jiri Slabya93963e2021-06-18 08:14:24 +0200280 unsigned char x_char; /* xon/xoff character */
281 u8 IER; /* Interrupt Enable Register */
282 u8 MCR; /* Modem control register */
Jiri Slaby1c456072008-02-07 00:16:46 -0800283
284 unsigned char stop_rx;
285 unsigned char ldisc_stop_rx;
286
287 int custom_divisor;
Jiri Slaby1c456072008-02-07 00:16:46 -0800288
Jiri Slaby1c456072008-02-07 00:16:46 -0800289 struct async_icount icount; /* kernel counters for 4 input interrupts */
Jiri Slaby104583b2017-09-12 12:39:54 +0200290 unsigned int timeout;
Jiri Slaby1c456072008-02-07 00:16:46 -0800291
Jiri Slabya93963e2021-06-18 08:14:24 +0200292 u8 read_status_mask;
293 u8 ignore_status_mask;
Jiri Slabydc33f642021-06-18 08:14:11 +0200294 u8 xmit_fifo_size;
Jiri Slaby02e43142021-06-18 08:14:25 +0200295 unsigned int xmit_head;
296 unsigned int xmit_tail;
297 unsigned int xmit_cnt;
Peter Hurleycd7b4b32016-01-10 14:51:38 -0800298 int closing;
Jiri Slaby1c456072008-02-07 00:16:46 -0800299
Alan Cox606d0992006-12-08 02:38:45 -0800300 struct ktermios normal_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -0800301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 spinlock_t slock;
Jiri Slaby1c456072008-02-07 00:16:46 -0800303};
304
305struct mxser_board {
306 unsigned int idx;
307 int irq;
308 const struct mxser_cardinfo *info;
309 unsigned long vector;
Jiri Slaby1c456072008-02-07 00:16:46 -0800310
Jiri Slabye4558362021-06-18 08:14:14 +0200311 enum mxser_must_hwid must_hwid;
Jiri Slaby928f9462021-06-18 08:14:16 +0200312 speed_t max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800313
314 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315};
316
Jiri Slaby1c456072008-02-07 00:16:46 -0800317static struct mxser_board mxser_boards[MXSER_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318static struct tty_driver *mxvar_sdriver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Christoph Hellwig148ff862008-04-30 00:54:29 -0700320static void mxser_enable_must_enchance_mode(unsigned long baseio)
321{
322 u8 oldlcr;
323 u8 efr;
324
325 oldlcr = inb(baseio + UART_LCR);
326 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
327
328 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
329 efr |= MOXA_MUST_EFR_EFRB_ENABLE;
330
331 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
332 outb(oldlcr, baseio + UART_LCR);
333}
334
335static void mxser_disable_must_enchance_mode(unsigned long baseio)
336{
337 u8 oldlcr;
338 u8 efr;
339
340 oldlcr = inb(baseio + UART_LCR);
341 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
342
343 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
344 efr &= ~MOXA_MUST_EFR_EFRB_ENABLE;
345
346 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
347 outb(oldlcr, baseio + UART_LCR);
348}
349
350static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
351{
352 u8 oldlcr;
353 u8 efr;
354
355 oldlcr = inb(baseio + UART_LCR);
356 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
357
358 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
359 efr &= ~MOXA_MUST_EFR_BANK_MASK;
360 efr |= MOXA_MUST_EFR_BANK0;
361
362 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
363 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
364 outb(oldlcr, baseio + UART_LCR);
365}
366
367static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
368{
369 u8 oldlcr;
370 u8 efr;
371
372 oldlcr = inb(baseio + UART_LCR);
373 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
374
375 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
376 efr &= ~MOXA_MUST_EFR_BANK_MASK;
377 efr |= MOXA_MUST_EFR_BANK0;
378
379 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
380 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
381 outb(oldlcr, baseio + UART_LCR);
382}
383
384static void mxser_set_must_fifo_value(struct mxser_port *info)
385{
386 u8 oldlcr;
387 u8 efr;
388
389 oldlcr = inb(info->ioaddr + UART_LCR);
390 outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
391
392 efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
393 efr &= ~MOXA_MUST_EFR_BANK_MASK;
394 efr |= MOXA_MUST_EFR_BANK1;
395
396 outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
Jiri Slabydc33f642021-06-18 08:14:11 +0200397 outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
398 outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
399 outb(info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700400 outb(oldlcr, info->ioaddr + UART_LCR);
401}
402
403static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
404{
405 u8 oldlcr;
406 u8 efr;
407
408 oldlcr = inb(baseio + UART_LCR);
409 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
410
411 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
412 efr &= ~MOXA_MUST_EFR_BANK_MASK;
413 efr |= MOXA_MUST_EFR_BANK2;
414
415 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
416 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
417 outb(oldlcr, baseio + UART_LCR);
418}
419
420static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
421{
422 u8 oldlcr;
423 u8 efr;
424
425 oldlcr = inb(baseio + UART_LCR);
426 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
427
428 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
429 efr &= ~MOXA_MUST_EFR_BANK_MASK;
430 efr |= MOXA_MUST_EFR_BANK2;
431
432 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
433 *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
434 outb(oldlcr, baseio + UART_LCR);
435}
436
437static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
438{
439 u8 oldlcr;
440 u8 efr;
441
442 oldlcr = inb(baseio + UART_LCR);
443 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
444
445 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
446 efr &= ~MOXA_MUST_EFR_SF_MASK;
447
448 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
449 outb(oldlcr, baseio + UART_LCR);
450}
451
452static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
453{
454 u8 oldlcr;
455 u8 efr;
456
457 oldlcr = inb(baseio + UART_LCR);
458 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
459
460 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
461 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
462 efr |= MOXA_MUST_EFR_SF_TX1;
463
464 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
465 outb(oldlcr, baseio + UART_LCR);
466}
467
468static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
469{
470 u8 oldlcr;
471 u8 efr;
472
473 oldlcr = inb(baseio + UART_LCR);
474 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
475
476 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
477 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
478
479 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
480 outb(oldlcr, baseio + UART_LCR);
481}
482
483static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
484{
485 u8 oldlcr;
486 u8 efr;
487
488 oldlcr = inb(baseio + UART_LCR);
489 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
490
491 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
492 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
493 efr |= MOXA_MUST_EFR_SF_RX1;
494
495 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
496 outb(oldlcr, baseio + UART_LCR);
497}
498
499static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
500{
501 u8 oldlcr;
502 u8 efr;
503
504 oldlcr = inb(baseio + UART_LCR);
505 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
506
507 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
508 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
509
510 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
511 outb(oldlcr, baseio + UART_LCR);
512}
513
Jiri Slabye4558362021-06-18 08:14:14 +0200514static enum mxser_must_hwid mxser_must_get_hwid(unsigned long io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 u8 oldmcr, hwid;
517 int i;
518
519 outb(0, io + UART_LCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700520 mxser_disable_must_enchance_mode(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 oldmcr = inb(io + UART_MCR);
522 outb(0, io + UART_MCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700523 mxser_set_must_xon1_value(io, 0x11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if ((hwid = inb(io + UART_MCR)) != 0) {
525 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700526 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
Christoph Hellwig148ff862008-04-30 00:54:29 -0700529 mxser_get_must_hardware_id(io, &hwid);
Jiri Slabye4558362021-06-18 08:14:14 +0200530 for (i = 1; i < UART_INFO_NUM; i++) /* 0 = OTHER_UART */
Jiri Slaby1c456072008-02-07 00:16:46 -0800531 if (hwid == Gpci_uart_info[i].type)
Jiri Slabye4558362021-06-18 08:14:14 +0200532 return hwid;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return MOXA_OTHER_UART;
535}
536
Jiri Slaby1c456072008-02-07 00:16:46 -0800537static void process_txrx_fifo(struct mxser_port *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 int i;
540
541 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 info->rx_high_water = 1;
543 info->rx_low_water = 1;
544 info->xmit_fifo_size = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800545 } else
546 for (i = 0; i < UART_INFO_NUM; i++)
Jiri Slaby292955a2021-06-18 08:14:13 +0200547 if (info->board->must_hwid == Gpci_uart_info[i].type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
549 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
Jiri Slabydc33f642021-06-18 08:14:11 +0200550 info->xmit_fifo_size = Gpci_uart_info[i].fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 break;
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Alan Cox31f35932009-01-02 13:45:05 +0000555static int mxser_carrier_raised(struct tty_port *port)
556{
557 struct mxser_port *mp = container_of(port, struct mxser_port, port);
558 return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
559}
560
Alan Coxfcc8ac12009-06-11 12:24:17 +0100561static void mxser_dtr_rts(struct tty_port *port, int on)
Alan Cox5d951fb2009-01-02 13:45:19 +0000562{
563 struct mxser_port *mp = container_of(port, struct mxser_port, port);
564 unsigned long flags;
565
566 spin_lock_irqsave(&mp->slock, flags);
Alan Coxfcc8ac12009-06-11 12:24:17 +0100567 if (on)
568 outb(inb(mp->ioaddr + UART_MCR) |
569 UART_MCR_DTR | UART_MCR_RTS, mp->ioaddr + UART_MCR);
570 else
571 outb(inb(mp->ioaddr + UART_MCR)&~(UART_MCR_DTR | UART_MCR_RTS),
572 mp->ioaddr + UART_MCR);
Alan Cox5d951fb2009-01-02 13:45:19 +0000573 spin_unlock_irqrestore(&mp->slock, flags);
574}
575
Jiri Slabydc33f642021-06-18 08:14:11 +0200576static int mxser_set_baud(struct tty_struct *tty, speed_t newspd)
Jiri Slaby1c456072008-02-07 00:16:46 -0800577{
Alan Cox216ba022008-10-13 10:40:19 +0100578 struct mxser_port *info = tty->driver_data;
Jiri Slaby104583b2017-09-12 12:39:54 +0200579 unsigned int quot = 0, baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800580 unsigned char cval;
Jiri Slaby104583b2017-09-12 12:39:54 +0200581 u64 timeout;
Jiri Slaby1c456072008-02-07 00:16:46 -0800582
Alan Cox216ba022008-10-13 10:40:19 +0100583 if (!info->ioaddr)
Jiri Slaby1c456072008-02-07 00:16:46 -0800584 return -1;
585
Jiri Slaby928f9462021-06-18 08:14:16 +0200586 if (newspd > info->board->max_baud)
Jiri Slaby1c456072008-02-07 00:16:46 -0800587 return -1;
588
589 if (newspd == 134) {
590 quot = 2 * info->baud_base / 269;
Alan Cox216ba022008-10-13 10:40:19 +0100591 tty_encode_baud_rate(tty, 134, 134);
Jiri Slaby1c456072008-02-07 00:16:46 -0800592 } else if (newspd) {
593 quot = info->baud_base / newspd;
594 if (quot == 0)
595 quot = 1;
596 baud = info->baud_base/quot;
Alan Cox216ba022008-10-13 10:40:19 +0100597 tty_encode_baud_rate(tty, baud, baud);
Jiri Slaby1c456072008-02-07 00:16:46 -0800598 } else {
599 quot = 0;
600 }
601
Jiri Slaby104583b2017-09-12 12:39:54 +0200602 /*
603 * worst case (128 * 1000 * 10 * 18432) needs 35 bits, so divide in the
604 * u64 domain
605 */
606 timeout = (u64)info->xmit_fifo_size * HZ * 10 * quot;
607 do_div(timeout, info->baud_base);
608 info->timeout = timeout + HZ / 50; /* Add .02 seconds of slop */
Jiri Slaby1c456072008-02-07 00:16:46 -0800609
610 if (quot) {
611 info->MCR |= UART_MCR_DTR;
612 outb(info->MCR, info->ioaddr + UART_MCR);
613 } else {
614 info->MCR &= ~UART_MCR_DTR;
615 outb(info->MCR, info->ioaddr + UART_MCR);
616 return 0;
617 }
618
619 cval = inb(info->ioaddr + UART_LCR);
620
621 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
622
623 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
624 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
625 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
626
627#ifdef BOTHER
Alan Cox216ba022008-10-13 10:40:19 +0100628 if (C_BAUD(tty) == BOTHER) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800629 quot = info->baud_base % newspd;
630 quot *= 8;
631 if (quot % newspd > newspd / 2) {
632 quot /= newspd;
633 quot++;
634 } else
635 quot /= newspd;
636
Christoph Hellwig148ff862008-04-30 00:54:29 -0700637 mxser_set_must_enum_value(info->ioaddr, quot);
Jiri Slaby1c456072008-02-07 00:16:46 -0800638 } else
639#endif
Christoph Hellwig148ff862008-04-30 00:54:29 -0700640 mxser_set_must_enum_value(info->ioaddr, 0);
Jiri Slaby1c456072008-02-07 00:16:46 -0800641
642 return 0;
643}
644
645/*
646 * This routine is called to set the UART divisor registers to match
647 * the specified baud rate for a serial port.
648 */
Jason Yanbeca62c2020-05-06 14:17:35 +0800649static void mxser_change_speed(struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -0800650{
Alan Cox216ba022008-10-13 10:40:19 +0100651 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800652 unsigned cflag, cval, fcr;
Jiri Slaby1c456072008-02-07 00:16:46 -0800653 unsigned char status;
654
Alan Coxadc8d742012-07-14 15:31:47 +0100655 cflag = tty->termios.c_cflag;
Alan Cox216ba022008-10-13 10:40:19 +0100656 if (!info->ioaddr)
Jason Yanbeca62c2020-05-06 14:17:35 +0800657 return;
Jiri Slaby1c456072008-02-07 00:16:46 -0800658
Jiri Slabyef3dff82021-06-18 08:14:29 +0200659 mxser_set_baud(tty, tty_get_baud_rate(tty));
Jiri Slaby1c456072008-02-07 00:16:46 -0800660
661 /* byte size and parity */
662 switch (cflag & CSIZE) {
663 case CS5:
664 cval = 0x00;
665 break;
666 case CS6:
667 cval = 0x01;
668 break;
669 case CS7:
670 cval = 0x02;
671 break;
672 case CS8:
673 cval = 0x03;
674 break;
675 default:
676 cval = 0x00;
677 break; /* too keep GCC shut... */
678 }
679 if (cflag & CSTOPB)
680 cval |= 0x04;
681 if (cflag & PARENB)
682 cval |= UART_LCR_PARITY;
683 if (!(cflag & PARODD))
684 cval |= UART_LCR_EPAR;
685 if (cflag & CMSPAR)
686 cval |= UART_LCR_SPAR;
687
688 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
Jiri Slaby292955a2021-06-18 08:14:13 +0200689 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800690 fcr = UART_FCR_ENABLE_FIFO;
691 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700692 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800693 } else
694 fcr = 0;
695 } else {
696 fcr = UART_FCR_ENABLE_FIFO;
Jiri Slaby292955a2021-06-18 08:14:13 +0200697 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800698 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700699 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800700 } else {
Jiri Slabydc33f642021-06-18 08:14:11 +0200701 switch (info->rx_high_water) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800702 case 1:
703 fcr |= UART_FCR_TRIGGER_1;
704 break;
705 case 4:
706 fcr |= UART_FCR_TRIGGER_4;
707 break;
708 case 8:
709 fcr |= UART_FCR_TRIGGER_8;
710 break;
711 default:
712 fcr |= UART_FCR_TRIGGER_14;
713 break;
714 }
715 }
716 }
717
718 /* CTS flow control flag and modem status interrupts */
719 info->IER &= ~UART_IER_MSI;
720 info->MCR &= ~UART_MCR_AFE;
Peter Hurley5604a982016-04-09 17:53:21 -0700721 tty_port_set_cts_flow(&info->port, cflag & CRTSCTS);
Jiri Slaby1c456072008-02-07 00:16:46 -0800722 if (cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800723 info->IER |= UART_IER_MSI;
Jiri Slaby292955a2021-06-18 08:14:13 +0200724 if ((info->type == PORT_16550A) || (info->board->must_hwid)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800725 info->MCR |= UART_MCR_AFE;
726 } else {
727 status = inb(info->ioaddr + UART_MSR);
Alan Cox216ba022008-10-13 10:40:19 +0100728 if (tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800729 if (status & UART_MSR_CTS) {
Alan Cox216ba022008-10-13 10:40:19 +0100730 tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800731 if (info->type != PORT_16550A &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200732 !info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800733 outb(info->IER & ~UART_IER_THRI,
734 info->ioaddr +
735 UART_IER);
736 info->IER |= UART_IER_THRI;
737 outb(info->IER, info->ioaddr +
738 UART_IER);
739 }
Alan Cox216ba022008-10-13 10:40:19 +0100740 tty_wakeup(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800741 }
742 } else {
743 if (!(status & UART_MSR_CTS)) {
Alan Cox216ba022008-10-13 10:40:19 +0100744 tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800745 if ((info->type != PORT_16550A) &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200746 (!info->board->must_hwid)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800747 info->IER &= ~UART_IER_THRI;
748 outb(info->IER, info->ioaddr +
749 UART_IER);
750 }
751 }
752 }
753 }
Jiri Slaby1c456072008-02-07 00:16:46 -0800754 }
755 outb(info->MCR, info->ioaddr + UART_MCR);
Peter Hurley2d686552016-04-09 17:53:23 -0700756 tty_port_set_check_carrier(&info->port, ~cflag & CLOCAL);
757 if (~cflag & CLOCAL)
Jiri Slaby1c456072008-02-07 00:16:46 -0800758 info->IER |= UART_IER_MSI;
Jiri Slaby1c456072008-02-07 00:16:46 -0800759 outb(info->IER, info->ioaddr + UART_IER);
760
761 /*
762 * Set up parity check flag
763 */
764 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
Alan Cox216ba022008-10-13 10:40:19 +0100765 if (I_INPCK(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800766 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Alan Cox216ba022008-10-13 10:40:19 +0100767 if (I_BRKINT(tty) || I_PARMRK(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800768 info->read_status_mask |= UART_LSR_BI;
769
770 info->ignore_status_mask = 0;
771
Alan Cox216ba022008-10-13 10:40:19 +0100772 if (I_IGNBRK(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800773 info->ignore_status_mask |= UART_LSR_BI;
774 info->read_status_mask |= UART_LSR_BI;
775 /*
776 * If we're ignore parity and break indicators, ignore
777 * overruns too. (For real raw support).
778 */
Alan Cox216ba022008-10-13 10:40:19 +0100779 if (I_IGNPAR(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800780 info->ignore_status_mask |=
781 UART_LSR_OE |
782 UART_LSR_PE |
783 UART_LSR_FE;
784 info->read_status_mask |=
785 UART_LSR_OE |
786 UART_LSR_PE |
787 UART_LSR_FE;
788 }
789 }
Jiri Slaby292955a2021-06-18 08:14:13 +0200790 if (info->board->must_hwid) {
Alan Cox216ba022008-10-13 10:40:19 +0100791 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
792 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
793 if (I_IXON(tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700794 mxser_enable_must_rx_software_flow_control(
795 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800796 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700797 mxser_disable_must_rx_software_flow_control(
798 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800799 }
Alan Cox216ba022008-10-13 10:40:19 +0100800 if (I_IXOFF(tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700801 mxser_enable_must_tx_software_flow_control(
802 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800803 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700804 mxser_disable_must_tx_software_flow_control(
805 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800806 }
807 }
808
809
810 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
811 outb(cval, info->ioaddr + UART_LCR);
Jiri Slaby1c456072008-02-07 00:16:46 -0800812}
813
Alan Cox216ba022008-10-13 10:40:19 +0100814static void mxser_check_modem_status(struct tty_struct *tty,
815 struct mxser_port *port, int status)
Jiri Slaby1c456072008-02-07 00:16:46 -0800816{
817 /* update input line counters */
818 if (status & UART_MSR_TERI)
819 port->icount.rng++;
820 if (status & UART_MSR_DDSR)
821 port->icount.dsr++;
822 if (status & UART_MSR_DDCD)
823 port->icount.dcd++;
824 if (status & UART_MSR_DCTS)
825 port->icount.cts++;
Alan Coxbdc04e32009-09-19 13:13:31 -0700826 wake_up_interruptible(&port->port.delta_msr_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800827
Peter Hurley2d686552016-04-09 17:53:23 -0700828 if (tty_port_check_carrier(&port->port) && (status & UART_MSR_DDCD)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800829 if (status & UART_MSR_DCD)
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100830 wake_up_interruptible(&port->port.open_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800831 }
832
Huang Shijief21ec3d2012-08-22 22:13:36 -0400833 if (tty_port_cts_enabled(&port->port)) {
Alan Cox216ba022008-10-13 10:40:19 +0100834 if (tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800835 if (status & UART_MSR_CTS) {
Alan Cox216ba022008-10-13 10:40:19 +0100836 tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800837
838 if ((port->type != PORT_16550A) &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200839 (!port->board->must_hwid)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800840 outb(port->IER & ~UART_IER_THRI,
841 port->ioaddr + UART_IER);
842 port->IER |= UART_IER_THRI;
843 outb(port->IER, port->ioaddr +
844 UART_IER);
845 }
Alan Cox216ba022008-10-13 10:40:19 +0100846 tty_wakeup(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800847 }
848 } else {
849 if (!(status & UART_MSR_CTS)) {
Alan Cox216ba022008-10-13 10:40:19 +0100850 tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800851 if (port->type != PORT_16550A &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200852 !port->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800853 port->IER &= ~UART_IER_THRI;
854 outb(port->IER, port->ioaddr +
855 UART_IER);
856 }
857 }
858 }
859 }
860}
861
Alan Cox67691402009-11-30 13:17:35 +0000862static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -0800863{
Alan Cox67691402009-11-30 13:17:35 +0000864 struct mxser_port *info = container_of(port, struct mxser_port, port);
Jiri Slaby1c456072008-02-07 00:16:46 -0800865 unsigned long page;
866 unsigned long flags;
867
868 page = __get_free_page(GFP_KERNEL);
869 if (!page)
870 return -ENOMEM;
871
872 spin_lock_irqsave(&info->slock, flags);
873
Jiri Slaby1c456072008-02-07 00:16:46 -0800874 if (!info->ioaddr || !info->type) {
Alan Cox216ba022008-10-13 10:40:19 +0100875 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800876 free_page(page);
877 spin_unlock_irqrestore(&info->slock, flags);
878 return 0;
879 }
Alan Cox67691402009-11-30 13:17:35 +0000880 info->port.xmit_buf = (unsigned char *) page;
Jiri Slaby1c456072008-02-07 00:16:46 -0800881
882 /*
883 * Clear the FIFO buffers and disable them
884 * (they will be reenabled in mxser_change_speed())
885 */
Jiri Slaby292955a2021-06-18 08:14:13 +0200886 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -0800887 outb((UART_FCR_CLEAR_RCVR |
888 UART_FCR_CLEAR_XMIT |
889 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
890 else
891 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
892 info->ioaddr + UART_FCR);
893
894 /*
895 * At this point there's no way the LSR could still be 0xFF;
896 * if it is, then bail out, because there's likely no UART
897 * here.
898 */
899 if (inb(info->ioaddr + UART_LSR) == 0xff) {
900 spin_unlock_irqrestore(&info->slock, flags);
901 if (capable(CAP_SYS_ADMIN)) {
Jiri Slabyf43a510d2009-12-09 12:34:15 -0800902 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800903 return 0;
904 } else
905 return -ENODEV;
906 }
907
908 /*
909 * Clear the interrupt registers.
910 */
911 (void) inb(info->ioaddr + UART_LSR);
912 (void) inb(info->ioaddr + UART_RX);
913 (void) inb(info->ioaddr + UART_IIR);
914 (void) inb(info->ioaddr + UART_MSR);
915
916 /*
917 * Now, initialize the UART
918 */
919 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
920 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
921 outb(info->MCR, info->ioaddr + UART_MCR);
922
923 /*
924 * Finally, enable interrupts
925 */
926 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
927
Jiri Slaby292955a2021-06-18 08:14:13 +0200928 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -0800929 info->IER |= MOXA_MUST_IER_EGDAI;
930 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
931
932 /*
933 * And clear the interrupt registers again for luck.
934 */
935 (void) inb(info->ioaddr + UART_LSR);
936 (void) inb(info->ioaddr + UART_RX);
937 (void) inb(info->ioaddr + UART_IIR);
938 (void) inb(info->ioaddr + UART_MSR);
939
Alan Cox216ba022008-10-13 10:40:19 +0100940 clear_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800941 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
942
943 /*
944 * and set the speed of the serial port
945 */
Jiri Slaby27997072017-09-12 12:39:55 +0200946 mxser_change_speed(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800947 spin_unlock_irqrestore(&info->slock, flags);
948
949 return 0;
950}
951
952/*
Alan Cox67691402009-11-30 13:17:35 +0000953 * This routine will shutdown a serial port
Jiri Slaby1c456072008-02-07 00:16:46 -0800954 */
Alan Cox67691402009-11-30 13:17:35 +0000955static void mxser_shutdown_port(struct tty_port *port)
Jiri Slaby1c456072008-02-07 00:16:46 -0800956{
Alan Cox67691402009-11-30 13:17:35 +0000957 struct mxser_port *info = container_of(port, struct mxser_port, port);
Jiri Slaby1c456072008-02-07 00:16:46 -0800958 unsigned long flags;
959
Jiri Slaby1c456072008-02-07 00:16:46 -0800960 spin_lock_irqsave(&info->slock, flags);
961
962 /*
963 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
964 * here so the queue might never be waken up
965 */
Alan Coxbdc04e32009-09-19 13:13:31 -0700966 wake_up_interruptible(&info->port.delta_msr_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800967
968 /*
Alan Cox67691402009-11-30 13:17:35 +0000969 * Free the xmit buffer, if necessary
Jiri Slaby1c456072008-02-07 00:16:46 -0800970 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100971 if (info->port.xmit_buf) {
972 free_page((unsigned long) info->port.xmit_buf);
973 info->port.xmit_buf = NULL;
Jiri Slaby1c456072008-02-07 00:16:46 -0800974 }
975
976 info->IER = 0;
977 outb(0x00, info->ioaddr + UART_IER);
978
Jiri Slaby1c456072008-02-07 00:16:46 -0800979 /* clear Rx/Tx FIFO's */
Jiri Slaby292955a2021-06-18 08:14:13 +0200980 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -0800981 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
982 MOXA_MUST_FCR_GDA_MODE_ENABLE,
983 info->ioaddr + UART_FCR);
984 else
985 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
986 info->ioaddr + UART_FCR);
987
988 /* read data port to reset things */
989 (void) inb(info->ioaddr + UART_RX);
990
Jiri Slaby1c456072008-02-07 00:16:46 -0800991
Jiri Slaby292955a2021-06-18 08:14:13 +0200992 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -0800993 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
994
995 spin_unlock_irqrestore(&info->slock, flags);
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998/*
999 * This routine is called whenever a serial port is opened. It
1000 * enables interrupts for a serial port, linking in its async structure into
1001 * the IRQ chain. It also performs the serial-specific
1002 * initialization for the tty structure.
1003 */
1004static int mxser_open(struct tty_struct *tty, struct file *filp)
1005{
Jiri Slaby1c456072008-02-07 00:16:46 -08001006 struct mxser_port *info;
Jiri Slaby389fc822021-06-18 08:14:38 +02001007 int line = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Jiri Slaby1c456072008-02-07 00:16:46 -08001009 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1010 if (!info->ioaddr)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001011 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Alan Coxa2d1e352010-04-23 16:01:18 +01001013 tty->driver_data = info;
Alan Cox67691402009-11-30 13:17:35 +00001014 return tty_port_open(&info->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
Alan Cox978e5952008-04-30 00:53:59 -07001017static void mxser_flush_buffer(struct tty_struct *tty)
1018{
1019 struct mxser_port *info = tty->driver_data;
1020 char fcr;
1021 unsigned long flags;
1022
1023
1024 spin_lock_irqsave(&info->slock, flags);
1025 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1026
1027 fcr = inb(info->ioaddr + UART_FCR);
1028 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1029 info->ioaddr + UART_FCR);
1030 outb(fcr, info->ioaddr + UART_FCR);
1031
1032 spin_unlock_irqrestore(&info->slock, flags);
1033
1034 tty_wakeup(tty);
1035}
1036
1037
Alan Cox67691402009-11-30 13:17:35 +00001038static void mxser_close_port(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Alan Cox1e2b0252009-09-19 13:13:21 -07001040 struct mxser_port *info = container_of(port, struct mxser_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 * At this point we stop accepting input. To do this, we
1044 * disable the receive line status interrupts, and tell the
1045 * interrupt driver to stop checking the data ready bit in the
1046 * line status register.
1047 */
1048 info->IER &= ~UART_IER_RLSI;
Jiri Slaby292955a2021-06-18 08:14:13 +02001049 if (info->board->must_hwid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001051
Alan Cox67691402009-11-30 13:17:35 +00001052 outb(info->IER, info->ioaddr + UART_IER);
1053 /*
1054 * Before we drop DTR, make sure the UART transmitter
1055 * has completely drained; this is especially
1056 * important if there is a transmit FIFO!
1057 */
1058 timeout = jiffies + HZ;
1059 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1060 schedule_timeout_interruptible(5);
1061 if (time_after(jiffies, timeout))
1062 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
Alan Cox1e2b0252009-09-19 13:13:21 -07001064}
1065
1066/*
1067 * This routine is called when the serial port gets closed. First, we
1068 * wait for the last remaining data to be sent. Then, we unlink its
1069 * async structure from the interrupt chain if necessary, and we free
1070 * that IRQ if nothing is left in the chain.
1071 */
1072static void mxser_close(struct tty_struct *tty, struct file *filp)
1073{
1074 struct mxser_port *info = tty->driver_data;
1075 struct tty_port *port = &info->port;
1076
Jiri Slaby389fc822021-06-18 08:14:38 +02001077 if (info == NULL)
Alan Cox1e2b0252009-09-19 13:13:21 -07001078 return;
1079 if (tty_port_close_start(port, tty, filp) == 0)
1080 return;
Peter Hurleycd7b4b32016-01-10 14:51:38 -08001081 info->closing = 1;
Alan Cox67691402009-11-30 13:17:35 +00001082 mutex_lock(&port->mutex);
1083 mxser_close_port(port);
Alan Cox1e2b0252009-09-19 13:13:21 -07001084 mxser_flush_buffer(tty);
Peter Hurleyd41861c2016-04-09 17:53:25 -07001085 if (tty_port_initialized(port) && C_HUPCL(tty))
1086 tty_port_lower_dtr_rts(port);
Alan Cox67691402009-11-30 13:17:35 +00001087 mxser_shutdown_port(port);
Peter Hurleyd41861c2016-04-09 17:53:25 -07001088 tty_port_set_initialized(port, 0);
Alan Cox67691402009-11-30 13:17:35 +00001089 mutex_unlock(&port->mutex);
Peter Hurleycd7b4b32016-01-10 14:51:38 -08001090 info->closing = 0;
Alan Coxa6614992009-01-02 13:46:50 +00001091 /* Right now the tty_port set is done outside of the close_end helper
1092 as we don't yet have everyone using refcounts */
1093 tty_port_close_end(port, tty);
1094 tty_port_tty_set(port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
1097static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1098{
1099 int c, total = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001100 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 unsigned long flags;
1102
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001103 if (!info->port.xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001104 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001107 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1108 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (c <= 0)
1110 break;
1111
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001112 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001114 info->xmit_head = (info->xmit_head + c) &
1115 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 info->xmit_cnt += c;
1117 spin_unlock_irqrestore(&info->slock, flags);
1118
1119 buf += c;
1120 count -= c;
1121 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001124 if (info->xmit_cnt && !tty->flow.stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001125 if (!tty->hw_stopped ||
1126 (info->type == PORT_16550A) ||
Jiri Slaby292955a2021-06-18 08:14:13 +02001127 (info->board->must_hwid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001129 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1130 UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001132 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 spin_unlock_irqrestore(&info->slock, flags);
1134 }
1135 }
1136 return total;
1137}
1138
Alan Cox0be2ead2008-04-30 00:54:03 -07001139static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Jiri Slaby1c456072008-02-07 00:16:46 -08001141 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 unsigned long flags;
1143
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001144 if (!info->port.xmit_buf)
Alan Cox0be2ead2008-04-30 00:54:03 -07001145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
Alan Cox0be2ead2008-04-30 00:54:03 -07001148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001151 info->port.xmit_buf[info->xmit_head++] = ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1153 info->xmit_cnt++;
1154 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001155 if (!tty->flow.stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001156 if (!tty->hw_stopped ||
1157 (info->type == PORT_16550A) ||
Jiri Slaby292955a2021-06-18 08:14:13 +02001158 info->board->must_hwid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001160 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001162 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 spin_unlock_irqrestore(&info->slock, flags);
1164 }
1165 }
Alan Cox0be2ead2008-04-30 00:54:03 -07001166 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
1169
1170static void mxser_flush_chars(struct tty_struct *tty)
1171{
Jiri Slaby1c456072008-02-07 00:16:46 -08001172 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 unsigned long flags;
1174
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001175 if (info->xmit_cnt <= 0 || tty->flow.stopped || !info->port.xmit_buf ||
Jiri Slabyace7dd92008-07-25 01:48:22 -07001176 (tty->hw_stopped && info->type != PORT_16550A &&
Jiri Slaby292955a2021-06-18 08:14:13 +02001177 !info->board->must_hwid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return;
1179
1180 spin_lock_irqsave(&info->slock, flags);
1181
Jiri Slaby1c456072008-02-07 00:16:46 -08001182 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001184 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 spin_unlock_irqrestore(&info->slock, flags);
1187}
1188
Jiri Slaby03b3b1a2021-05-05 11:19:15 +02001189static unsigned int mxser_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Jiri Slaby1c456072008-02-07 00:16:46 -08001191 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int ret;
1193
1194 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
Jiri Slabyace7dd92008-07-25 01:48:22 -07001195 return ret < 0 ? 0 : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Jiri Slabyfff4ef12021-05-05 11:19:19 +02001198static unsigned int mxser_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Jiri Slaby1c456072008-02-07 00:16:46 -08001200 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return info->xmit_cnt;
1202}
1203
Jiri Slaby1c456072008-02-07 00:16:46 -08001204/*
1205 * ------------------------------------------------------------
1206 * friends of mxser_ioctl()
1207 * ------------------------------------------------------------
1208 */
Alan Cox216ba022008-10-13 10:40:19 +01001209static int mxser_get_serial_info(struct tty_struct *tty,
Al Viro6da5b582018-09-11 22:22:06 -04001210 struct serial_struct *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Alan Cox216ba022008-10-13 10:40:19 +01001212 struct mxser_port *info = tty->driver_data;
Al Viro6da5b582018-09-11 22:22:06 -04001213 struct tty_port *port = &info->port;
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001214 unsigned int closing_wait, close_delay;
Al Viro6da5b582018-09-11 22:22:06 -04001215
Al Viro6da5b582018-09-11 22:22:06 -04001216 mutex_lock(&port->mutex);
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001217
1218 close_delay = jiffies_to_msecs(info->port.close_delay) / 10;
1219 closing_wait = info->port.closing_wait;
1220 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1221 closing_wait = jiffies_to_msecs(closing_wait) / 10;
1222
Al Viro6da5b582018-09-11 22:22:06 -04001223 ss->type = info->type,
1224 ss->line = tty->index,
1225 ss->port = info->ioaddr,
1226 ss->irq = info->board->irq,
1227 ss->flags = info->port.flags,
1228 ss->baud_base = info->baud_base,
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001229 ss->close_delay = close_delay;
1230 ss->closing_wait = closing_wait;
Al Viro6da5b582018-09-11 22:22:06 -04001231 ss->custom_divisor = info->custom_divisor,
1232 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001233 return 0;
1234}
1235
Alan Cox216ba022008-10-13 10:40:19 +01001236static int mxser_set_serial_info(struct tty_struct *tty,
Al Viro6da5b582018-09-11 22:22:06 -04001237 struct serial_struct *ss)
Jiri Slaby1c456072008-02-07 00:16:46 -08001238{
Alan Cox216ba022008-10-13 10:40:19 +01001239 struct mxser_port *info = tty->driver_data;
Alan Cox07f86c02009-11-30 13:17:41 +00001240 struct tty_port *port = &info->port;
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001241 speed_t baud;
Jiri Slaby1c456072008-02-07 00:16:46 -08001242 unsigned long sl_flags;
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001243 unsigned int flags, close_delay, closing_wait;
Jiri Slaby1c456072008-02-07 00:16:46 -08001244 int retval = 0;
1245
Al Viro6da5b582018-09-11 22:22:06 -04001246 if (tty_io_error(tty))
1247 return -EIO;
Jiri Slaby1c456072008-02-07 00:16:46 -08001248
Al Viro6da5b582018-09-11 22:22:06 -04001249 mutex_lock(&port->mutex);
1250 if (!info->ioaddr) {
1251 mutex_unlock(&port->mutex);
1252 return -ENODEV;
1253 }
1254
1255 if (ss->irq != info->board->irq ||
1256 ss->port != info->ioaddr) {
1257 mutex_unlock(&port->mutex);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001258 return -EINVAL;
Al Viro6da5b582018-09-11 22:22:06 -04001259 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001260
Alan Cox07f86c02009-11-30 13:17:41 +00001261 flags = port->flags & ASYNC_SPD_MASK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001262
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001263 close_delay = msecs_to_jiffies(ss->close_delay * 10);
1264 closing_wait = ss->closing_wait;
1265 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1266 closing_wait = msecs_to_jiffies(closing_wait * 10);
1267
Jiri Slaby1c456072008-02-07 00:16:46 -08001268 if (!capable(CAP_SYS_ADMIN)) {
Al Viro6da5b582018-09-11 22:22:06 -04001269 if ((ss->baud_base != info->baud_base) ||
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001270 (close_delay != info->port.close_delay) ||
Johan Hovoldb91cfb22021-04-07 12:23:32 +02001271 (closing_wait != info->port.closing_wait) ||
Al Viro6da5b582018-09-11 22:22:06 -04001272 ((ss->flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK))) {
1273 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001274 return -EPERM;
Al Viro6da5b582018-09-11 22:22:06 -04001275 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001276 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
Al Viro6da5b582018-09-11 22:22:06 -04001277 (ss->flags & ASYNC_USR_MASK));
Jiri Slaby1c456072008-02-07 00:16:46 -08001278 } else {
1279 /*
1280 * OK, past this point, all the error checking has been done.
1281 * At this point, we start making changes.....
1282 */
Alan Cox07f86c02009-11-30 13:17:41 +00001283 port->flags = ((port->flags & ~ASYNC_FLAGS) |
Al Viro6da5b582018-09-11 22:22:06 -04001284 (ss->flags & ASYNC_FLAGS));
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001285 port->close_delay = close_delay;
1286 port->closing_wait = closing_wait;
Alan Cox07f86c02009-11-30 13:17:41 +00001287 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
Al Viro6da5b582018-09-11 22:22:06 -04001288 (ss->baud_base != info->baud_base ||
1289 ss->custom_divisor !=
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001290 info->custom_divisor)) {
Al Viro6da5b582018-09-11 22:22:06 -04001291 if (ss->custom_divisor == 0) {
1292 mutex_unlock(&port->mutex);
Alan Cox07f86c02009-11-30 13:17:41 +00001293 return -EINVAL;
Al Viro6da5b582018-09-11 22:22:06 -04001294 }
1295 baud = ss->baud_base / ss->custom_divisor;
Alan Cox216ba022008-10-13 10:40:19 +01001296 tty_encode_baud_rate(tty, baud, baud);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001297 }
Johan Hovoldb91cfb22021-04-07 12:23:32 +02001298
1299 info->type = ss->type;
1300
1301 process_txrx_fifo(info);
Jiri Slaby1c456072008-02-07 00:16:46 -08001302 }
1303
Peter Hurleyd41861c2016-04-09 17:53:25 -07001304 if (tty_port_initialized(port)) {
Alan Cox07f86c02009-11-30 13:17:41 +00001305 if (flags != (port->flags & ASYNC_SPD_MASK)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001306 spin_lock_irqsave(&info->slock, sl_flags);
Jiri Slaby27997072017-09-12 12:39:55 +02001307 mxser_change_speed(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001308 spin_unlock_irqrestore(&info->slock, sl_flags);
1309 }
Alan Cox67691402009-11-30 13:17:35 +00001310 } else {
Alan Cox07f86c02009-11-30 13:17:41 +00001311 retval = mxser_activate(port, tty);
Alan Cox67691402009-11-30 13:17:35 +00001312 if (retval == 0)
Peter Hurleyd41861c2016-04-09 17:53:25 -07001313 tty_port_set_initialized(port, 1);
Alan Cox67691402009-11-30 13:17:35 +00001314 }
Al Viro6da5b582018-09-11 22:22:06 -04001315 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001316 return retval;
1317}
1318
1319/*
1320 * mxser_get_lsr_info - get line status register info
1321 *
1322 * Purpose: Let user call ioctl() to get info when the UART physically
1323 * is emptied. On bus types like RS485, the transmitter must
1324 * release the bus after transmitting. This must be done when
1325 * the transmit shift register is empty, not be done when the
1326 * transmit holding register is empty. This functionality
1327 * allows an RS485 driver to be written in user space.
1328 */
1329static int mxser_get_lsr_info(struct mxser_port *info,
1330 unsigned int __user *value)
1331{
1332 unsigned char status;
1333 unsigned int result;
1334 unsigned long flags;
1335
1336 spin_lock_irqsave(&info->slock, flags);
1337 status = inb(info->ioaddr + UART_LSR);
1338 spin_unlock_irqrestore(&info->slock, flags);
1339 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1340 return put_user(result, value);
1341}
1342
Alan Cox60b33c12011-02-14 16:26:14 +00001343static int mxser_tiocmget(struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -08001344{
1345 struct mxser_port *info = tty->driver_data;
1346 unsigned char control, status;
1347 unsigned long flags;
1348
Peter Hurley18900ca2016-04-09 17:06:48 -07001349 if (tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001350 return -EIO;
1351
1352 control = info->MCR;
1353
1354 spin_lock_irqsave(&info->slock, flags);
1355 status = inb(info->ioaddr + UART_MSR);
1356 if (status & UART_MSR_ANY_DELTA)
Alan Cox216ba022008-10-13 10:40:19 +01001357 mxser_check_modem_status(tty, info, status);
Jiri Slaby1c456072008-02-07 00:16:46 -08001358 spin_unlock_irqrestore(&info->slock, flags);
1359 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1360 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1361 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1362 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1363 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1364 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1365}
1366
Alan Cox20b9d172011-02-14 16:26:50 +00001367static int mxser_tiocmset(struct tty_struct *tty,
Jiri Slaby1c456072008-02-07 00:16:46 -08001368 unsigned int set, unsigned int clear)
1369{
1370 struct mxser_port *info = tty->driver_data;
1371 unsigned long flags;
1372
Peter Hurley18900ca2016-04-09 17:06:48 -07001373 if (tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001374 return -EIO;
1375
1376 spin_lock_irqsave(&info->slock, flags);
1377
1378 if (set & TIOCM_RTS)
1379 info->MCR |= UART_MCR_RTS;
1380 if (set & TIOCM_DTR)
1381 info->MCR |= UART_MCR_DTR;
1382
1383 if (clear & TIOCM_RTS)
1384 info->MCR &= ~UART_MCR_RTS;
1385 if (clear & TIOCM_DTR)
1386 info->MCR &= ~UART_MCR_DTR;
1387
1388 outb(info->MCR, info->ioaddr + UART_MCR);
1389 spin_unlock_irqrestore(&info->slock, flags);
1390 return 0;
1391}
1392
Jiri Slaby1c456072008-02-07 00:16:46 -08001393static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1394 struct async_icount *cprev)
1395{
1396 struct async_icount cnow;
1397 unsigned long flags;
1398 int ret;
1399
1400 spin_lock_irqsave(&info->slock, flags);
1401 cnow = info->icount; /* atomic copy */
1402 spin_unlock_irqrestore(&info->slock, flags);
1403
1404 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1405 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1406 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1407 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1408
1409 *cprev = cnow;
1410
1411 return ret;
1412}
1413
Alan Cox6caa76b2011-02-14 16:27:22 +00001414static int mxser_ioctl(struct tty_struct *tty,
Jiri Slaby1c456072008-02-07 00:16:46 -08001415 unsigned int cmd, unsigned long arg)
1416{
1417 struct mxser_port *info = tty->driver_data;
1418 struct async_icount cnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 unsigned long flags;
1420 void __user *argp = (void __user *)arg;
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001423 int p;
1424 unsigned long opmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1426 int shiftbit;
1427 unsigned char val, mask;
1428
Jiri Slaby292955a2021-06-18 08:14:13 +02001429 if (info->board->must_hwid != MOXA_MUST_MU860_HWID)
Matwey V. Kornilove037f952013-05-22 11:13:38 +04001430 return -EFAULT;
1431
Jiri Slaby1c456072008-02-07 00:16:46 -08001432 p = tty->index % 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (cmd == MOXA_SET_OP_MODE) {
1434 if (get_user(opmode, (int __user *) argp))
1435 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001436 if (opmode != RS232_MODE &&
1437 opmode != RS485_2WIRE_MODE &&
1438 opmode != RS422_MODE &&
1439 opmode != RS485_4WIRE_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return -EFAULT;
1441 mask = ModeMask[p];
1442 shiftbit = p * 2;
Alan Cox07f86c02009-11-30 13:17:41 +00001443 spin_lock_irq(&info->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 val = inb(info->opmode_ioaddr);
1445 val &= mask;
1446 val |= (opmode << shiftbit);
1447 outb(val, info->opmode_ioaddr);
Alan Cox07f86c02009-11-30 13:17:41 +00001448 spin_unlock_irq(&info->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 } else {
1450 shiftbit = p * 2;
Alan Cox07f86c02009-11-30 13:17:41 +00001451 spin_lock_irq(&info->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 opmode = inb(info->opmode_ioaddr) >> shiftbit;
Alan Cox07f86c02009-11-30 13:17:41 +00001453 spin_unlock_irq(&info->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 opmode &= OP_MODE_MASK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001455 if (put_user(opmode, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return -EFAULT;
1457 }
1458 return 0;
1459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Al Viro6da5b582018-09-11 22:22:06 -04001461 if (cmd != TIOCMIWAIT && tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001462 return -EIO;
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 case TIOCSERGETLSR: /* Get line status register */
Alan Cox9d6d1622008-04-30 00:53:20 -07001466 return mxser_get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 /*
1468 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1469 * - mask passed in arg for lines of interest
1470 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1471 * Caller should use TIOCGICOUNT to see which one it was
1472 */
Jiri Slabyfc838152007-04-23 14:41:04 -07001473 case TIOCMIWAIT:
1474 spin_lock_irqsave(&info->slock, flags);
1475 cnow = info->icount; /* note the counters on entry */
1476 spin_unlock_irqrestore(&info->slock, flags);
1477
Alan Coxbdc04e32009-09-19 13:13:31 -07001478 return wait_event_interruptible(info->port.delta_msr_wait,
Jiri Slaby1c456072008-02-07 00:16:46 -08001479 mxser_cflags_changed(info, arg, &cnow));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 default:
1481 return -ENOIOCTLCMD;
1482 }
1483 return 0;
1484}
1485
Alan Cox05871022010-09-16 18:21:52 +01001486 /*
1487 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1488 * Return: write counters to the user passed counter struct
1489 * NB: both 1->0 and 0->1 transitions are counted except for
1490 * RI where only 0->1 is counted.
1491 */
1492
1493static int mxser_get_icount(struct tty_struct *tty,
1494 struct serial_icounter_struct *icount)
1495
1496{
1497 struct mxser_port *info = tty->driver_data;
1498 struct async_icount cnow;
1499 unsigned long flags;
1500
1501 spin_lock_irqsave(&info->slock, flags);
1502 cnow = info->icount;
1503 spin_unlock_irqrestore(&info->slock, flags);
1504
1505 icount->frame = cnow.frame;
1506 icount->brk = cnow.brk;
1507 icount->overrun = cnow.overrun;
1508 icount->buf_overrun = cnow.buf_overrun;
1509 icount->parity = cnow.parity;
1510 icount->rx = cnow.rx;
1511 icount->tx = cnow.tx;
1512 icount->cts = cnow.cts;
1513 icount->dsr = cnow.dsr;
1514 icount->rng = cnow.rng;
1515 icount->dcd = cnow.dcd;
1516 return 0;
1517}
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519static void mxser_stoprx(struct tty_struct *tty)
1520{
Jiri Slaby1c456072008-02-07 00:16:46 -08001521 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 info->ldisc_stop_rx = 1;
1524 if (I_IXOFF(tty)) {
Jiri Slaby292955a2021-06-18 08:14:13 +02001525 if (info->board->must_hwid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001527 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 info->x_char = STOP_CHAR(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001530 outb(0, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001532 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534 }
1535
Peter Hurley9db276f2016-01-10 20:36:15 -08001536 if (C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001537 info->MCR &= ~UART_MCR_RTS;
1538 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
1540}
1541
1542/*
1543 * This routine is called by the upper-layer tty layer to signal that
1544 * incoming characters should be throttled.
1545 */
1546static void mxser_throttle(struct tty_struct *tty)
1547{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
1551static void mxser_unthrottle(struct tty_struct *tty)
1552{
Jiri Slaby1c456072008-02-07 00:16:46 -08001553 struct mxser_port *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001554
Jiri Slaby1c456072008-02-07 00:16:46 -08001555 /* startrx */
1556 info->ldisc_stop_rx = 0;
1557 if (I_IXOFF(tty)) {
1558 if (info->x_char)
1559 info->x_char = 0;
1560 else {
Jiri Slaby292955a2021-06-18 08:14:13 +02001561 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001562 info->IER |= MOXA_MUST_RECV_ISR;
1563 outb(info->IER, info->ioaddr + UART_IER);
1564 } else {
1565 info->x_char = START_CHAR(tty);
1566 outb(0, info->ioaddr + UART_IER);
1567 info->IER |= UART_IER_THRI;
1568 outb(info->IER, info->ioaddr + UART_IER);
1569 }
1570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572
Peter Hurley9db276f2016-01-10 20:36:15 -08001573 if (C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001574 info->MCR |= UART_MCR_RTS;
1575 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577}
1578
1579/*
1580 * mxser_stop() and mxser_start()
1581 *
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001582 * This routines are called before setting or resetting tty->flow.stopped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 * They enable or disable transmitter interrupts, as necessary.
1584 */
1585static void mxser_stop(struct tty_struct *tty)
1586{
Jiri Slaby1c456072008-02-07 00:16:46 -08001587 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 unsigned long flags;
1589
1590 spin_lock_irqsave(&info->slock, flags);
1591 if (info->IER & UART_IER_THRI) {
1592 info->IER &= ~UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001593 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
1595 spin_unlock_irqrestore(&info->slock, flags);
1596}
1597
1598static void mxser_start(struct tty_struct *tty)
1599{
Jiri Slaby1c456072008-02-07 00:16:46 -08001600 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 unsigned long flags;
1602
1603 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001604 if (info->xmit_cnt && info->port.xmit_buf) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001605 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001607 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
1609 spin_unlock_irqrestore(&info->slock, flags);
1610}
1611
Jiri Slaby1c456072008-02-07 00:16:46 -08001612static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1613{
1614 struct mxser_port *info = tty->driver_data;
1615 unsigned long flags;
1616
1617 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby27997072017-09-12 12:39:55 +02001618 mxser_change_speed(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001619 spin_unlock_irqrestore(&info->slock, flags);
1620
Peter Hurley9db276f2016-01-10 20:36:15 -08001621 if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001622 tty->hw_stopped = 0;
1623 mxser_start(tty);
1624 }
1625
1626 /* Handle sw stopped */
Peter Hurley9db276f2016-01-10 20:36:15 -08001627 if ((old_termios->c_iflag & IXON) && !I_IXON(tty)) {
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001628 tty->flow.stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001629
Jiri Slaby292955a2021-06-18 08:14:13 +02001630 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001631 spin_lock_irqsave(&info->slock, flags);
Christoph Hellwig148ff862008-04-30 00:54:29 -07001632 mxser_disable_must_rx_software_flow_control(
1633 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -08001634 spin_unlock_irqrestore(&info->slock, flags);
1635 }
1636
1637 mxser_start(tty);
1638 }
1639}
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641/*
1642 * mxser_wait_until_sent() --- wait until the transmitter is empty
1643 */
1644static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1645{
Jiri Slaby1c456072008-02-07 00:16:46 -08001646 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 unsigned long orig_jiffies, char_time;
Alan Cox07f86c02009-11-30 13:17:41 +00001648 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 int lsr;
1650
1651 if (info->type == PORT_UNKNOWN)
1652 return;
1653
1654 if (info->xmit_fifo_size == 0)
1655 return; /* Just in case.... */
1656
1657 orig_jiffies = jiffies;
1658 /*
1659 * Set the check interval to be 1/5 of the estimated time to
1660 * send a single character, and make it at least 1. The check
1661 * interval should also be less than the timeout.
1662 *
1663 * Note: we have to use pretty tight timings here to satisfy
1664 * the NIST-PCTS.
1665 */
1666 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1667 char_time = char_time / 5;
1668 if (char_time == 0)
1669 char_time = 1;
1670 if (timeout && timeout < char_time)
1671 char_time = timeout;
1672 /*
1673 * If the transmitter hasn't cleared in twice the approximate
1674 * amount of time to send the entire FIFO, it probably won't
1675 * ever clear. This assumes the UART isn't doing flow
1676 * control, which is currently the case. Hence, if it ever
1677 * takes longer than info->timeout, this is probably due to a
1678 * UART bug of some kind. So, we clamp the timeout parameter at
1679 * 2*info->timeout.
1680 */
1681 if (!timeout || timeout > 2 * info->timeout)
1682 timeout = 2 * info->timeout;
Jiri Slaby8bab5342011-07-14 14:35:15 +02001683
Alan Cox07f86c02009-11-30 13:17:41 +00001684 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001685 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Alan Cox07f86c02009-11-30 13:17:41 +00001686 spin_unlock_irqrestore(&info->slock, flags);
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001687 schedule_timeout_interruptible(char_time);
Alan Cox07f86c02009-11-30 13:17:41 +00001688 spin_lock_irqsave(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (signal_pending(current))
1690 break;
1691 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1692 break;
1693 }
Alan Cox07f86c02009-11-30 13:17:41 +00001694 spin_unlock_irqrestore(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698/*
1699 * This routine is called by tty_hangup() when a hangup is signaled.
1700 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001701static void mxser_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Jiri Slaby1c456072008-02-07 00:16:46 -08001703 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 mxser_flush_buffer(tty);
Alan Cox3b6826b2009-01-02 13:45:58 +00001706 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709/*
1710 * mxser_rs_break() --- routine which turns the break handling on or off
1711 */
Alan Cox9e98966c2008-07-22 11:18:03 +01001712static int mxser_rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Jiri Slaby1c456072008-02-07 00:16:46 -08001714 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 unsigned long flags;
1716
1717 spin_lock_irqsave(&info->slock, flags);
1718 if (break_state == -1)
Jiri Slaby1c456072008-02-07 00:16:46 -08001719 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1720 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 else
Jiri Slaby1c456072008-02-07 00:16:46 -08001722 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1723 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 spin_unlock_irqrestore(&info->slock, flags);
Alan Cox9e98966c2008-07-22 11:18:03 +01001725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001728static bool mxser_receive_chars_new(struct tty_struct *tty,
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001729 struct mxser_port *port, u8 status)
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001730{
1731 enum mxser_must_hwid hwid = port->board->must_hwid;
1732 u8 gdl;
1733
1734 if (hwid == MOXA_OTHER_UART)
1735 return false;
Jiri Slaby70640052021-06-18 08:14:26 +02001736 if (status & UART_LSR_BRK_ERROR_BITS)
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001737 return false;
1738 if (hwid == MOXA_MUST_MU860_HWID && (status & MOXA_MUST_LSR_RERR))
1739 return false;
1740 if (status & MOXA_MUST_LSR_RERR)
1741 return false;
1742
1743 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
1744 if (hwid == MOXA_MUST_MU150_HWID)
1745 gdl &= MOXA_MUST_GDL_MASK;
1746
1747 if (gdl >= tty->receive_room && !port->ldisc_stop_rx)
1748 mxser_stoprx(tty);
1749
1750 while (gdl--) {
1751 u8 ch = inb(port->ioaddr + UART_RX);
1752 tty_insert_flip_char(&port->port, ch, 0);
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001753 }
1754
1755 return true;
1756}
1757
Jiri Slaby0c419422021-06-18 08:14:22 +02001758static u8 mxser_receive_chars_old(struct tty_struct *tty,
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001759 struct mxser_port *port, u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Jiri Slaby0c419422021-06-18 08:14:22 +02001761 enum mxser_must_hwid hwid = port->board->must_hwid;
1762 int recv_room = tty->receive_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 int ignored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 int max = 256;
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001765 int cnt = 0;
Jiri Slaby0c419422021-06-18 08:14:22 +02001766 u8 ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 do {
1769 if (max-- < 0)
1770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Jiri Slaby1c456072008-02-07 00:16:46 -08001772 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby0c419422021-06-18 08:14:22 +02001773 if (hwid && (status & UART_LSR_OE))
Jiri Slaby1c456072008-02-07 00:16:46 -08001774 outb(0x23, port->ioaddr + UART_FCR);
Jiri Slaby15517802021-06-18 08:14:18 +02001775 status &= port->read_status_mask;
1776 if (status & port->ignore_status_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (++ignored > 100)
1778 break;
1779 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001780 char flag = 0;
Jiri Slaby70640052021-06-18 08:14:26 +02001781 if (status & UART_LSR_BRK_ERROR_BITS) {
Jiri Slaby15517802021-06-18 08:14:18 +02001782 if (status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001783 flag = TTY_BREAK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001784 port->icount.brk++;
1785
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001786 if (port->port.flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 do_SAK(tty);
Jiri Slaby15517802021-06-18 08:14:18 +02001788 } else if (status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001789 flag = TTY_PARITY;
Jiri Slaby1c456072008-02-07 00:16:46 -08001790 port->icount.parity++;
Jiri Slaby15517802021-06-18 08:14:18 +02001791 } else if (status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001792 flag = TTY_FRAME;
Jiri Slaby1c456072008-02-07 00:16:46 -08001793 port->icount.frame++;
Jiri Slaby15517802021-06-18 08:14:18 +02001794 } else if (status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001795 flag = TTY_OVERRUN;
Jiri Slaby1c456072008-02-07 00:16:46 -08001796 port->icount.overrun++;
Jiri Slaby6de6e5c2021-06-18 08:14:23 +02001797 }
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001798 }
Jiri Slaby92a19f92013-01-03 15:53:03 +01001799 tty_insert_flip_char(&port->port, ch, flag);
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001800 cnt++;
1801 if (cnt >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001802 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 break;
1805 }
1806
1807 }
1808
Jiri Slaby0c419422021-06-18 08:14:22 +02001809 if (hwid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Jiri Slaby15517802021-06-18 08:14:18 +02001812 status = inb(port->ioaddr + UART_LSR);
1813 } while (status & UART_LSR_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Jiri Slaby0c419422021-06-18 08:14:22 +02001815 return status;
1816}
1817
1818static u8 mxser_receive_chars(struct tty_struct *tty,
1819 struct mxser_port *port, u8 status)
1820{
Jiri Slaby0c419422021-06-18 08:14:22 +02001821 if (tty->receive_room == 0 && !port->ldisc_stop_rx)
1822 mxser_stoprx(tty);
1823
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001824 if (!mxser_receive_chars_new(tty, port, status))
1825 status = mxser_receive_chars_old(tty, port, status);
Jiri Slaby0c419422021-06-18 08:14:22 +02001826
Jiri Slaby2e124b42013-01-03 15:53:06 +01001827 tty_flip_buffer_push(&port->port);
Jiri Slaby15517802021-06-18 08:14:18 +02001828
1829 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
Alan Cox216ba022008-10-13 10:40:19 +01001832static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 int count, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Jiri Slaby1c456072008-02-07 00:16:46 -08001836 if (port->x_char) {
1837 outb(port->x_char, port->ioaddr + UART_TX);
1838 port->x_char = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001839 port->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return;
1841 }
1842
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001843 if (port->port.xmit_buf == NULL)
Jiri Slaby1c456072008-02-07 00:16:46 -08001844 return;
1845
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001846 if (port->xmit_cnt <= 0 || tty->flow.stopped ||
Alan Cox216ba022008-10-13 10:40:19 +01001847 (tty->hw_stopped &&
Jiri Slaby1c456072008-02-07 00:16:46 -08001848 (port->type != PORT_16550A) &&
Jiri Slaby292955a2021-06-18 08:14:13 +02001849 (!port->board->must_hwid))) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001850 port->IER &= ~UART_IER_THRI;
1851 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return;
1853 }
1854
Jiri Slaby1c456072008-02-07 00:16:46 -08001855 cnt = port->xmit_cnt;
1856 count = port->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 do {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001858 outb(port->port.xmit_buf[port->xmit_tail++],
Jiri Slaby1c456072008-02-07 00:16:46 -08001859 port->ioaddr + UART_TX);
1860 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1861 if (--port->xmit_cnt <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 break;
1863 } while (--count > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Jiri Slaby1c456072008-02-07 00:16:46 -08001865 port->icount.tx += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Kulikov Vasiliy464eb8f2010-07-23 20:34:53 +04001867 if (port->xmit_cnt < WAKEUP_CHARS)
Alan Cox216ba022008-10-13 10:40:19 +01001868 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Jiri Slaby1c456072008-02-07 00:16:46 -08001870 if (port->xmit_cnt <= 0) {
1871 port->IER &= ~UART_IER_THRI;
1872 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
1875
Jiri Slaby9e40ea12021-06-18 08:14:19 +02001876static bool mxser_port_isr(struct mxser_port *port)
1877{
1878 struct tty_struct *tty;
1879 u8 iir, msr, status;
1880 bool error = false;
1881
1882 iir = inb(port->ioaddr + UART_IIR);
1883 if (iir & UART_IIR_NO_INT)
1884 return true;
1885
1886 iir &= MOXA_MUST_IIR_MASK;
1887 tty = tty_port_tty_get(&port->port);
1888 if (!tty || port->closing || !tty_port_initialized(&port->port)) {
1889 status = inb(port->ioaddr + UART_LSR);
1890 outb(0x27, port->ioaddr + UART_FCR);
1891 inb(port->ioaddr + UART_MSR);
1892
1893 error = true;
1894 goto put_tty;
1895 }
1896
1897 status = inb(port->ioaddr + UART_LSR);
1898
Jiri Slaby9e40ea12021-06-18 08:14:19 +02001899 if (port->board->must_hwid) {
1900 if (iir == MOXA_MUST_IIR_GDA ||
1901 iir == MOXA_MUST_IIR_RDA ||
1902 iir == MOXA_MUST_IIR_RTO ||
1903 iir == MOXA_MUST_IIR_LSR)
1904 status = mxser_receive_chars(tty, port, status);
1905 } else {
1906 status &= port->read_status_mask;
1907 if (status & UART_LSR_DR)
1908 status = mxser_receive_chars(tty, port, status);
1909 }
1910
1911 msr = inb(port->ioaddr + UART_MSR);
1912 if (msr & UART_MSR_ANY_DELTA)
1913 mxser_check_modem_status(tty, port, msr);
1914
1915 if (port->board->must_hwid) {
1916 if (iir == 0x02 && (status & UART_LSR_THRE))
1917 mxser_transmit_chars(tty, port);
1918 } else {
1919 if (status & UART_LSR_THRE)
1920 mxser_transmit_chars(tty, port);
1921 }
1922
1923put_tty:
1924 tty_kref_put(tty);
1925
1926 return error;
1927}
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929/*
Jiri Slaby1c456072008-02-07 00:16:46 -08001930 * This is the serial driver's generic interrupt routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001932static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Jiri Slabycef222c2021-06-18 08:14:17 +02001934 struct mxser_board *brd = dev_id;
Jiri Slaby1c456072008-02-07 00:16:46 -08001935 struct mxser_port *port;
Jiri Slaby1c456072008-02-07 00:16:46 -08001936 unsigned int int_cnt, pass_counter = 0;
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001937 unsigned int i, max = brd->info->nports;
Jiri Slaby1c456072008-02-07 00:16:46 -08001938 int handled = IRQ_NONE;
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001939 u8 irqbits, bits, mask = BIT(max) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Jiri Slaby1c456072008-02-07 00:16:46 -08001941 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001942 irqbits = inb(brd->vector) & mask;
1943 if (irqbits == mask)
Jiri Slaby1c456072008-02-07 00:16:46 -08001944 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Jiri Slaby1c456072008-02-07 00:16:46 -08001946 handled = IRQ_HANDLED;
1947 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001948 if (irqbits == mask)
Jiri Slaby1c456072008-02-07 00:16:46 -08001949 break;
1950 if (bits & irqbits)
1951 continue;
1952 port = &brd->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Jiri Slaby1c456072008-02-07 00:16:46 -08001954 int_cnt = 0;
1955 spin_lock(&port->slock);
1956 do {
Jiri Slaby9e40ea12021-06-18 08:14:19 +02001957 if (mxser_port_isr(port))
Jiri Slaby1c456072008-02-07 00:16:46 -08001958 break;
Jiri Slaby1c456072008-02-07 00:16:46 -08001959 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
1960 spin_unlock(&port->slock);
1961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
1963
Jiri Slaby1c456072008-02-07 00:16:46 -08001964 return handled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
Jiri Slaby1c456072008-02-07 00:16:46 -08001967static const struct tty_operations mxser_ops = {
1968 .open = mxser_open,
1969 .close = mxser_close,
1970 .write = mxser_write,
1971 .put_char = mxser_put_char,
1972 .flush_chars = mxser_flush_chars,
1973 .write_room = mxser_write_room,
1974 .chars_in_buffer = mxser_chars_in_buffer,
1975 .flush_buffer = mxser_flush_buffer,
1976 .ioctl = mxser_ioctl,
1977 .throttle = mxser_throttle,
1978 .unthrottle = mxser_unthrottle,
1979 .set_termios = mxser_set_termios,
1980 .stop = mxser_stop,
1981 .start = mxser_start,
1982 .hangup = mxser_hangup,
1983 .break_ctl = mxser_rs_break,
1984 .wait_until_sent = mxser_wait_until_sent,
1985 .tiocmget = mxser_tiocmget,
1986 .tiocmset = mxser_tiocmset,
Al Viro6da5b582018-09-11 22:22:06 -04001987 .set_serial = mxser_set_serial_info,
1988 .get_serial = mxser_get_serial_info,
Alan Cox05871022010-09-16 18:21:52 +01001989 .get_icount = mxser_get_icount,
Jiri Slaby1c456072008-02-07 00:16:46 -08001990};
1991
Aya Mahfouz04b757d2015-12-15 01:53:36 +02001992static const struct tty_port_operations mxser_port_ops = {
Alan Cox31f35932009-01-02 13:45:05 +00001993 .carrier_raised = mxser_carrier_raised,
Alan Coxfcc8ac12009-06-11 12:24:17 +01001994 .dtr_rts = mxser_dtr_rts,
Alan Cox67691402009-11-30 13:17:35 +00001995 .activate = mxser_activate,
1996 .shutdown = mxser_shutdown_port,
Alan Cox31f35932009-01-02 13:45:05 +00001997};
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002000 * The MOXA Smartio/Industio serial driver boot-time initialization code!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002002
Jiri Slaby27997072017-09-12 12:39:55 +02002003static int mxser_initbrd(struct mxser_board *brd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
Jiri Slaby1c456072008-02-07 00:16:46 -08002005 struct mxser_port *info;
2006 unsigned int i;
2007 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Jiri Slaby1c456072008-02-07 00:16:46 -08002009 for (i = 0; i < brd->info->nports; i++) {
2010 info = &brd->ports[i];
Alan Cox44b7d1b2008-07-16 21:57:18 +01002011 tty_port_init(&info->port);
Alan Cox31f35932009-01-02 13:45:05 +00002012 info->port.ops = &mxser_port_ops;
Jiri Slaby1c456072008-02-07 00:16:46 -08002013 info->board = brd;
2014 info->stop_rx = 0;
2015 info->ldisc_stop_rx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Jiri Slaby1c456072008-02-07 00:16:46 -08002017 /* Enhance mode enabled here */
Jiri Slaby292955a2021-06-18 08:14:13 +02002018 if (brd->must_hwid != MOXA_OTHER_UART)
Christoph Hellwig148ff862008-04-30 00:54:29 -07002019 mxser_enable_must_enchance_mode(info->ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Jiri Slaby58a2ddb2021-06-18 08:14:15 +02002021 info->type = PORT_16550A;
Jiri Slaby1c456072008-02-07 00:16:46 -08002022
2023 process_txrx_fifo(info);
2024
2025 info->custom_divisor = info->baud_base * 16;
Alan Cox44b7d1b2008-07-16 21:57:18 +01002026 info->port.close_delay = 5 * HZ / 10;
2027 info->port.closing_wait = 30 * HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002028 info->normal_termios = mxvar_sdriver->init_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -08002029 spin_lock_init(&info->slock);
2030
2031 /* before set INT ISR, disable all int */
2032 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2033 info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035
Jiri Slaby1c456072008-02-07 00:16:46 -08002036 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2037 brd);
Jiri Slaby191c5f12012-11-15 09:49:56 +01002038 if (retval) {
2039 for (i = 0; i < brd->info->nports; i++)
2040 tty_port_destroy(&brd->ports[i].port);
Jiri Slaby1c456072008-02-07 00:16:46 -08002041 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2042 "conflict with another device.\n",
2043 brd->info->name, brd->irq);
Jiri Slaby191c5f12012-11-15 09:49:56 +01002044 }
Jiri Slabydf480512010-09-03 10:31:37 +02002045
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002046 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
Jiri Slaby191c5f12012-11-15 09:49:56 +01002049static void mxser_board_remove(struct mxser_board *brd)
2050{
2051 unsigned int i;
2052
2053 for (i = 0; i < brd->info->nports; i++) {
2054 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2055 tty_port_destroy(&brd->ports[i].port);
2056 }
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002057 free_irq(brd->irq, brd);
Jiri Slaby191c5f12012-11-15 09:49:56 +01002058}
2059
Bill Pemberton9671f092012-11-19 13:21:50 -05002060static int mxser_probe(struct pci_dev *pdev,
Jiri Slaby1c456072008-02-07 00:16:46 -08002061 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Jiri Slaby1c456072008-02-07 00:16:46 -08002063 struct mxser_board *brd;
2064 unsigned int i, j;
2065 unsigned long ioaddress;
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002066 struct device *tty_dev;
Jiri Slaby1c456072008-02-07 00:16:46 -08002067 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Jiri Slaby1c456072008-02-07 00:16:46 -08002069 for (i = 0; i < MXSER_BOARDS; i++)
2070 if (mxser_boards[i].info == NULL)
2071 break;
2072
2073 if (i >= MXSER_BOARDS) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002074 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
2075 "not configured\n", MXSER_BOARDS);
Jiri Slaby1c456072008-02-07 00:16:46 -08002076 goto err;
2077 }
2078
2079 brd = &mxser_boards[i];
2080 brd->idx = i * MXSER_PORTS_PER_BOARD;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002081 dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
Jiri Slaby1c456072008-02-07 00:16:46 -08002082 mxser_cards[ent->driver_data].name,
2083 pdev->bus->number, PCI_SLOT(pdev->devfn));
2084
2085 retval = pci_enable_device(pdev);
2086 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002087 dev_err(&pdev->dev, "PCI enable failed\n");
Jiri Slaby1c456072008-02-07 00:16:46 -08002088 goto err;
2089 }
2090
2091 /* io address */
2092 ioaddress = pci_resource_start(pdev, 2);
2093 retval = pci_request_region(pdev, 2, "mxser(IO)");
2094 if (retval)
Jiri Slabydf480512010-09-03 10:31:37 +02002095 goto err_dis;
Jiri Slaby1c456072008-02-07 00:16:46 -08002096
2097 brd->info = &mxser_cards[ent->driver_data];
2098 for (i = 0; i < brd->info->nports; i++)
2099 brd->ports[i].ioaddr = ioaddress + 8 * i;
2100
2101 /* vector */
2102 ioaddress = pci_resource_start(pdev, 3);
2103 retval = pci_request_region(pdev, 3, "mxser(vector)");
2104 if (retval)
Jiri Slabydf480512010-09-03 10:31:37 +02002105 goto err_zero;
Jiri Slaby1c456072008-02-07 00:16:46 -08002106 brd->vector = ioaddress;
2107
2108 /* irq */
2109 brd->irq = pdev->irq;
2110
Jiri Slaby292955a2021-06-18 08:14:13 +02002111 brd->must_hwid = mxser_must_get_hwid(brd->ports[0].ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -08002112
Jiri Slaby928f9462021-06-18 08:14:16 +02002113 for (j = 0; j < UART_INFO_NUM; j++) {
2114 if (Gpci_uart_info[j].type == brd->must_hwid) {
2115 brd->max_baud = Gpci_uart_info[j].max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -08002116
Jiri Slaby928f9462021-06-18 08:14:16 +02002117 /* exception....CP-102 */
2118 if (brd->info->flags & MXSER_HIGHBAUD)
2119 brd->max_baud = 921600;
2120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002122 }
2123
Jiri Slaby292955a2021-06-18 08:14:13 +02002124 if (brd->must_hwid == MOXA_MUST_MU860_HWID) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002125 for (i = 0; i < brd->info->nports; i++) {
2126 if (i < 4)
2127 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2128 else
2129 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002131 outb(0, ioaddress + 4); /* default set to RS232 mode */
2132 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002134
2135 for (i = 0; i < brd->info->nports; i++) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002136 brd->ports[i].baud_base = 921600;
2137 }
2138
2139 /* mxser_initbrd will hook ISR. */
Jiri Slaby27997072017-09-12 12:39:55 +02002140 retval = mxser_initbrd(brd);
Jiri Slaby1c456072008-02-07 00:16:46 -08002141 if (retval)
Jiri Slabydf480512010-09-03 10:31:37 +02002142 goto err_rel3;
Jiri Slaby1c456072008-02-07 00:16:46 -08002143
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002144 for (i = 0; i < brd->info->nports; i++) {
2145 tty_dev = tty_port_register_device(&brd->ports[i].port,
2146 mxvar_sdriver, brd->idx + i, &pdev->dev);
2147 if (IS_ERR(tty_dev)) {
2148 retval = PTR_ERR(tty_dev);
Alexey Khoroshilov1b581f12013-04-07 23:46:47 +04002149 for (; i > 0; i--)
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002150 tty_unregister_device(mxvar_sdriver,
Alexey Khoroshilov1b581f12013-04-07 23:46:47 +04002151 brd->idx + i - 1);
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002152 goto err_relbrd;
2153 }
2154 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002155
2156 pci_set_drvdata(pdev, brd);
2157
2158 return 0;
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002159err_relbrd:
2160 for (i = 0; i < brd->info->nports; i++)
2161 tty_port_destroy(&brd->ports[i].port);
2162 free_irq(brd->irq, brd);
Jiri Slabydf480512010-09-03 10:31:37 +02002163err_rel3:
2164 pci_release_region(pdev, 3);
2165err_zero:
Jiri Slaby1c456072008-02-07 00:16:46 -08002166 brd->info = NULL;
Jiri Slabydf480512010-09-03 10:31:37 +02002167 pci_release_region(pdev, 2);
2168err_dis:
2169 pci_disable_device(pdev);
Jiri Slaby1c456072008-02-07 00:16:46 -08002170err:
2171 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
Bill Pembertonae8d8a12012-11-19 13:26:18 -05002174static void mxser_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Jiri Slaby1c456072008-02-07 00:16:46 -08002176 struct mxser_board *brd = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Jiri Slaby191c5f12012-11-15 09:49:56 +01002178 mxser_board_remove(brd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Jiri Slabydf480512010-09-03 10:31:37 +02002180 pci_release_region(pdev, 2);
2181 pci_release_region(pdev, 3);
2182 pci_disable_device(pdev);
Jiri Slaby1c456072008-02-07 00:16:46 -08002183 brd->info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
Jiri Slaby1c456072008-02-07 00:16:46 -08002186static struct pci_driver mxser_driver = {
2187 .name = "mxser",
2188 .id_table = mxser_pcibrds,
2189 .probe = mxser_probe,
Bill Pemberton91116cb2012-11-19 13:21:06 -05002190 .remove = mxser_remove
Jiri Slaby1c456072008-02-07 00:16:46 -08002191};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Jiri Slaby1c456072008-02-07 00:16:46 -08002193static int __init mxser_module_init(void)
2194{
Jiri Slaby1df00922008-07-25 01:48:22 -07002195 int retval;
Jiri Slaby1c456072008-02-07 00:16:46 -08002196
Jiri Slaby389fc822021-06-18 08:14:38 +02002197 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS);
Jiri Slaby1c456072008-02-07 00:16:46 -08002198 if (!mxvar_sdriver)
2199 return -ENOMEM;
2200
Jiri Slaby1c456072008-02-07 00:16:46 -08002201 /* Initialize the tty_driver structure */
Jiri Slaby1c456072008-02-07 00:16:46 -08002202 mxvar_sdriver->name = "ttyMI";
2203 mxvar_sdriver->major = ttymajor;
2204 mxvar_sdriver->minor_start = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002205 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2206 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2207 mxvar_sdriver->init_termios = tty_std_termios;
2208 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2209 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2210 tty_set_operations(mxvar_sdriver, &mxser_ops);
2211
2212 retval = tty_register_driver(mxvar_sdriver);
2213 if (retval) {
2214 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2215 "tty driver !\n");
2216 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002218
Jiri Slaby1c456072008-02-07 00:16:46 -08002219 retval = pci_register_driver(&mxser_driver);
2220 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002221 printk(KERN_ERR "mxser: can't register pci driver\n");
Jiri Slaby29134362021-06-18 08:14:07 +02002222 goto err_unr;
Jiri Slaby1c456072008-02-07 00:16:46 -08002223 }
2224
Jiri Slaby1c456072008-02-07 00:16:46 -08002225 return 0;
2226err_unr:
2227 tty_unregister_driver(mxvar_sdriver);
2228err_put:
2229 put_tty_driver(mxvar_sdriver);
2230 return retval;
2231}
2232
2233static void __exit mxser_module_exit(void)
2234{
Jiri Slaby1c456072008-02-07 00:16:46 -08002235 pci_unregister_driver(&mxser_driver);
Jiri Slaby1c456072008-02-07 00:16:46 -08002236 tty_unregister_driver(mxvar_sdriver);
2237 put_tty_driver(mxvar_sdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
2240module_init(mxser_module_init);
2241module_exit(mxser_module_exit);