blob: a321aba24d588a56fe828271beffd9be375a3df9 [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
53#define MOXA_GETDATACOUNT (MOXA + 23)
54#define MOXA_DIAGNOSE (MOXA + 50)
55#define MOXA_CHKPORTENABLE (MOXA + 60)
56#define MOXA_HighSpeedOn (MOXA + 61)
57#define MOXA_GET_MAJOR (MOXA + 63)
58#define MOXA_GETMSTATUS (MOXA + 65)
59#define MOXA_SET_OP_MODE (MOXA + 66)
60#define MOXA_GET_OP_MODE (MOXA + 67)
61
62#define RS232_MODE 0
63#define RS485_2WIRE_MODE 1
64#define RS422_MODE 2
65#define RS485_4WIRE_MODE 3
66#define OP_MODE_MASK 3
67
68#define MOXA_SDS_RSTICOUNTER (MOXA + 69)
69#define MOXA_ASPP_OQUEUE (MOXA + 70)
70#define MOXA_ASPP_MON (MOXA + 73)
71#define MOXA_ASPP_LSTATUS (MOXA + 74)
72#define MOXA_ASPP_MON_EXT (MOXA + 75)
73#define MOXA_SET_BAUD_METHOD (MOXA + 76)
74
75/* --------------------------------------------------- */
76
77#define NPPI_NOTIFY_PARITY 0x01
78#define NPPI_NOTIFY_FRAMING 0x02
79#define NPPI_NOTIFY_HW_OVERRUN 0x04
80#define NPPI_NOTIFY_SW_OVERRUN 0x08
81#define NPPI_NOTIFY_BREAK 0x10
82
83#define NPPI_NOTIFY_CTSHOLD 0x01 /* Tx hold by CTS low */
84#define NPPI_NOTIFY_DSRHOLD 0x02 /* Tx hold by DSR low */
85#define NPPI_NOTIFY_XOFFHOLD 0x08 /* Tx hold by Xoff received */
86#define NPPI_NOTIFY_XOFFXENT 0x10 /* Xoff Sent */
87
88/*
89 * Follow just what Moxa Must chip defines.
90 *
91 * When LCR register (offset 0x03) writes the following value, the Must chip
92 * will enter enchance mode. And write value on EFR (offset 0x02) bit 6,7 to
93 * change bank.
94 */
95#define MOXA_MUST_ENTER_ENCHANCE 0xBF
96
97/* when enhance mode enabled, access on general bank register */
98#define MOXA_MUST_GDL_REGISTER 0x07
99#define MOXA_MUST_GDL_MASK 0x7F
100#define MOXA_MUST_GDL_HAS_BAD_DATA 0x80
101
102#define MOXA_MUST_LSR_RERR 0x80 /* error in receive FIFO */
103/* enchance register bank select and enchance mode setting register */
104/* when LCR register equals to 0xBF */
105#define MOXA_MUST_EFR_REGISTER 0x02
106#define MOXA_MUST_EFR_EFRB_ENABLE 0x10 /* enchance mode enable */
107/* enchance register bank set 0, 1, 2 */
108#define MOXA_MUST_EFR_BANK0 0x00
109#define MOXA_MUST_EFR_BANK1 0x40
110#define MOXA_MUST_EFR_BANK2 0x80
111#define MOXA_MUST_EFR_BANK3 0xC0
112#define MOXA_MUST_EFR_BANK_MASK 0xC0
113
114/* set XON1 value register, when LCR=0xBF and change to bank0 */
115#define MOXA_MUST_XON1_REGISTER 0x04
116
117/* set XON2 value register, when LCR=0xBF and change to bank0 */
118#define MOXA_MUST_XON2_REGISTER 0x05
119
120/* set XOFF1 value register, when LCR=0xBF and change to bank0 */
121#define MOXA_MUST_XOFF1_REGISTER 0x06
122
123/* set XOFF2 value register, when LCR=0xBF and change to bank0 */
124#define MOXA_MUST_XOFF2_REGISTER 0x07
125
126#define MOXA_MUST_RBRTL_REGISTER 0x04
127#define MOXA_MUST_RBRTH_REGISTER 0x05
128#define MOXA_MUST_RBRTI_REGISTER 0x06
129#define MOXA_MUST_THRTL_REGISTER 0x07
130#define MOXA_MUST_ENUM_REGISTER 0x04
131#define MOXA_MUST_HWID_REGISTER 0x05
132#define MOXA_MUST_ECR_REGISTER 0x06
133#define MOXA_MUST_CSR_REGISTER 0x07
134
135#define MOXA_MUST_FCR_GDA_MODE_ENABLE 0x20 /* good data mode enable */
136#define MOXA_MUST_FCR_GDA_ONLY_ENABLE 0x10 /* only good data put into RxFIFO */
137
138#define MOXA_MUST_IER_ECTSI 0x80 /* enable CTS interrupt */
139#define MOXA_MUST_IER_ERTSI 0x40 /* enable RTS interrupt */
140#define MOXA_MUST_IER_XINT 0x20 /* enable Xon/Xoff interrupt */
141#define MOXA_MUST_IER_EGDAI 0x10 /* enable GDA interrupt */
142
143#define MOXA_MUST_RECV_ISR (UART_IER_RDI | MOXA_MUST_IER_EGDAI)
144
145/* GDA interrupt pending */
146#define MOXA_MUST_IIR_GDA 0x1C
147#define MOXA_MUST_IIR_RDA 0x04
148#define MOXA_MUST_IIR_RTO 0x0C
149#define MOXA_MUST_IIR_LSR 0x06
150
151/* received Xon/Xoff or specical interrupt pending */
152#define MOXA_MUST_IIR_XSC 0x10
153
154/* RTS/CTS change state interrupt pending */
155#define MOXA_MUST_IIR_RTSCTS 0x20
156#define MOXA_MUST_IIR_MASK 0x3E
157
158#define MOXA_MUST_MCR_XON_FLAG 0x40
159#define MOXA_MUST_MCR_XON_ANY 0x80
160#define MOXA_MUST_MCR_TX_XON 0x08
161
162#define MOXA_MUST_EFR_SF_MASK 0x0F /* software flow control on chip mask value */
163#define MOXA_MUST_EFR_SF_TX1 0x08 /* send Xon1/Xoff1 */
164#define MOXA_MUST_EFR_SF_TX2 0x04 /* send Xon2/Xoff2 */
165#define MOXA_MUST_EFR_SF_TX12 0x0C /* send Xon1,Xon2/Xoff1,Xoff2 */
166#define MOXA_MUST_EFR_SF_TX_NO 0x00 /* don't send Xon/Xoff */
167#define MOXA_MUST_EFR_SF_TX_MASK 0x0C /* Tx software flow control mask */
168#define MOXA_MUST_EFR_SF_RX_NO 0x00 /* don't receive Xon/Xoff */
169#define MOXA_MUST_EFR_SF_RX1 0x02 /* receive Xon1/Xoff1 */
170#define MOXA_MUST_EFR_SF_RX2 0x01 /* receive Xon2/Xoff2 */
171#define MOXA_MUST_EFR_SF_RX12 0x03 /* receive Xon1,Xon2/Xoff1,Xoff2 */
172#define MOXA_MUST_EFR_SF_RX_MASK 0x03 /* Rx software flow control mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#define MXSERMAJOR 174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#define MXSER_BOARDS 4 /* Max. boards */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slaby1c456072008-02-07 00:16:46 -0800178#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
179#define MXSER_ISR_PASS_LIMIT 100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#define WAKEUP_CHARS 256
182
183#define UART_MCR_AFE 0x20
184#define UART_LSR_SPECIAL 0x1E
185
Jiri Slabye129def2008-07-22 11:20:34 +0100186#define PCI_DEVICE_ID_POS104UL 0x1044
Jiri Slaby1c456072008-02-07 00:16:46 -0800187#define PCI_DEVICE_ID_CB108 0x1080
Jiri Slabye129def2008-07-22 11:20:34 +0100188#define PCI_DEVICE_ID_CP102UF 0x1023
Jiri Slaby502f2952009-09-10 12:20:07 +0200189#define PCI_DEVICE_ID_CP112UL 0x1120
Jiri Slaby1c456072008-02-07 00:16:46 -0800190#define PCI_DEVICE_ID_CB114 0x1142
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800191#define PCI_DEVICE_ID_CP114UL 0x1143
Jiri Slaby1c456072008-02-07 00:16:46 -0800192#define PCI_DEVICE_ID_CB134I 0x1341
193#define PCI_DEVICE_ID_CP138U 0x1380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Jiri Slaby1c456072008-02-07 00:16:46 -0800195#define MXSER_HIGHBAUD 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Jiri Slabye4558362021-06-18 08:14:14 +0200197enum mxser_must_hwid {
198 MOXA_OTHER_UART = 0x00,
199 MOXA_MUST_MU150_HWID = 0x01,
200 MOXA_MUST_MU860_HWID = 0x02,
201};
202
Jiri Slaby1c456072008-02-07 00:16:46 -0800203static const struct {
Jiri Slabydc33f642021-06-18 08:14:11 +0200204 u8 type;
205 u8 fifo_size;
206 u8 rx_high_water;
207 u8 rx_low_water;
208 speed_t max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800209} Gpci_uart_info[] = {
Jiri Slabydc33f642021-06-18 08:14:11 +0200210 { MOXA_OTHER_UART, 16, 14, 1, 921600 },
211 { MOXA_MUST_MU150_HWID, 64, 48, 16, 230400 },
212 { MOXA_MUST_MU860_HWID, 128, 96, 32, 921600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
Jiri Slaby1c456072008-02-07 00:16:46 -0800214#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Jiri Slaby1c456072008-02-07 00:16:46 -0800216struct mxser_cardinfo {
217 char *name;
218 unsigned int nports;
219 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221
Jiri Slaby1c456072008-02-07 00:16:46 -0800222static const struct mxser_cardinfo mxser_cards[] = {
Jiri Slaby15254902021-06-18 08:14:08 +0200223/* 0*/ { "C168H/PCI series", 8, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800224 { "C104H/PCI series", 4, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800225 { "CP-132 series", 2, },
226 { "CP-114 series", 4, },
Jiri Slaby15254902021-06-18 08:14:08 +0200227 { "CT-114 series", 4, },
228/* 5*/ { "CP-102 series", 2, MXSER_HIGHBAUD },
Jiri Slaby1c456072008-02-07 00:16:46 -0800229 { "CP-104U series", 4, },
230 { "CP-168U series", 8, },
231 { "CP-132U series", 2, },
Jiri Slaby15254902021-06-18 08:14:08 +0200232 { "CP-134U series", 4, },
233/*10*/ { "CP-104JU series", 4, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800234 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
235 { "CP-118U series", 8, },
236 { "CP-102UL series", 2, },
Jiri Slaby15254902021-06-18 08:14:08 +0200237 { "CP-102U series", 2, },
238/*15*/ { "CP-118EL series", 8, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800239 { "CP-168EL series", 8, },
240 { "CP-104EL series", 4, },
241 { "CB-108 series", 8, },
Jiri Slaby15254902021-06-18 08:14:08 +0200242 { "CB-114 series", 4, },
243/*20*/ { "CB-134I series", 4, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800244 { "CP-138U series", 8, },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800245 { "POS-104UL series", 4, },
Jiri Slabye129def2008-07-22 11:20:34 +0100246 { "CP-114UL series", 4, },
Jiri Slaby15254902021-06-18 08:14:08 +0200247 { "CP-102UF series", 2, },
248/*25*/ { "CP-112UL series", 2, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800249};
250
251/* driver_data correspond to the lines in the structure above
252 see also ISA probe function before you change something */
Arvind Yadav3385ecf2017-07-23 15:31:01 +0530253static const struct pci_device_id mxser_pcibrds[] = {
Jiri Slaby15254902021-06-18 08:14:08 +0200254 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 0 },
255 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 1 },
256 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 2 },
257 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 3 },
258 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 4 },
259 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 5 },
260 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 6 },
261 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 7 },
262 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 8 },
263 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 9 },
264 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 10 },
265 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 11 },
266 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 12 },
267 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 13 },
268 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 14 },
269 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 15 },
270 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 16 },
271 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 17 },
272 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 18 },
273 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 19 },
274 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 20 },
275 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 21 },
276 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 22 },
277 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 23 },
278 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 24 },
279 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP112UL), .driver_data = 25 },
Jiri Slaby1c456072008-02-07 00:16:46 -0800280 { }
281};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static int ttymajor = MXSERMAJOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/* Variables for insmod */
287
288MODULE_AUTHOR("Casper Yang");
289MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800290module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291MODULE_LICENSE("GPL");
292
293struct mxser_log {
294 int tick;
295 unsigned long rxcnt[MXSER_PORTS];
296 unsigned long txcnt[MXSER_PORTS];
297};
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299struct mxser_mon {
300 unsigned long rxcnt;
301 unsigned long txcnt;
302 unsigned long up_rxcnt;
303 unsigned long up_txcnt;
304 int modem_status;
305 unsigned char hold_reason;
306};
307
308struct mxser_mon_ext {
309 unsigned long rx_cnt[32];
310 unsigned long tx_cnt[32];
311 unsigned long up_rxcnt[32];
312 unsigned long up_txcnt[32];
313 int modem_status[32];
314
315 long baudrate[32];
316 int databits[32];
317 int stopbits[32];
318 int parity[32];
319 int flowctrl[32];
320 int fifo[32];
321 int iftype[32];
322};
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700323
Jiri Slaby1c456072008-02-07 00:16:46 -0800324struct mxser_board;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Jiri Slaby1c456072008-02-07 00:16:46 -0800326struct mxser_port {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100327 struct tty_port port;
Jiri Slaby1c456072008-02-07 00:16:46 -0800328 struct mxser_board *board;
Jiri Slaby1c456072008-02-07 00:16:46 -0800329
330 unsigned long ioaddr;
331 unsigned long opmode_ioaddr;
Jiri Slaby1c456072008-02-07 00:16:46 -0800332
Jiri Slabydc33f642021-06-18 08:14:11 +0200333 u8 rx_high_water;
334 u8 rx_low_water;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 int baud_base; /* max. speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int type; /* UART type */
Jiri Slaby1c456072008-02-07 00:16:46 -0800337
338 int x_char; /* xon/xoff character */
339 int IER; /* Interrupt Enable Register */
340 int MCR; /* Modem control register */
341
342 unsigned char stop_rx;
343 unsigned char ldisc_stop_rx;
344
345 int custom_divisor;
Jiri Slaby1c456072008-02-07 00:16:46 -0800346 unsigned char err_shadow;
Jiri Slaby1c456072008-02-07 00:16:46 -0800347
Jiri Slaby1c456072008-02-07 00:16:46 -0800348 struct async_icount icount; /* kernel counters for 4 input interrupts */
Jiri Slaby104583b2017-09-12 12:39:54 +0200349 unsigned int timeout;
Jiri Slaby1c456072008-02-07 00:16:46 -0800350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 int read_status_mask;
352 int ignore_status_mask;
Jiri Slabydc33f642021-06-18 08:14:11 +0200353 u8 xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int xmit_head;
355 int xmit_tail;
356 int xmit_cnt;
Peter Hurleycd7b4b32016-01-10 14:51:38 -0800357 int closing;
Jiri Slaby1c456072008-02-07 00:16:46 -0800358
Alan Cox606d0992006-12-08 02:38:45 -0800359 struct ktermios normal_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -0800360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct mxser_mon mon_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 spinlock_t slock;
Jiri Slaby1c456072008-02-07 00:16:46 -0800364};
365
366struct mxser_board {
367 unsigned int idx;
368 int irq;
369 const struct mxser_cardinfo *info;
370 unsigned long vector;
Jiri Slaby1c456072008-02-07 00:16:46 -0800371
Jiri Slabye4558362021-06-18 08:14:14 +0200372 enum mxser_must_hwid must_hwid;
Jiri Slaby928f9462021-06-18 08:14:16 +0200373 speed_t max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800374
375 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376};
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378struct mxser_mstatus {
379 tcflag_t cflag;
380 int cts;
381 int dsr;
382 int ri;
383 int dcd;
384};
385
Jiri Slaby1c456072008-02-07 00:16:46 -0800386static struct mxser_board mxser_boards[MXSER_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387static struct tty_driver *mxvar_sdriver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388static struct mxser_log mxvar_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389static int mxser_set_baud_method[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Christoph Hellwig148ff862008-04-30 00:54:29 -0700391static void mxser_enable_must_enchance_mode(unsigned long baseio)
392{
393 u8 oldlcr;
394 u8 efr;
395
396 oldlcr = inb(baseio + UART_LCR);
397 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
398
399 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
400 efr |= MOXA_MUST_EFR_EFRB_ENABLE;
401
402 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
403 outb(oldlcr, baseio + UART_LCR);
404}
405
406static void mxser_disable_must_enchance_mode(unsigned long baseio)
407{
408 u8 oldlcr;
409 u8 efr;
410
411 oldlcr = inb(baseio + UART_LCR);
412 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
413
414 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
415 efr &= ~MOXA_MUST_EFR_EFRB_ENABLE;
416
417 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
418 outb(oldlcr, baseio + UART_LCR);
419}
420
421static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
422{
423 u8 oldlcr;
424 u8 efr;
425
426 oldlcr = inb(baseio + UART_LCR);
427 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
428
429 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
430 efr &= ~MOXA_MUST_EFR_BANK_MASK;
431 efr |= MOXA_MUST_EFR_BANK0;
432
433 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
434 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
435 outb(oldlcr, baseio + UART_LCR);
436}
437
438static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
439{
440 u8 oldlcr;
441 u8 efr;
442
443 oldlcr = inb(baseio + UART_LCR);
444 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
445
446 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
447 efr &= ~MOXA_MUST_EFR_BANK_MASK;
448 efr |= MOXA_MUST_EFR_BANK0;
449
450 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
451 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
452 outb(oldlcr, baseio + UART_LCR);
453}
454
455static void mxser_set_must_fifo_value(struct mxser_port *info)
456{
457 u8 oldlcr;
458 u8 efr;
459
460 oldlcr = inb(info->ioaddr + UART_LCR);
461 outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
462
463 efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
464 efr &= ~MOXA_MUST_EFR_BANK_MASK;
465 efr |= MOXA_MUST_EFR_BANK1;
466
467 outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
Jiri Slabydc33f642021-06-18 08:14:11 +0200468 outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
469 outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
470 outb(info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700471 outb(oldlcr, info->ioaddr + UART_LCR);
472}
473
474static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
475{
476 u8 oldlcr;
477 u8 efr;
478
479 oldlcr = inb(baseio + UART_LCR);
480 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
481
482 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
483 efr &= ~MOXA_MUST_EFR_BANK_MASK;
484 efr |= MOXA_MUST_EFR_BANK2;
485
486 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
487 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
488 outb(oldlcr, baseio + UART_LCR);
489}
490
491static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
492{
493 u8 oldlcr;
494 u8 efr;
495
496 oldlcr = inb(baseio + UART_LCR);
497 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
498
499 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
500 efr &= ~MOXA_MUST_EFR_BANK_MASK;
501 efr |= MOXA_MUST_EFR_BANK2;
502
503 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
504 *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
505 outb(oldlcr, baseio + UART_LCR);
506}
507
508static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
509{
510 u8 oldlcr;
511 u8 efr;
512
513 oldlcr = inb(baseio + UART_LCR);
514 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
515
516 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
517 efr &= ~MOXA_MUST_EFR_SF_MASK;
518
519 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
520 outb(oldlcr, baseio + UART_LCR);
521}
522
523static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
524{
525 u8 oldlcr;
526 u8 efr;
527
528 oldlcr = inb(baseio + UART_LCR);
529 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
530
531 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
532 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
533 efr |= MOXA_MUST_EFR_SF_TX1;
534
535 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
536 outb(oldlcr, baseio + UART_LCR);
537}
538
539static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
540{
541 u8 oldlcr;
542 u8 efr;
543
544 oldlcr = inb(baseio + UART_LCR);
545 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
546
547 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
548 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
549
550 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
551 outb(oldlcr, baseio + UART_LCR);
552}
553
554static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
555{
556 u8 oldlcr;
557 u8 efr;
558
559 oldlcr = inb(baseio + UART_LCR);
560 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
561
562 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
563 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
564 efr |= MOXA_MUST_EFR_SF_RX1;
565
566 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
567 outb(oldlcr, baseio + UART_LCR);
568}
569
570static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
571{
572 u8 oldlcr;
573 u8 efr;
574
575 oldlcr = inb(baseio + UART_LCR);
576 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
577
578 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
579 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
580
581 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
582 outb(oldlcr, baseio + UART_LCR);
583}
584
Jiri Slabye4558362021-06-18 08:14:14 +0200585static enum mxser_must_hwid mxser_must_get_hwid(unsigned long io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 u8 oldmcr, hwid;
588 int i;
589
590 outb(0, io + UART_LCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700591 mxser_disable_must_enchance_mode(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 oldmcr = inb(io + UART_MCR);
593 outb(0, io + UART_MCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700594 mxser_set_must_xon1_value(io, 0x11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if ((hwid = inb(io + UART_MCR)) != 0) {
596 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700597 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
Christoph Hellwig148ff862008-04-30 00:54:29 -0700600 mxser_get_must_hardware_id(io, &hwid);
Jiri Slabye4558362021-06-18 08:14:14 +0200601 for (i = 1; i < UART_INFO_NUM; i++) /* 0 = OTHER_UART */
Jiri Slaby1c456072008-02-07 00:16:46 -0800602 if (hwid == Gpci_uart_info[i].type)
Jiri Slabye4558362021-06-18 08:14:14 +0200603 return hwid;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return MOXA_OTHER_UART;
606}
607
Jiri Slaby1c456072008-02-07 00:16:46 -0800608static void process_txrx_fifo(struct mxser_port *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 int i;
611
612 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 info->rx_high_water = 1;
614 info->rx_low_water = 1;
615 info->xmit_fifo_size = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800616 } else
617 for (i = 0; i < UART_INFO_NUM; i++)
Jiri Slaby292955a2021-06-18 08:14:13 +0200618 if (info->board->must_hwid == Gpci_uart_info[i].type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
620 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
Jiri Slabydc33f642021-06-18 08:14:11 +0200621 info->xmit_fifo_size = Gpci_uart_info[i].fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 break;
623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Jiri Slaby1c456072008-02-07 00:16:46 -0800626static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Jiri Slaby72800df2008-07-25 01:48:20 -0700628 static unsigned char mxser_msr[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 unsigned char status = 0;
630
631 status = inb(baseaddr + UART_MSR);
632
633 mxser_msr[port] &= 0x0F;
634 mxser_msr[port] |= status;
635 status = mxser_msr[port];
636 if (mode)
637 mxser_msr[port] = 0;
638
639 return status;
640}
641
Alan Cox31f35932009-01-02 13:45:05 +0000642static int mxser_carrier_raised(struct tty_port *port)
643{
644 struct mxser_port *mp = container_of(port, struct mxser_port, port);
645 return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
646}
647
Alan Coxfcc8ac12009-06-11 12:24:17 +0100648static void mxser_dtr_rts(struct tty_port *port, int on)
Alan Cox5d951fb2009-01-02 13:45:19 +0000649{
650 struct mxser_port *mp = container_of(port, struct mxser_port, port);
651 unsigned long flags;
652
653 spin_lock_irqsave(&mp->slock, flags);
Alan Coxfcc8ac12009-06-11 12:24:17 +0100654 if (on)
655 outb(inb(mp->ioaddr + UART_MCR) |
656 UART_MCR_DTR | UART_MCR_RTS, mp->ioaddr + UART_MCR);
657 else
658 outb(inb(mp->ioaddr + UART_MCR)&~(UART_MCR_DTR | UART_MCR_RTS),
659 mp->ioaddr + UART_MCR);
Alan Cox5d951fb2009-01-02 13:45:19 +0000660 spin_unlock_irqrestore(&mp->slock, flags);
661}
662
Jiri Slabydc33f642021-06-18 08:14:11 +0200663static int mxser_set_baud(struct tty_struct *tty, speed_t newspd)
Jiri Slaby1c456072008-02-07 00:16:46 -0800664{
Alan Cox216ba022008-10-13 10:40:19 +0100665 struct mxser_port *info = tty->driver_data;
Jiri Slaby104583b2017-09-12 12:39:54 +0200666 unsigned int quot = 0, baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800667 unsigned char cval;
Jiri Slaby104583b2017-09-12 12:39:54 +0200668 u64 timeout;
Jiri Slaby1c456072008-02-07 00:16:46 -0800669
Alan Cox216ba022008-10-13 10:40:19 +0100670 if (!info->ioaddr)
Jiri Slaby1c456072008-02-07 00:16:46 -0800671 return -1;
672
Jiri Slaby928f9462021-06-18 08:14:16 +0200673 if (newspd > info->board->max_baud)
Jiri Slaby1c456072008-02-07 00:16:46 -0800674 return -1;
675
676 if (newspd == 134) {
677 quot = 2 * info->baud_base / 269;
Alan Cox216ba022008-10-13 10:40:19 +0100678 tty_encode_baud_rate(tty, 134, 134);
Jiri Slaby1c456072008-02-07 00:16:46 -0800679 } else if (newspd) {
680 quot = info->baud_base / newspd;
681 if (quot == 0)
682 quot = 1;
683 baud = info->baud_base/quot;
Alan Cox216ba022008-10-13 10:40:19 +0100684 tty_encode_baud_rate(tty, baud, baud);
Jiri Slaby1c456072008-02-07 00:16:46 -0800685 } else {
686 quot = 0;
687 }
688
Jiri Slaby104583b2017-09-12 12:39:54 +0200689 /*
690 * worst case (128 * 1000 * 10 * 18432) needs 35 bits, so divide in the
691 * u64 domain
692 */
693 timeout = (u64)info->xmit_fifo_size * HZ * 10 * quot;
694 do_div(timeout, info->baud_base);
695 info->timeout = timeout + HZ / 50; /* Add .02 seconds of slop */
Jiri Slaby1c456072008-02-07 00:16:46 -0800696
697 if (quot) {
698 info->MCR |= UART_MCR_DTR;
699 outb(info->MCR, info->ioaddr + UART_MCR);
700 } else {
701 info->MCR &= ~UART_MCR_DTR;
702 outb(info->MCR, info->ioaddr + UART_MCR);
703 return 0;
704 }
705
706 cval = inb(info->ioaddr + UART_LCR);
707
708 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
709
710 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
711 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
712 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
713
714#ifdef BOTHER
Alan Cox216ba022008-10-13 10:40:19 +0100715 if (C_BAUD(tty) == BOTHER) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800716 quot = info->baud_base % newspd;
717 quot *= 8;
718 if (quot % newspd > newspd / 2) {
719 quot /= newspd;
720 quot++;
721 } else
722 quot /= newspd;
723
Christoph Hellwig148ff862008-04-30 00:54:29 -0700724 mxser_set_must_enum_value(info->ioaddr, quot);
Jiri Slaby1c456072008-02-07 00:16:46 -0800725 } else
726#endif
Christoph Hellwig148ff862008-04-30 00:54:29 -0700727 mxser_set_must_enum_value(info->ioaddr, 0);
Jiri Slaby1c456072008-02-07 00:16:46 -0800728
729 return 0;
730}
731
732/*
733 * This routine is called to set the UART divisor registers to match
734 * the specified baud rate for a serial port.
735 */
Jason Yanbeca62c2020-05-06 14:17:35 +0800736static void mxser_change_speed(struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -0800737{
Alan Cox216ba022008-10-13 10:40:19 +0100738 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800739 unsigned cflag, cval, fcr;
Jiri Slaby1c456072008-02-07 00:16:46 -0800740 unsigned char status;
741
Alan Coxadc8d742012-07-14 15:31:47 +0100742 cflag = tty->termios.c_cflag;
Alan Cox216ba022008-10-13 10:40:19 +0100743 if (!info->ioaddr)
Jason Yanbeca62c2020-05-06 14:17:35 +0800744 return;
Jiri Slaby1c456072008-02-07 00:16:46 -0800745
Alan Cox216ba022008-10-13 10:40:19 +0100746 if (mxser_set_baud_method[tty->index] == 0)
747 mxser_set_baud(tty, tty_get_baud_rate(tty));
Jiri Slaby1c456072008-02-07 00:16:46 -0800748
749 /* byte size and parity */
750 switch (cflag & CSIZE) {
751 case CS5:
752 cval = 0x00;
753 break;
754 case CS6:
755 cval = 0x01;
756 break;
757 case CS7:
758 cval = 0x02;
759 break;
760 case CS8:
761 cval = 0x03;
762 break;
763 default:
764 cval = 0x00;
765 break; /* too keep GCC shut... */
766 }
767 if (cflag & CSTOPB)
768 cval |= 0x04;
769 if (cflag & PARENB)
770 cval |= UART_LCR_PARITY;
771 if (!(cflag & PARODD))
772 cval |= UART_LCR_EPAR;
773 if (cflag & CMSPAR)
774 cval |= UART_LCR_SPAR;
775
776 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
Jiri Slaby292955a2021-06-18 08:14:13 +0200777 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800778 fcr = UART_FCR_ENABLE_FIFO;
779 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700780 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800781 } else
782 fcr = 0;
783 } else {
784 fcr = UART_FCR_ENABLE_FIFO;
Jiri Slaby292955a2021-06-18 08:14:13 +0200785 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800786 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700787 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800788 } else {
Jiri Slabydc33f642021-06-18 08:14:11 +0200789 switch (info->rx_high_water) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800790 case 1:
791 fcr |= UART_FCR_TRIGGER_1;
792 break;
793 case 4:
794 fcr |= UART_FCR_TRIGGER_4;
795 break;
796 case 8:
797 fcr |= UART_FCR_TRIGGER_8;
798 break;
799 default:
800 fcr |= UART_FCR_TRIGGER_14;
801 break;
802 }
803 }
804 }
805
806 /* CTS flow control flag and modem status interrupts */
807 info->IER &= ~UART_IER_MSI;
808 info->MCR &= ~UART_MCR_AFE;
Peter Hurley5604a982016-04-09 17:53:21 -0700809 tty_port_set_cts_flow(&info->port, cflag & CRTSCTS);
Jiri Slaby1c456072008-02-07 00:16:46 -0800810 if (cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800811 info->IER |= UART_IER_MSI;
Jiri Slaby292955a2021-06-18 08:14:13 +0200812 if ((info->type == PORT_16550A) || (info->board->must_hwid)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800813 info->MCR |= UART_MCR_AFE;
814 } else {
815 status = inb(info->ioaddr + UART_MSR);
Alan Cox216ba022008-10-13 10:40:19 +0100816 if (tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800817 if (status & UART_MSR_CTS) {
Alan Cox216ba022008-10-13 10:40:19 +0100818 tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800819 if (info->type != PORT_16550A &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200820 !info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800821 outb(info->IER & ~UART_IER_THRI,
822 info->ioaddr +
823 UART_IER);
824 info->IER |= UART_IER_THRI;
825 outb(info->IER, info->ioaddr +
826 UART_IER);
827 }
Alan Cox216ba022008-10-13 10:40:19 +0100828 tty_wakeup(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800829 }
830 } else {
831 if (!(status & UART_MSR_CTS)) {
Alan Cox216ba022008-10-13 10:40:19 +0100832 tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800833 if ((info->type != PORT_16550A) &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200834 (!info->board->must_hwid)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800835 info->IER &= ~UART_IER_THRI;
836 outb(info->IER, info->ioaddr +
837 UART_IER);
838 }
839 }
840 }
841 }
Jiri Slaby1c456072008-02-07 00:16:46 -0800842 }
843 outb(info->MCR, info->ioaddr + UART_MCR);
Peter Hurley2d686552016-04-09 17:53:23 -0700844 tty_port_set_check_carrier(&info->port, ~cflag & CLOCAL);
845 if (~cflag & CLOCAL)
Jiri Slaby1c456072008-02-07 00:16:46 -0800846 info->IER |= UART_IER_MSI;
Jiri Slaby1c456072008-02-07 00:16:46 -0800847 outb(info->IER, info->ioaddr + UART_IER);
848
849 /*
850 * Set up parity check flag
851 */
852 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
Alan Cox216ba022008-10-13 10:40:19 +0100853 if (I_INPCK(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800854 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Alan Cox216ba022008-10-13 10:40:19 +0100855 if (I_BRKINT(tty) || I_PARMRK(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800856 info->read_status_mask |= UART_LSR_BI;
857
858 info->ignore_status_mask = 0;
859
Alan Cox216ba022008-10-13 10:40:19 +0100860 if (I_IGNBRK(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800861 info->ignore_status_mask |= UART_LSR_BI;
862 info->read_status_mask |= UART_LSR_BI;
863 /*
864 * If we're ignore parity and break indicators, ignore
865 * overruns too. (For real raw support).
866 */
Alan Cox216ba022008-10-13 10:40:19 +0100867 if (I_IGNPAR(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800868 info->ignore_status_mask |=
869 UART_LSR_OE |
870 UART_LSR_PE |
871 UART_LSR_FE;
872 info->read_status_mask |=
873 UART_LSR_OE |
874 UART_LSR_PE |
875 UART_LSR_FE;
876 }
877 }
Jiri Slaby292955a2021-06-18 08:14:13 +0200878 if (info->board->must_hwid) {
Alan Cox216ba022008-10-13 10:40:19 +0100879 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
880 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
881 if (I_IXON(tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700882 mxser_enable_must_rx_software_flow_control(
883 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800884 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700885 mxser_disable_must_rx_software_flow_control(
886 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800887 }
Alan Cox216ba022008-10-13 10:40:19 +0100888 if (I_IXOFF(tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700889 mxser_enable_must_tx_software_flow_control(
890 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800891 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700892 mxser_disable_must_tx_software_flow_control(
893 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800894 }
895 }
896
897
898 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
899 outb(cval, info->ioaddr + UART_LCR);
Jiri Slaby1c456072008-02-07 00:16:46 -0800900}
901
Alan Cox216ba022008-10-13 10:40:19 +0100902static void mxser_check_modem_status(struct tty_struct *tty,
903 struct mxser_port *port, int status)
Jiri Slaby1c456072008-02-07 00:16:46 -0800904{
905 /* update input line counters */
906 if (status & UART_MSR_TERI)
907 port->icount.rng++;
908 if (status & UART_MSR_DDSR)
909 port->icount.dsr++;
910 if (status & UART_MSR_DDCD)
911 port->icount.dcd++;
912 if (status & UART_MSR_DCTS)
913 port->icount.cts++;
914 port->mon_data.modem_status = status;
Alan Coxbdc04e32009-09-19 13:13:31 -0700915 wake_up_interruptible(&port->port.delta_msr_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800916
Peter Hurley2d686552016-04-09 17:53:23 -0700917 if (tty_port_check_carrier(&port->port) && (status & UART_MSR_DDCD)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800918 if (status & UART_MSR_DCD)
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100919 wake_up_interruptible(&port->port.open_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800920 }
921
Huang Shijief21ec3d2012-08-22 22:13:36 -0400922 if (tty_port_cts_enabled(&port->port)) {
Alan Cox216ba022008-10-13 10:40:19 +0100923 if (tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800924 if (status & UART_MSR_CTS) {
Alan Cox216ba022008-10-13 10:40:19 +0100925 tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800926
927 if ((port->type != PORT_16550A) &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200928 (!port->board->must_hwid)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800929 outb(port->IER & ~UART_IER_THRI,
930 port->ioaddr + UART_IER);
931 port->IER |= UART_IER_THRI;
932 outb(port->IER, port->ioaddr +
933 UART_IER);
934 }
Alan Cox216ba022008-10-13 10:40:19 +0100935 tty_wakeup(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800936 }
937 } else {
938 if (!(status & UART_MSR_CTS)) {
Alan Cox216ba022008-10-13 10:40:19 +0100939 tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800940 if (port->type != PORT_16550A &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200941 !port->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800942 port->IER &= ~UART_IER_THRI;
943 outb(port->IER, port->ioaddr +
944 UART_IER);
945 }
946 }
947 }
948 }
949}
950
Alan Cox67691402009-11-30 13:17:35 +0000951static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -0800952{
Alan Cox67691402009-11-30 13:17:35 +0000953 struct mxser_port *info = container_of(port, struct mxser_port, port);
Jiri Slaby1c456072008-02-07 00:16:46 -0800954 unsigned long page;
955 unsigned long flags;
956
957 page = __get_free_page(GFP_KERNEL);
958 if (!page)
959 return -ENOMEM;
960
961 spin_lock_irqsave(&info->slock, flags);
962
Jiri Slaby1c456072008-02-07 00:16:46 -0800963 if (!info->ioaddr || !info->type) {
Alan Cox216ba022008-10-13 10:40:19 +0100964 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800965 free_page(page);
966 spin_unlock_irqrestore(&info->slock, flags);
967 return 0;
968 }
Alan Cox67691402009-11-30 13:17:35 +0000969 info->port.xmit_buf = (unsigned char *) page;
Jiri Slaby1c456072008-02-07 00:16:46 -0800970
971 /*
972 * Clear the FIFO buffers and disable them
973 * (they will be reenabled in mxser_change_speed())
974 */
Jiri Slaby292955a2021-06-18 08:14:13 +0200975 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -0800976 outb((UART_FCR_CLEAR_RCVR |
977 UART_FCR_CLEAR_XMIT |
978 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
979 else
980 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
981 info->ioaddr + UART_FCR);
982
983 /*
984 * At this point there's no way the LSR could still be 0xFF;
985 * if it is, then bail out, because there's likely no UART
986 * here.
987 */
988 if (inb(info->ioaddr + UART_LSR) == 0xff) {
989 spin_unlock_irqrestore(&info->slock, flags);
990 if (capable(CAP_SYS_ADMIN)) {
Jiri Slabyf43a510d2009-12-09 12:34:15 -0800991 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800992 return 0;
993 } else
994 return -ENODEV;
995 }
996
997 /*
998 * Clear the interrupt registers.
999 */
1000 (void) inb(info->ioaddr + UART_LSR);
1001 (void) inb(info->ioaddr + UART_RX);
1002 (void) inb(info->ioaddr + UART_IIR);
1003 (void) inb(info->ioaddr + UART_MSR);
1004
1005 /*
1006 * Now, initialize the UART
1007 */
1008 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
1009 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
1010 outb(info->MCR, info->ioaddr + UART_MCR);
1011
1012 /*
1013 * Finally, enable interrupts
1014 */
1015 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
1016
Jiri Slaby292955a2021-06-18 08:14:13 +02001017 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -08001018 info->IER |= MOXA_MUST_IER_EGDAI;
1019 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
1020
1021 /*
1022 * And clear the interrupt registers again for luck.
1023 */
1024 (void) inb(info->ioaddr + UART_LSR);
1025 (void) inb(info->ioaddr + UART_RX);
1026 (void) inb(info->ioaddr + UART_IIR);
1027 (void) inb(info->ioaddr + UART_MSR);
1028
Alan Cox216ba022008-10-13 10:40:19 +01001029 clear_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001030 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1031
1032 /*
1033 * and set the speed of the serial port
1034 */
Jiri Slaby27997072017-09-12 12:39:55 +02001035 mxser_change_speed(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001036 spin_unlock_irqrestore(&info->slock, flags);
1037
1038 return 0;
1039}
1040
1041/*
Alan Cox67691402009-11-30 13:17:35 +00001042 * This routine will shutdown a serial port
Jiri Slaby1c456072008-02-07 00:16:46 -08001043 */
Alan Cox67691402009-11-30 13:17:35 +00001044static void mxser_shutdown_port(struct tty_port *port)
Jiri Slaby1c456072008-02-07 00:16:46 -08001045{
Alan Cox67691402009-11-30 13:17:35 +00001046 struct mxser_port *info = container_of(port, struct mxser_port, port);
Jiri Slaby1c456072008-02-07 00:16:46 -08001047 unsigned long flags;
1048
Jiri Slaby1c456072008-02-07 00:16:46 -08001049 spin_lock_irqsave(&info->slock, flags);
1050
1051 /*
1052 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1053 * here so the queue might never be waken up
1054 */
Alan Coxbdc04e32009-09-19 13:13:31 -07001055 wake_up_interruptible(&info->port.delta_msr_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -08001056
1057 /*
Alan Cox67691402009-11-30 13:17:35 +00001058 * Free the xmit buffer, if necessary
Jiri Slaby1c456072008-02-07 00:16:46 -08001059 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001060 if (info->port.xmit_buf) {
1061 free_page((unsigned long) info->port.xmit_buf);
1062 info->port.xmit_buf = NULL;
Jiri Slaby1c456072008-02-07 00:16:46 -08001063 }
1064
1065 info->IER = 0;
1066 outb(0x00, info->ioaddr + UART_IER);
1067
Jiri Slaby1c456072008-02-07 00:16:46 -08001068 /* clear Rx/Tx FIFO's */
Jiri Slaby292955a2021-06-18 08:14:13 +02001069 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -08001070 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
1071 MOXA_MUST_FCR_GDA_MODE_ENABLE,
1072 info->ioaddr + UART_FCR);
1073 else
1074 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
1075 info->ioaddr + UART_FCR);
1076
1077 /* read data port to reset things */
1078 (void) inb(info->ioaddr + UART_RX);
1079
Jiri Slaby1c456072008-02-07 00:16:46 -08001080
Jiri Slaby292955a2021-06-18 08:14:13 +02001081 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -08001082 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1083
1084 spin_unlock_irqrestore(&info->slock, flags);
1085}
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087/*
1088 * This routine is called whenever a serial port is opened. It
1089 * enables interrupts for a serial port, linking in its async structure into
1090 * the IRQ chain. It also performs the serial-specific
1091 * initialization for the tty structure.
1092 */
1093static int mxser_open(struct tty_struct *tty, struct file *filp)
1094{
Jiri Slaby1c456072008-02-07 00:16:46 -08001095 struct mxser_port *info;
Alan Cox67691402009-11-30 13:17:35 +00001096 int line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 line = tty->index;
1099 if (line == MXSER_PORTS)
1100 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001101 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1102 if (!info->ioaddr)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001103 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Alan Coxa2d1e352010-04-23 16:01:18 +01001105 tty->driver_data = info;
Alan Cox67691402009-11-30 13:17:35 +00001106 return tty_port_open(&info->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
Alan Cox978e5952008-04-30 00:53:59 -07001109static void mxser_flush_buffer(struct tty_struct *tty)
1110{
1111 struct mxser_port *info = tty->driver_data;
1112 char fcr;
1113 unsigned long flags;
1114
1115
1116 spin_lock_irqsave(&info->slock, flags);
1117 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1118
1119 fcr = inb(info->ioaddr + UART_FCR);
1120 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1121 info->ioaddr + UART_FCR);
1122 outb(fcr, info->ioaddr + UART_FCR);
1123
1124 spin_unlock_irqrestore(&info->slock, flags);
1125
1126 tty_wakeup(tty);
1127}
1128
1129
Alan Cox67691402009-11-30 13:17:35 +00001130static void mxser_close_port(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Alan Cox1e2b0252009-09-19 13:13:21 -07001132 struct mxser_port *info = container_of(port, struct mxser_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 * At this point we stop accepting input. To do this, we
1136 * disable the receive line status interrupts, and tell the
1137 * interrupt driver to stop checking the data ready bit in the
1138 * line status register.
1139 */
1140 info->IER &= ~UART_IER_RLSI;
Jiri Slaby292955a2021-06-18 08:14:13 +02001141 if (info->board->must_hwid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001143
Alan Cox67691402009-11-30 13:17:35 +00001144 outb(info->IER, info->ioaddr + UART_IER);
1145 /*
1146 * Before we drop DTR, make sure the UART transmitter
1147 * has completely drained; this is especially
1148 * important if there is a transmit FIFO!
1149 */
1150 timeout = jiffies + HZ;
1151 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1152 schedule_timeout_interruptible(5);
1153 if (time_after(jiffies, timeout))
1154 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Alan Cox1e2b0252009-09-19 13:13:21 -07001156}
1157
1158/*
1159 * This routine is called when the serial port gets closed. First, we
1160 * wait for the last remaining data to be sent. Then, we unlink its
1161 * async structure from the interrupt chain if necessary, and we free
1162 * that IRQ if nothing is left in the chain.
1163 */
1164static void mxser_close(struct tty_struct *tty, struct file *filp)
1165{
1166 struct mxser_port *info = tty->driver_data;
1167 struct tty_port *port = &info->port;
1168
Alan Coxa2d1e352010-04-23 16:01:18 +01001169 if (tty->index == MXSER_PORTS || info == NULL)
Alan Cox1e2b0252009-09-19 13:13:21 -07001170 return;
1171 if (tty_port_close_start(port, tty, filp) == 0)
1172 return;
Peter Hurleycd7b4b32016-01-10 14:51:38 -08001173 info->closing = 1;
Alan Cox67691402009-11-30 13:17:35 +00001174 mutex_lock(&port->mutex);
1175 mxser_close_port(port);
Alan Cox1e2b0252009-09-19 13:13:21 -07001176 mxser_flush_buffer(tty);
Peter Hurleyd41861c2016-04-09 17:53:25 -07001177 if (tty_port_initialized(port) && C_HUPCL(tty))
1178 tty_port_lower_dtr_rts(port);
Alan Cox67691402009-11-30 13:17:35 +00001179 mxser_shutdown_port(port);
Peter Hurleyd41861c2016-04-09 17:53:25 -07001180 tty_port_set_initialized(port, 0);
Alan Cox67691402009-11-30 13:17:35 +00001181 mutex_unlock(&port->mutex);
Peter Hurleycd7b4b32016-01-10 14:51:38 -08001182 info->closing = 0;
Alan Coxa6614992009-01-02 13:46:50 +00001183 /* Right now the tty_port set is done outside of the close_end helper
1184 as we don't yet have everyone using refcounts */
1185 tty_port_close_end(port, tty);
1186 tty_port_tty_set(port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
1189static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1190{
1191 int c, total = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001192 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 unsigned long flags;
1194
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001195 if (!info->port.xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001199 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1200 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (c <= 0)
1202 break;
1203
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001204 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001206 info->xmit_head = (info->xmit_head + c) &
1207 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 info->xmit_cnt += c;
1209 spin_unlock_irqrestore(&info->slock, flags);
1210
1211 buf += c;
1212 count -= c;
1213 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001216 if (info->xmit_cnt && !tty->flow.stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001217 if (!tty->hw_stopped ||
1218 (info->type == PORT_16550A) ||
Jiri Slaby292955a2021-06-18 08:14:13 +02001219 (info->board->must_hwid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001221 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1222 UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001224 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 spin_unlock_irqrestore(&info->slock, flags);
1226 }
1227 }
1228 return total;
1229}
1230
Alan Cox0be2ead2008-04-30 00:54:03 -07001231static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Jiri Slaby1c456072008-02-07 00:16:46 -08001233 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 unsigned long flags;
1235
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001236 if (!info->port.xmit_buf)
Alan Cox0be2ead2008-04-30 00:54:03 -07001237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
Alan Cox0be2ead2008-04-30 00:54:03 -07001240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001243 info->port.xmit_buf[info->xmit_head++] = ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1245 info->xmit_cnt++;
1246 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001247 if (!tty->flow.stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001248 if (!tty->hw_stopped ||
1249 (info->type == PORT_16550A) ||
Jiri Slaby292955a2021-06-18 08:14:13 +02001250 info->board->must_hwid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001252 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001254 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 spin_unlock_irqrestore(&info->slock, flags);
1256 }
1257 }
Alan Cox0be2ead2008-04-30 00:54:03 -07001258 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
1261
1262static void mxser_flush_chars(struct tty_struct *tty)
1263{
Jiri Slaby1c456072008-02-07 00:16:46 -08001264 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 unsigned long flags;
1266
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001267 if (info->xmit_cnt <= 0 || tty->flow.stopped || !info->port.xmit_buf ||
Jiri Slabyace7dd92008-07-25 01:48:22 -07001268 (tty->hw_stopped && info->type != PORT_16550A &&
Jiri Slaby292955a2021-06-18 08:14:13 +02001269 !info->board->must_hwid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return;
1271
1272 spin_lock_irqsave(&info->slock, flags);
1273
Jiri Slaby1c456072008-02-07 00:16:46 -08001274 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001276 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 spin_unlock_irqrestore(&info->slock, flags);
1279}
1280
Jiri Slaby03b3b1a2021-05-05 11:19:15 +02001281static unsigned int mxser_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Jiri Slaby1c456072008-02-07 00:16:46 -08001283 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int ret;
1285
1286 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
Jiri Slabyace7dd92008-07-25 01:48:22 -07001287 return ret < 0 ? 0 : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Jiri Slabyfff4ef12021-05-05 11:19:19 +02001290static unsigned int mxser_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Jiri Slaby1c456072008-02-07 00:16:46 -08001292 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return info->xmit_cnt;
1294}
1295
Jiri Slaby1c456072008-02-07 00:16:46 -08001296/*
1297 * ------------------------------------------------------------
1298 * friends of mxser_ioctl()
1299 * ------------------------------------------------------------
1300 */
Alan Cox216ba022008-10-13 10:40:19 +01001301static int mxser_get_serial_info(struct tty_struct *tty,
Al Viro6da5b582018-09-11 22:22:06 -04001302 struct serial_struct *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Alan Cox216ba022008-10-13 10:40:19 +01001304 struct mxser_port *info = tty->driver_data;
Al Viro6da5b582018-09-11 22:22:06 -04001305 struct tty_port *port = &info->port;
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001306 unsigned int closing_wait, close_delay;
Al Viro6da5b582018-09-11 22:22:06 -04001307
1308 if (tty->index == MXSER_PORTS)
1309 return -ENOTTY;
1310
1311 mutex_lock(&port->mutex);
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001312
1313 close_delay = jiffies_to_msecs(info->port.close_delay) / 10;
1314 closing_wait = info->port.closing_wait;
1315 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1316 closing_wait = jiffies_to_msecs(closing_wait) / 10;
1317
Al Viro6da5b582018-09-11 22:22:06 -04001318 ss->type = info->type,
1319 ss->line = tty->index,
1320 ss->port = info->ioaddr,
1321 ss->irq = info->board->irq,
1322 ss->flags = info->port.flags,
1323 ss->baud_base = info->baud_base,
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001324 ss->close_delay = close_delay;
1325 ss->closing_wait = closing_wait;
Al Viro6da5b582018-09-11 22:22:06 -04001326 ss->custom_divisor = info->custom_divisor,
1327 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001328 return 0;
1329}
1330
Alan Cox216ba022008-10-13 10:40:19 +01001331static int mxser_set_serial_info(struct tty_struct *tty,
Al Viro6da5b582018-09-11 22:22:06 -04001332 struct serial_struct *ss)
Jiri Slaby1c456072008-02-07 00:16:46 -08001333{
Alan Cox216ba022008-10-13 10:40:19 +01001334 struct mxser_port *info = tty->driver_data;
Alan Cox07f86c02009-11-30 13:17:41 +00001335 struct tty_port *port = &info->port;
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001336 speed_t baud;
Jiri Slaby1c456072008-02-07 00:16:46 -08001337 unsigned long sl_flags;
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001338 unsigned int flags, close_delay, closing_wait;
Jiri Slaby1c456072008-02-07 00:16:46 -08001339 int retval = 0;
1340
Al Viro6da5b582018-09-11 22:22:06 -04001341 if (tty->index == MXSER_PORTS)
1342 return -ENOTTY;
1343 if (tty_io_error(tty))
1344 return -EIO;
Jiri Slaby1c456072008-02-07 00:16:46 -08001345
Al Viro6da5b582018-09-11 22:22:06 -04001346 mutex_lock(&port->mutex);
1347 if (!info->ioaddr) {
1348 mutex_unlock(&port->mutex);
1349 return -ENODEV;
1350 }
1351
1352 if (ss->irq != info->board->irq ||
1353 ss->port != info->ioaddr) {
1354 mutex_unlock(&port->mutex);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001355 return -EINVAL;
Al Viro6da5b582018-09-11 22:22:06 -04001356 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001357
Alan Cox07f86c02009-11-30 13:17:41 +00001358 flags = port->flags & ASYNC_SPD_MASK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001359
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001360 close_delay = msecs_to_jiffies(ss->close_delay * 10);
1361 closing_wait = ss->closing_wait;
1362 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1363 closing_wait = msecs_to_jiffies(closing_wait * 10);
1364
Jiri Slaby1c456072008-02-07 00:16:46 -08001365 if (!capable(CAP_SYS_ADMIN)) {
Al Viro6da5b582018-09-11 22:22:06 -04001366 if ((ss->baud_base != info->baud_base) ||
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001367 (close_delay != info->port.close_delay) ||
Johan Hovoldb91cfb22021-04-07 12:23:32 +02001368 (closing_wait != info->port.closing_wait) ||
Al Viro6da5b582018-09-11 22:22:06 -04001369 ((ss->flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK))) {
1370 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001371 return -EPERM;
Al Viro6da5b582018-09-11 22:22:06 -04001372 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001373 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
Al Viro6da5b582018-09-11 22:22:06 -04001374 (ss->flags & ASYNC_USR_MASK));
Jiri Slaby1c456072008-02-07 00:16:46 -08001375 } else {
1376 /*
1377 * OK, past this point, all the error checking has been done.
1378 * At this point, we start making changes.....
1379 */
Alan Cox07f86c02009-11-30 13:17:41 +00001380 port->flags = ((port->flags & ~ASYNC_FLAGS) |
Al Viro6da5b582018-09-11 22:22:06 -04001381 (ss->flags & ASYNC_FLAGS));
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001382 port->close_delay = close_delay;
1383 port->closing_wait = closing_wait;
Alan Cox07f86c02009-11-30 13:17:41 +00001384 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
Al Viro6da5b582018-09-11 22:22:06 -04001385 (ss->baud_base != info->baud_base ||
1386 ss->custom_divisor !=
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001387 info->custom_divisor)) {
Al Viro6da5b582018-09-11 22:22:06 -04001388 if (ss->custom_divisor == 0) {
1389 mutex_unlock(&port->mutex);
Alan Cox07f86c02009-11-30 13:17:41 +00001390 return -EINVAL;
Al Viro6da5b582018-09-11 22:22:06 -04001391 }
1392 baud = ss->baud_base / ss->custom_divisor;
Alan Cox216ba022008-10-13 10:40:19 +01001393 tty_encode_baud_rate(tty, baud, baud);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001394 }
Johan Hovoldb91cfb22021-04-07 12:23:32 +02001395
1396 info->type = ss->type;
1397
1398 process_txrx_fifo(info);
Jiri Slaby1c456072008-02-07 00:16:46 -08001399 }
1400
Peter Hurleyd41861c2016-04-09 17:53:25 -07001401 if (tty_port_initialized(port)) {
Alan Cox07f86c02009-11-30 13:17:41 +00001402 if (flags != (port->flags & ASYNC_SPD_MASK)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001403 spin_lock_irqsave(&info->slock, sl_flags);
Jiri Slaby27997072017-09-12 12:39:55 +02001404 mxser_change_speed(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001405 spin_unlock_irqrestore(&info->slock, sl_flags);
1406 }
Alan Cox67691402009-11-30 13:17:35 +00001407 } else {
Alan Cox07f86c02009-11-30 13:17:41 +00001408 retval = mxser_activate(port, tty);
Alan Cox67691402009-11-30 13:17:35 +00001409 if (retval == 0)
Peter Hurleyd41861c2016-04-09 17:53:25 -07001410 tty_port_set_initialized(port, 1);
Alan Cox67691402009-11-30 13:17:35 +00001411 }
Al Viro6da5b582018-09-11 22:22:06 -04001412 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001413 return retval;
1414}
1415
1416/*
1417 * mxser_get_lsr_info - get line status register info
1418 *
1419 * Purpose: Let user call ioctl() to get info when the UART physically
1420 * is emptied. On bus types like RS485, the transmitter must
1421 * release the bus after transmitting. This must be done when
1422 * the transmit shift register is empty, not be done when the
1423 * transmit holding register is empty. This functionality
1424 * allows an RS485 driver to be written in user space.
1425 */
1426static int mxser_get_lsr_info(struct mxser_port *info,
1427 unsigned int __user *value)
1428{
1429 unsigned char status;
1430 unsigned int result;
1431 unsigned long flags;
1432
1433 spin_lock_irqsave(&info->slock, flags);
1434 status = inb(info->ioaddr + UART_LSR);
1435 spin_unlock_irqrestore(&info->slock, flags);
1436 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1437 return put_user(result, value);
1438}
1439
Alan Cox60b33c12011-02-14 16:26:14 +00001440static int mxser_tiocmget(struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -08001441{
1442 struct mxser_port *info = tty->driver_data;
1443 unsigned char control, status;
1444 unsigned long flags;
1445
1446
1447 if (tty->index == MXSER_PORTS)
1448 return -ENOIOCTLCMD;
Peter Hurley18900ca2016-04-09 17:06:48 -07001449 if (tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001450 return -EIO;
1451
1452 control = info->MCR;
1453
1454 spin_lock_irqsave(&info->slock, flags);
1455 status = inb(info->ioaddr + UART_MSR);
1456 if (status & UART_MSR_ANY_DELTA)
Alan Cox216ba022008-10-13 10:40:19 +01001457 mxser_check_modem_status(tty, info, status);
Jiri Slaby1c456072008-02-07 00:16:46 -08001458 spin_unlock_irqrestore(&info->slock, flags);
1459 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1460 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1461 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1462 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1463 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1464 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1465}
1466
Alan Cox20b9d172011-02-14 16:26:50 +00001467static int mxser_tiocmset(struct tty_struct *tty,
Jiri Slaby1c456072008-02-07 00:16:46 -08001468 unsigned int set, unsigned int clear)
1469{
1470 struct mxser_port *info = tty->driver_data;
1471 unsigned long flags;
1472
1473
1474 if (tty->index == MXSER_PORTS)
1475 return -ENOIOCTLCMD;
Peter Hurley18900ca2016-04-09 17:06:48 -07001476 if (tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001477 return -EIO;
1478
1479 spin_lock_irqsave(&info->slock, flags);
1480
1481 if (set & TIOCM_RTS)
1482 info->MCR |= UART_MCR_RTS;
1483 if (set & TIOCM_DTR)
1484 info->MCR |= UART_MCR_DTR;
1485
1486 if (clear & TIOCM_RTS)
1487 info->MCR &= ~UART_MCR_RTS;
1488 if (clear & TIOCM_DTR)
1489 info->MCR &= ~UART_MCR_DTR;
1490
1491 outb(info->MCR, info->ioaddr + UART_MCR);
1492 spin_unlock_irqrestore(&info->slock, flags);
1493 return 0;
1494}
1495
Jiri Slaby1c456072008-02-07 00:16:46 -08001496static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1497{
Alan Cox07f86c02009-11-30 13:17:41 +00001498 struct mxser_port *ip;
1499 struct tty_port *port;
Alan Cox216ba022008-10-13 10:40:19 +01001500 struct tty_struct *tty;
Jiri Slaby1c456072008-02-07 00:16:46 -08001501 int result, status;
1502 unsigned int i, j;
Alan Cox9d6d1622008-04-30 00:53:20 -07001503 int ret = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001504
1505 switch (cmd) {
1506 case MOXA_GET_MAJOR:
Manuel Zerpies5a3c6b2512011-06-16 14:07:22 +02001507 printk_ratelimited(KERN_WARNING "mxser: '%s' uses deprecated ioctl "
Jiri Slaby8f3d1372008-07-29 22:33:38 -07001508 "%x (GET_MAJOR), fix your userspace\n",
1509 current->comm, cmd);
Jiri Slaby1c456072008-02-07 00:16:46 -08001510 return put_user(ttymajor, (int __user *)argp);
1511
1512 case MOXA_CHKPORTENABLE:
1513 result = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001514 for (i = 0; i < MXSER_BOARDS; i++)
1515 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1516 if (mxser_boards[i].ports[j].ioaddr)
1517 result |= (1 << i);
Jiri Slaby1c456072008-02-07 00:16:46 -08001518 return put_user(result, (unsigned long __user *)argp);
1519 case MOXA_GETDATACOUNT:
Alan Cox07f86c02009-11-30 13:17:41 +00001520 /* The receive side is locked by port->slock but it isn't
1521 clear that an exact snapshot is worth copying here */
Jiri Slaby1c456072008-02-07 00:16:46 -08001522 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
Alan Cox9d6d1622008-04-30 00:53:20 -07001523 ret = -EFAULT;
Alan Cox9d6d1622008-04-30 00:53:20 -07001524 return ret;
Jiri Slaby72800df2008-07-25 01:48:20 -07001525 case MOXA_GETMSTATUS: {
1526 struct mxser_mstatus ms, __user *msu = argp;
Jiri Slaby1c456072008-02-07 00:16:46 -08001527 for (i = 0; i < MXSER_BOARDS; i++)
1528 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
Alan Cox07f86c02009-11-30 13:17:41 +00001529 ip = &mxser_boards[i].ports[j];
1530 port = &ip->port;
Jiri Slaby72800df2008-07-25 01:48:20 -07001531 memset(&ms, 0, sizeof(ms));
Jiri Slaby1c456072008-02-07 00:16:46 -08001532
Alan Cox07f86c02009-11-30 13:17:41 +00001533 mutex_lock(&port->mutex);
1534 if (!ip->ioaddr)
Jiri Slaby72800df2008-07-25 01:48:20 -07001535 goto copy;
Alan Cox216ba022008-10-13 10:40:19 +01001536
Alan Cox07f86c02009-11-30 13:17:41 +00001537 tty = tty_port_tty_get(port);
Jiri Slaby1c456072008-02-07 00:16:46 -08001538
Alan Coxadc8d742012-07-14 15:31:47 +01001539 if (!tty)
Alan Cox07f86c02009-11-30 13:17:41 +00001540 ms.cflag = ip->normal_termios.c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -08001541 else
Alan Coxadc8d742012-07-14 15:31:47 +01001542 ms.cflag = tty->termios.c_cflag;
Alan Cox216ba022008-10-13 10:40:19 +01001543 tty_kref_put(tty);
Alan Cox07f86c02009-11-30 13:17:41 +00001544 spin_lock_irq(&ip->slock);
1545 status = inb(ip->ioaddr + UART_MSR);
1546 spin_unlock_irq(&ip->slock);
Jiri Slaby72800df2008-07-25 01:48:20 -07001547 if (status & UART_MSR_DCD)
1548 ms.dcd = 1;
1549 if (status & UART_MSR_DSR)
1550 ms.dsr = 1;
1551 if (status & UART_MSR_CTS)
1552 ms.cts = 1;
1553 copy:
Alan Cox07f86c02009-11-30 13:17:41 +00001554 mutex_unlock(&port->mutex);
1555 if (copy_to_user(msu, &ms, sizeof(ms)))
Jiri Slaby72800df2008-07-25 01:48:20 -07001556 return -EFAULT;
Jiri Slaby72800df2008-07-25 01:48:20 -07001557 msu++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001558 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001559 return 0;
Jiri Slaby72800df2008-07-25 01:48:20 -07001560 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001561 case MOXA_ASPP_MON_EXT: {
Jiri Slaby72800df2008-07-25 01:48:20 -07001562 struct mxser_mon_ext *me; /* it's 2k, stack unfriendly */
1563 unsigned int cflag, iflag, p;
1564 u8 opmode;
1565
1566 me = kzalloc(sizeof(*me), GFP_KERNEL);
1567 if (!me)
1568 return -ENOMEM;
Jiri Slaby1c456072008-02-07 00:16:46 -08001569
Jiri Slaby72800df2008-07-25 01:48:20 -07001570 for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
1571 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
1572 if (p >= ARRAY_SIZE(me->rx_cnt)) {
1573 i = MXSER_BOARDS;
1574 break;
1575 }
Alan Cox07f86c02009-11-30 13:17:41 +00001576 ip = &mxser_boards[i].ports[j];
1577 port = &ip->port;
Jiri Slaby1c456072008-02-07 00:16:46 -08001578
Alan Cox07f86c02009-11-30 13:17:41 +00001579 mutex_lock(&port->mutex);
1580 if (!ip->ioaddr) {
1581 mutex_unlock(&port->mutex);
1582 continue;
1583 }
1584
1585 spin_lock_irq(&ip->slock);
1586 status = mxser_get_msr(ip->ioaddr, 0, p);
Jiri Slaby1c456072008-02-07 00:16:46 -08001587
1588 if (status & UART_MSR_TERI)
Alan Cox07f86c02009-11-30 13:17:41 +00001589 ip->icount.rng++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001590 if (status & UART_MSR_DDSR)
Alan Cox07f86c02009-11-30 13:17:41 +00001591 ip->icount.dsr++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001592 if (status & UART_MSR_DDCD)
Alan Cox07f86c02009-11-30 13:17:41 +00001593 ip->icount.dcd++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001594 if (status & UART_MSR_DCTS)
Alan Cox07f86c02009-11-30 13:17:41 +00001595 ip->icount.cts++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001596
Alan Cox07f86c02009-11-30 13:17:41 +00001597 ip->mon_data.modem_status = status;
1598 me->rx_cnt[p] = ip->mon_data.rxcnt;
1599 me->tx_cnt[p] = ip->mon_data.txcnt;
1600 me->up_rxcnt[p] = ip->mon_data.up_rxcnt;
1601 me->up_txcnt[p] = ip->mon_data.up_txcnt;
Jiri Slaby72800df2008-07-25 01:48:20 -07001602 me->modem_status[p] =
Alan Cox07f86c02009-11-30 13:17:41 +00001603 ip->mon_data.modem_status;
1604 spin_unlock_irq(&ip->slock);
1605
1606 tty = tty_port_tty_get(&ip->port);
Jiri Slaby1c456072008-02-07 00:16:46 -08001607
Alan Coxadc8d742012-07-14 15:31:47 +01001608 if (!tty) {
Alan Cox07f86c02009-11-30 13:17:41 +00001609 cflag = ip->normal_termios.c_cflag;
1610 iflag = ip->normal_termios.c_iflag;
1611 me->baudrate[p] = tty_termios_baud_rate(&ip->normal_termios);
Jiri Slaby1c456072008-02-07 00:16:46 -08001612 } else {
Alan Coxadc8d742012-07-14 15:31:47 +01001613 cflag = tty->termios.c_cflag;
1614 iflag = tty->termios.c_iflag;
Alan Cox216ba022008-10-13 10:40:19 +01001615 me->baudrate[p] = tty_get_baud_rate(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001616 }
Alan Cox216ba022008-10-13 10:40:19 +01001617 tty_kref_put(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001618
Jiri Slaby72800df2008-07-25 01:48:20 -07001619 me->databits[p] = cflag & CSIZE;
1620 me->stopbits[p] = cflag & CSTOPB;
1621 me->parity[p] = cflag & (PARENB | PARODD |
1622 CMSPAR);
Jiri Slaby1c456072008-02-07 00:16:46 -08001623
1624 if (cflag & CRTSCTS)
Jiri Slaby72800df2008-07-25 01:48:20 -07001625 me->flowctrl[p] |= 0x03;
Jiri Slaby1c456072008-02-07 00:16:46 -08001626
1627 if (iflag & (IXON | IXOFF))
Jiri Slaby72800df2008-07-25 01:48:20 -07001628 me->flowctrl[p] |= 0x0C;
Jiri Slaby1c456072008-02-07 00:16:46 -08001629
Alan Cox07f86c02009-11-30 13:17:41 +00001630 if (ip->type == PORT_16550A)
Jiri Slaby72800df2008-07-25 01:48:20 -07001631 me->fifo[p] = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -08001632
Jiri Slaby292955a2021-06-18 08:14:13 +02001633 if (ip->board->must_hwid == MOXA_MUST_MU860_HWID) {
Matwey V. Kornilovdfc7b832013-05-21 13:57:37 +04001634 opmode = inb(ip->opmode_ioaddr)>>((p % 4) * 2);
1635 opmode &= OP_MODE_MASK;
1636 } else {
1637 opmode = RS232_MODE;
1638 }
Jiri Slaby72800df2008-07-25 01:48:20 -07001639 me->iftype[p] = opmode;
Alan Cox07f86c02009-11-30 13:17:41 +00001640 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001641 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001642 }
Jiri Slaby72800df2008-07-25 01:48:20 -07001643 if (copy_to_user(argp, me, sizeof(*me)))
1644 ret = -EFAULT;
1645 kfree(me);
1646 return ret;
Alan Cox9d6d1622008-04-30 00:53:20 -07001647 }
1648 default:
Jiri Slaby1c456072008-02-07 00:16:46 -08001649 return -ENOIOCTLCMD;
1650 }
1651 return 0;
1652}
1653
1654static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1655 struct async_icount *cprev)
1656{
1657 struct async_icount cnow;
1658 unsigned long flags;
1659 int ret;
1660
1661 spin_lock_irqsave(&info->slock, flags);
1662 cnow = info->icount; /* atomic copy */
1663 spin_unlock_irqrestore(&info->slock, flags);
1664
1665 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1666 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1667 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1668 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1669
1670 *cprev = cnow;
1671
1672 return ret;
1673}
1674
Alan Cox6caa76b2011-02-14 16:27:22 +00001675static int mxser_ioctl(struct tty_struct *tty,
Jiri Slaby1c456072008-02-07 00:16:46 -08001676 unsigned int cmd, unsigned long arg)
1677{
1678 struct mxser_port *info = tty->driver_data;
1679 struct async_icount cnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 unsigned long flags;
1681 void __user *argp = (void __user *)arg;
1682
1683 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001684 return mxser_ioctl_special(cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001687 int p;
1688 unsigned long opmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1690 int shiftbit;
1691 unsigned char val, mask;
1692
Jiri Slaby292955a2021-06-18 08:14:13 +02001693 if (info->board->must_hwid != MOXA_MUST_MU860_HWID)
Matwey V. Kornilove037f952013-05-22 11:13:38 +04001694 return -EFAULT;
1695
Jiri Slaby1c456072008-02-07 00:16:46 -08001696 p = tty->index % 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (cmd == MOXA_SET_OP_MODE) {
1698 if (get_user(opmode, (int __user *) argp))
1699 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001700 if (opmode != RS232_MODE &&
1701 opmode != RS485_2WIRE_MODE &&
1702 opmode != RS422_MODE &&
1703 opmode != RS485_4WIRE_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return -EFAULT;
1705 mask = ModeMask[p];
1706 shiftbit = p * 2;
Alan Cox07f86c02009-11-30 13:17:41 +00001707 spin_lock_irq(&info->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 val = inb(info->opmode_ioaddr);
1709 val &= mask;
1710 val |= (opmode << shiftbit);
1711 outb(val, info->opmode_ioaddr);
Alan Cox07f86c02009-11-30 13:17:41 +00001712 spin_unlock_irq(&info->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 } else {
1714 shiftbit = p * 2;
Alan Cox07f86c02009-11-30 13:17:41 +00001715 spin_lock_irq(&info->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 opmode = inb(info->opmode_ioaddr) >> shiftbit;
Alan Cox07f86c02009-11-30 13:17:41 +00001717 spin_unlock_irq(&info->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 opmode &= OP_MODE_MASK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001719 if (put_user(opmode, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return -EFAULT;
1721 }
1722 return 0;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Al Viro6da5b582018-09-11 22:22:06 -04001725 if (cmd != TIOCMIWAIT && tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001726 return -EIO;
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 case TIOCSERGETLSR: /* Get line status register */
Alan Cox9d6d1622008-04-30 00:53:20 -07001730 return mxser_get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /*
1732 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1733 * - mask passed in arg for lines of interest
1734 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1735 * Caller should use TIOCGICOUNT to see which one it was
1736 */
Jiri Slabyfc838152007-04-23 14:41:04 -07001737 case TIOCMIWAIT:
1738 spin_lock_irqsave(&info->slock, flags);
1739 cnow = info->icount; /* note the counters on entry */
1740 spin_unlock_irqrestore(&info->slock, flags);
1741
Alan Coxbdc04e32009-09-19 13:13:31 -07001742 return wait_event_interruptible(info->port.delta_msr_wait,
Jiri Slaby1c456072008-02-07 00:16:46 -08001743 mxser_cflags_changed(info, arg, &cnow));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 case MOXA_HighSpeedOn:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001745 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
Jiri Slaby1c456072008-02-07 00:16:46 -08001746 case MOXA_SDS_RSTICOUNTER:
Alan Cox07f86c02009-11-30 13:17:41 +00001747 spin_lock_irq(&info->slock);
Jiri Slaby1c456072008-02-07 00:16:46 -08001748 info->mon_data.rxcnt = 0;
1749 info->mon_data.txcnt = 0;
Alan Cox07f86c02009-11-30 13:17:41 +00001750 spin_unlock_irq(&info->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return 0;
1752
1753 case MOXA_ASPP_OQUEUE:{
Jiri Slaby1c456072008-02-07 00:16:46 -08001754 int len, lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Jiri Slaby1c456072008-02-07 00:16:46 -08001756 len = mxser_chars_in_buffer(tty);
Dan Carpenterc6eb69a2010-04-06 14:34:50 -07001757 spin_lock_irq(&info->slock);
Jiri Slabya75b7b62009-09-10 12:20:08 +02001758 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_THRE;
Alan Cox07f86c02009-11-30 13:17:41 +00001759 spin_unlock_irq(&info->slock);
Jiri Slaby1c456072008-02-07 00:16:46 -08001760 len += (lsr ? 0 : 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Jiri Slaby1c456072008-02-07 00:16:46 -08001762 return put_user(len, (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001764 case MOXA_ASPP_MON: {
1765 int mcr, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Dan Carpenterc6eb69a2010-04-06 14:34:50 -07001767 spin_lock_irq(&info->slock);
Jiri Slaby1c456072008-02-07 00:16:46 -08001768 status = mxser_get_msr(info->ioaddr, 1, tty->index);
Alan Cox216ba022008-10-13 10:40:19 +01001769 mxser_check_modem_status(tty, info, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Jiri Slaby1c456072008-02-07 00:16:46 -08001771 mcr = inb(info->ioaddr + UART_MCR);
Dan Carpenterc6eb69a2010-04-06 14:34:50 -07001772 spin_unlock_irq(&info->slock);
Alan Cox07f86c02009-11-30 13:17:41 +00001773
Jiri Slaby1c456072008-02-07 00:16:46 -08001774 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1775 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1776 else
1777 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Jiri Slaby1c456072008-02-07 00:16:46 -08001779 if (mcr & MOXA_MUST_MCR_TX_XON)
1780 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1781 else
1782 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1783
Alan Cox216ba022008-10-13 10:40:19 +01001784 if (tty->hw_stopped)
Jiri Slaby1c456072008-02-07 00:16:46 -08001785 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1786 else
1787 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
Alan Cox07f86c02009-11-30 13:17:41 +00001788
Jiri Slaby1c456072008-02-07 00:16:46 -08001789 if (copy_to_user(argp, &info->mon_data,
1790 sizeof(struct mxser_mon)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001794 }
1795 case MOXA_ASPP_LSTATUS: {
1796 if (put_user(info->err_shadow, (unsigned char __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Jiri Slaby1c456072008-02-07 00:16:46 -08001799 info->err_shadow = 0;
1800 return 0;
1801 }
1802 case MOXA_SET_BAUD_METHOD: {
1803 int method;
1804
1805 if (get_user(method, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001807 mxser_set_baud_method[tty->index] = method;
1808 return put_user(method, (int __user *)argp);
1809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 default:
1811 return -ENOIOCTLCMD;
1812 }
1813 return 0;
1814}
1815
Alan Cox05871022010-09-16 18:21:52 +01001816 /*
1817 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1818 * Return: write counters to the user passed counter struct
1819 * NB: both 1->0 and 0->1 transitions are counted except for
1820 * RI where only 0->1 is counted.
1821 */
1822
1823static int mxser_get_icount(struct tty_struct *tty,
1824 struct serial_icounter_struct *icount)
1825
1826{
1827 struct mxser_port *info = tty->driver_data;
1828 struct async_icount cnow;
1829 unsigned long flags;
1830
1831 spin_lock_irqsave(&info->slock, flags);
1832 cnow = info->icount;
1833 spin_unlock_irqrestore(&info->slock, flags);
1834
1835 icount->frame = cnow.frame;
1836 icount->brk = cnow.brk;
1837 icount->overrun = cnow.overrun;
1838 icount->buf_overrun = cnow.buf_overrun;
1839 icount->parity = cnow.parity;
1840 icount->rx = cnow.rx;
1841 icount->tx = cnow.tx;
1842 icount->cts = cnow.cts;
1843 icount->dsr = cnow.dsr;
1844 icount->rng = cnow.rng;
1845 icount->dcd = cnow.dcd;
1846 return 0;
1847}
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849static void mxser_stoprx(struct tty_struct *tty)
1850{
Jiri Slaby1c456072008-02-07 00:16:46 -08001851 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 info->ldisc_stop_rx = 1;
1854 if (I_IXOFF(tty)) {
Jiri Slaby292955a2021-06-18 08:14:13 +02001855 if (info->board->must_hwid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001857 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 info->x_char = STOP_CHAR(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001860 outb(0, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001862 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
1864 }
1865
Peter Hurley9db276f2016-01-10 20:36:15 -08001866 if (C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001867 info->MCR &= ~UART_MCR_RTS;
1868 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870}
1871
1872/*
1873 * This routine is called by the upper-layer tty layer to signal that
1874 * incoming characters should be throttled.
1875 */
1876static void mxser_throttle(struct tty_struct *tty)
1877{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
1881static void mxser_unthrottle(struct tty_struct *tty)
1882{
Jiri Slaby1c456072008-02-07 00:16:46 -08001883 struct mxser_port *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001884
Jiri Slaby1c456072008-02-07 00:16:46 -08001885 /* startrx */
1886 info->ldisc_stop_rx = 0;
1887 if (I_IXOFF(tty)) {
1888 if (info->x_char)
1889 info->x_char = 0;
1890 else {
Jiri Slaby292955a2021-06-18 08:14:13 +02001891 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001892 info->IER |= MOXA_MUST_RECV_ISR;
1893 outb(info->IER, info->ioaddr + UART_IER);
1894 } else {
1895 info->x_char = START_CHAR(tty);
1896 outb(0, info->ioaddr + UART_IER);
1897 info->IER |= UART_IER_THRI;
1898 outb(info->IER, info->ioaddr + UART_IER);
1899 }
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
1902
Peter Hurley9db276f2016-01-10 20:36:15 -08001903 if (C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001904 info->MCR |= UART_MCR_RTS;
1905 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907}
1908
1909/*
1910 * mxser_stop() and mxser_start()
1911 *
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001912 * This routines are called before setting or resetting tty->flow.stopped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 * They enable or disable transmitter interrupts, as necessary.
1914 */
1915static void mxser_stop(struct tty_struct *tty)
1916{
Jiri Slaby1c456072008-02-07 00:16:46 -08001917 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 unsigned long flags;
1919
1920 spin_lock_irqsave(&info->slock, flags);
1921 if (info->IER & UART_IER_THRI) {
1922 info->IER &= ~UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001923 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925 spin_unlock_irqrestore(&info->slock, flags);
1926}
1927
1928static void mxser_start(struct tty_struct *tty)
1929{
Jiri Slaby1c456072008-02-07 00:16:46 -08001930 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 unsigned long flags;
1932
1933 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001934 if (info->xmit_cnt && info->port.xmit_buf) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001935 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001937 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
1939 spin_unlock_irqrestore(&info->slock, flags);
1940}
1941
Jiri Slaby1c456072008-02-07 00:16:46 -08001942static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1943{
1944 struct mxser_port *info = tty->driver_data;
1945 unsigned long flags;
1946
1947 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby27997072017-09-12 12:39:55 +02001948 mxser_change_speed(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001949 spin_unlock_irqrestore(&info->slock, flags);
1950
Peter Hurley9db276f2016-01-10 20:36:15 -08001951 if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001952 tty->hw_stopped = 0;
1953 mxser_start(tty);
1954 }
1955
1956 /* Handle sw stopped */
Peter Hurley9db276f2016-01-10 20:36:15 -08001957 if ((old_termios->c_iflag & IXON) && !I_IXON(tty)) {
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001958 tty->flow.stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001959
Jiri Slaby292955a2021-06-18 08:14:13 +02001960 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001961 spin_lock_irqsave(&info->slock, flags);
Christoph Hellwig148ff862008-04-30 00:54:29 -07001962 mxser_disable_must_rx_software_flow_control(
1963 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -08001964 spin_unlock_irqrestore(&info->slock, flags);
1965 }
1966
1967 mxser_start(tty);
1968 }
1969}
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971/*
1972 * mxser_wait_until_sent() --- wait until the transmitter is empty
1973 */
1974static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1975{
Jiri Slaby1c456072008-02-07 00:16:46 -08001976 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 unsigned long orig_jiffies, char_time;
Alan Cox07f86c02009-11-30 13:17:41 +00001978 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 int lsr;
1980
1981 if (info->type == PORT_UNKNOWN)
1982 return;
1983
1984 if (info->xmit_fifo_size == 0)
1985 return; /* Just in case.... */
1986
1987 orig_jiffies = jiffies;
1988 /*
1989 * Set the check interval to be 1/5 of the estimated time to
1990 * send a single character, and make it at least 1. The check
1991 * interval should also be less than the timeout.
1992 *
1993 * Note: we have to use pretty tight timings here to satisfy
1994 * the NIST-PCTS.
1995 */
1996 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1997 char_time = char_time / 5;
1998 if (char_time == 0)
1999 char_time = 1;
2000 if (timeout && timeout < char_time)
2001 char_time = timeout;
2002 /*
2003 * If the transmitter hasn't cleared in twice the approximate
2004 * amount of time to send the entire FIFO, it probably won't
2005 * ever clear. This assumes the UART isn't doing flow
2006 * control, which is currently the case. Hence, if it ever
2007 * takes longer than info->timeout, this is probably due to a
2008 * UART bug of some kind. So, we clamp the timeout parameter at
2009 * 2*info->timeout.
2010 */
2011 if (!timeout || timeout > 2 * info->timeout)
2012 timeout = 2 * info->timeout;
Jiri Slaby8bab5342011-07-14 14:35:15 +02002013
Alan Cox07f86c02009-11-30 13:17:41 +00002014 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08002015 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Alan Cox07f86c02009-11-30 13:17:41 +00002016 spin_unlock_irqrestore(&info->slock, flags);
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07002017 schedule_timeout_interruptible(char_time);
Alan Cox07f86c02009-11-30 13:17:41 +00002018 spin_lock_irqsave(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (signal_pending(current))
2020 break;
2021 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2022 break;
2023 }
Alan Cox07f86c02009-11-30 13:17:41 +00002024 spin_unlock_irqrestore(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028/*
2029 * This routine is called by tty_hangup() when a hangup is signaled.
2030 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002031static void mxser_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Jiri Slaby1c456072008-02-07 00:16:46 -08002033 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 mxser_flush_buffer(tty);
Alan Cox3b6826b2009-01-02 13:45:58 +00002036 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039/*
2040 * mxser_rs_break() --- routine which turns the break handling on or off
2041 */
Alan Cox9e98966c2008-07-22 11:18:03 +01002042static int mxser_rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Jiri Slaby1c456072008-02-07 00:16:46 -08002044 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 unsigned long flags;
2046
2047 spin_lock_irqsave(&info->slock, flags);
2048 if (break_state == -1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002049 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2050 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002052 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2053 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 spin_unlock_irqrestore(&info->slock, flags);
Alan Cox9e98966c2008-07-22 11:18:03 +01002055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02002058static bool mxser_receive_chars_new(struct tty_struct *tty,
2059 struct mxser_port *port, u8 status, int *cnt)
2060{
2061 enum mxser_must_hwid hwid = port->board->must_hwid;
2062 u8 gdl;
2063
2064 if (hwid == MOXA_OTHER_UART)
2065 return false;
2066 if (status & UART_LSR_SPECIAL)
2067 return false;
2068 if (hwid == MOXA_MUST_MU860_HWID && (status & MOXA_MUST_LSR_RERR))
2069 return false;
2070 if (status & MOXA_MUST_LSR_RERR)
2071 return false;
2072
2073 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2074 if (hwid == MOXA_MUST_MU150_HWID)
2075 gdl &= MOXA_MUST_GDL_MASK;
2076
2077 if (gdl >= tty->receive_room && !port->ldisc_stop_rx)
2078 mxser_stoprx(tty);
2079
2080 while (gdl--) {
2081 u8 ch = inb(port->ioaddr + UART_RX);
2082 tty_insert_flip_char(&port->port, ch, 0);
2083 (*cnt)++;
2084 }
2085
2086 return true;
2087}
2088
Jiri Slaby15517802021-06-18 08:14:18 +02002089static u8 mxser_receive_chars(struct tty_struct *tty,
2090 struct mxser_port *port, u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02002092 unsigned char ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 int ignored = 0;
2094 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 int recv_room;
2096 int max = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Alan Cox33f0f882006-01-09 20:54:13 -08002098 recv_room = tty->receive_room;
Alan Cox216ba022008-10-13 10:40:19 +01002099 if (recv_room == 0 && !port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02002102 if (mxser_receive_chars_new(tty, port, status, &cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 goto end_intr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 do {
2106 if (max-- < 0)
2107 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Jiri Slaby1c456072008-02-07 00:16:46 -08002109 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby15517802021-06-18 08:14:18 +02002110 if (port->board->must_hwid && (status & UART_LSR_OE))
Jiri Slaby1c456072008-02-07 00:16:46 -08002111 outb(0x23, port->ioaddr + UART_FCR);
Jiri Slaby15517802021-06-18 08:14:18 +02002112 status &= port->read_status_mask;
2113 if (status & port->ignore_status_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 if (++ignored > 100)
2115 break;
2116 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002117 char flag = 0;
Jiri Slaby15517802021-06-18 08:14:18 +02002118 if (status & UART_LSR_SPECIAL) {
2119 if (status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002120 flag = TTY_BREAK;
Jiri Slaby1c456072008-02-07 00:16:46 -08002121 port->icount.brk++;
2122
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002123 if (port->port.flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 do_SAK(tty);
Jiri Slaby15517802021-06-18 08:14:18 +02002125 } else if (status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002126 flag = TTY_PARITY;
Jiri Slaby1c456072008-02-07 00:16:46 -08002127 port->icount.parity++;
Jiri Slaby15517802021-06-18 08:14:18 +02002128 } else if (status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002129 flag = TTY_FRAME;
Jiri Slaby1c456072008-02-07 00:16:46 -08002130 port->icount.frame++;
Jiri Slaby15517802021-06-18 08:14:18 +02002131 } else if (status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002132 flag = TTY_OVERRUN;
Jiri Slaby1c456072008-02-07 00:16:46 -08002133 port->icount.overrun++;
2134 } else
2135 flag = TTY_BREAK;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002136 }
Jiri Slaby92a19f92013-01-03 15:53:03 +01002137 tty_insert_flip_char(&port->port, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 cnt++;
2139 if (cnt >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002140 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 break;
2143 }
2144
2145 }
2146
Jiri Slaby292955a2021-06-18 08:14:13 +02002147 if (port->board->must_hwid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Jiri Slaby15517802021-06-18 08:14:18 +02002150 status = inb(port->ioaddr + UART_LSR);
2151 } while (status & UART_LSR_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Jiri Slaby1c456072008-02-07 00:16:46 -08002153end_intr:
Alan Cox216ba022008-10-13 10:40:19 +01002154 mxvar_log.rxcnt[tty->index] += cnt;
Jiri Slaby1c456072008-02-07 00:16:46 -08002155 port->mon_data.rxcnt += cnt;
2156 port->mon_data.up_rxcnt += cnt;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002157
Jiri Slaby2e124b42013-01-03 15:53:06 +01002158 tty_flip_buffer_push(&port->port);
Jiri Slaby15517802021-06-18 08:14:18 +02002159
2160 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
Alan Cox216ba022008-10-13 10:40:19 +01002163static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 int count, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Jiri Slaby1c456072008-02-07 00:16:46 -08002167 if (port->x_char) {
2168 outb(port->x_char, port->ioaddr + UART_TX);
2169 port->x_char = 0;
Alan Cox216ba022008-10-13 10:40:19 +01002170 mxvar_log.txcnt[tty->index]++;
Jiri Slaby1c456072008-02-07 00:16:46 -08002171 port->mon_data.txcnt++;
2172 port->mon_data.up_txcnt++;
2173 port->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 return;
2175 }
2176
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002177 if (port->port.xmit_buf == NULL)
Jiri Slaby1c456072008-02-07 00:16:46 -08002178 return;
2179
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02002180 if (port->xmit_cnt <= 0 || tty->flow.stopped ||
Alan Cox216ba022008-10-13 10:40:19 +01002181 (tty->hw_stopped &&
Jiri Slaby1c456072008-02-07 00:16:46 -08002182 (port->type != PORT_16550A) &&
Jiri Slaby292955a2021-06-18 08:14:13 +02002183 (!port->board->must_hwid))) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002184 port->IER &= ~UART_IER_THRI;
2185 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return;
2187 }
2188
Jiri Slaby1c456072008-02-07 00:16:46 -08002189 cnt = port->xmit_cnt;
2190 count = port->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 do {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002192 outb(port->port.xmit_buf[port->xmit_tail++],
Jiri Slaby1c456072008-02-07 00:16:46 -08002193 port->ioaddr + UART_TX);
2194 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2195 if (--port->xmit_cnt <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 break;
2197 } while (--count > 0);
Alan Cox216ba022008-10-13 10:40:19 +01002198 mxvar_log.txcnt[tty->index] += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Jiri Slaby1c456072008-02-07 00:16:46 -08002200 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2201 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2202 port->icount.tx += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Kulikov Vasiliy464eb8f2010-07-23 20:34:53 +04002204 if (port->xmit_cnt < WAKEUP_CHARS)
Alan Cox216ba022008-10-13 10:40:19 +01002205 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Jiri Slaby1c456072008-02-07 00:16:46 -08002207 if (port->xmit_cnt <= 0) {
2208 port->IER &= ~UART_IER_THRI;
2209 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
2212
Jiri Slaby9e40ea12021-06-18 08:14:19 +02002213static bool mxser_port_isr(struct mxser_port *port)
2214{
2215 struct tty_struct *tty;
2216 u8 iir, msr, status;
2217 bool error = false;
2218
2219 iir = inb(port->ioaddr + UART_IIR);
2220 if (iir & UART_IIR_NO_INT)
2221 return true;
2222
2223 iir &= MOXA_MUST_IIR_MASK;
2224 tty = tty_port_tty_get(&port->port);
2225 if (!tty || port->closing || !tty_port_initialized(&port->port)) {
2226 status = inb(port->ioaddr + UART_LSR);
2227 outb(0x27, port->ioaddr + UART_FCR);
2228 inb(port->ioaddr + UART_MSR);
2229
2230 error = true;
2231 goto put_tty;
2232 }
2233
2234 status = inb(port->ioaddr + UART_LSR);
2235
2236 if (status & UART_LSR_PE)
2237 port->err_shadow |= NPPI_NOTIFY_PARITY;
2238 if (status & UART_LSR_FE)
2239 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2240 if (status & UART_LSR_OE)
2241 port->err_shadow |= NPPI_NOTIFY_HW_OVERRUN;
2242 if (status & UART_LSR_BI)
2243 port->err_shadow |= NPPI_NOTIFY_BREAK;
2244
2245 if (port->board->must_hwid) {
2246 if (iir == MOXA_MUST_IIR_GDA ||
2247 iir == MOXA_MUST_IIR_RDA ||
2248 iir == MOXA_MUST_IIR_RTO ||
2249 iir == MOXA_MUST_IIR_LSR)
2250 status = mxser_receive_chars(tty, port, status);
2251 } else {
2252 status &= port->read_status_mask;
2253 if (status & UART_LSR_DR)
2254 status = mxser_receive_chars(tty, port, status);
2255 }
2256
2257 msr = inb(port->ioaddr + UART_MSR);
2258 if (msr & UART_MSR_ANY_DELTA)
2259 mxser_check_modem_status(tty, port, msr);
2260
2261 if (port->board->must_hwid) {
2262 if (iir == 0x02 && (status & UART_LSR_THRE))
2263 mxser_transmit_chars(tty, port);
2264 } else {
2265 if (status & UART_LSR_THRE)
2266 mxser_transmit_chars(tty, port);
2267 }
2268
2269put_tty:
2270 tty_kref_put(tty);
2271
2272 return error;
2273}
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002276 * This is the serial driver's generic interrupt routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002278static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
Jiri Slabycef222c2021-06-18 08:14:17 +02002280 struct mxser_board *brd = dev_id;
Jiri Slaby1c456072008-02-07 00:16:46 -08002281 struct mxser_port *port;
Jiri Slaby1c456072008-02-07 00:16:46 -08002282 unsigned int int_cnt, pass_counter = 0;
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02002283 unsigned int i, max = brd->info->nports;
Jiri Slaby1c456072008-02-07 00:16:46 -08002284 int handled = IRQ_NONE;
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02002285 u8 irqbits, bits, mask = BIT(max) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Jiri Slaby1c456072008-02-07 00:16:46 -08002287 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02002288 irqbits = inb(brd->vector) & mask;
2289 if (irqbits == mask)
Jiri Slaby1c456072008-02-07 00:16:46 -08002290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Jiri Slaby1c456072008-02-07 00:16:46 -08002292 handled = IRQ_HANDLED;
2293 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02002294 if (irqbits == mask)
Jiri Slaby1c456072008-02-07 00:16:46 -08002295 break;
2296 if (bits & irqbits)
2297 continue;
2298 port = &brd->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Jiri Slaby1c456072008-02-07 00:16:46 -08002300 int_cnt = 0;
2301 spin_lock(&port->slock);
2302 do {
Jiri Slaby9e40ea12021-06-18 08:14:19 +02002303 if (mxser_port_isr(port))
Jiri Slaby1c456072008-02-07 00:16:46 -08002304 break;
Jiri Slaby1c456072008-02-07 00:16:46 -08002305 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2306 spin_unlock(&port->slock);
2307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309
Jiri Slaby1c456072008-02-07 00:16:46 -08002310 return handled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
Jiri Slaby1c456072008-02-07 00:16:46 -08002313static const struct tty_operations mxser_ops = {
2314 .open = mxser_open,
2315 .close = mxser_close,
2316 .write = mxser_write,
2317 .put_char = mxser_put_char,
2318 .flush_chars = mxser_flush_chars,
2319 .write_room = mxser_write_room,
2320 .chars_in_buffer = mxser_chars_in_buffer,
2321 .flush_buffer = mxser_flush_buffer,
2322 .ioctl = mxser_ioctl,
2323 .throttle = mxser_throttle,
2324 .unthrottle = mxser_unthrottle,
2325 .set_termios = mxser_set_termios,
2326 .stop = mxser_stop,
2327 .start = mxser_start,
2328 .hangup = mxser_hangup,
2329 .break_ctl = mxser_rs_break,
2330 .wait_until_sent = mxser_wait_until_sent,
2331 .tiocmget = mxser_tiocmget,
2332 .tiocmset = mxser_tiocmset,
Al Viro6da5b582018-09-11 22:22:06 -04002333 .set_serial = mxser_set_serial_info,
2334 .get_serial = mxser_get_serial_info,
Alan Cox05871022010-09-16 18:21:52 +01002335 .get_icount = mxser_get_icount,
Jiri Slaby1c456072008-02-07 00:16:46 -08002336};
2337
Aya Mahfouz04b757d2015-12-15 01:53:36 +02002338static const struct tty_port_operations mxser_port_ops = {
Alan Cox31f35932009-01-02 13:45:05 +00002339 .carrier_raised = mxser_carrier_raised,
Alan Coxfcc8ac12009-06-11 12:24:17 +01002340 .dtr_rts = mxser_dtr_rts,
Alan Cox67691402009-11-30 13:17:35 +00002341 .activate = mxser_activate,
2342 .shutdown = mxser_shutdown_port,
Alan Cox31f35932009-01-02 13:45:05 +00002343};
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002346 * The MOXA Smartio/Industio serial driver boot-time initialization code!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002348
Jiri Slaby27997072017-09-12 12:39:55 +02002349static int mxser_initbrd(struct mxser_board *brd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Jiri Slaby1c456072008-02-07 00:16:46 -08002351 struct mxser_port *info;
2352 unsigned int i;
2353 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Jiri Slaby1c456072008-02-07 00:16:46 -08002355 for (i = 0; i < brd->info->nports; i++) {
2356 info = &brd->ports[i];
Alan Cox44b7d1b2008-07-16 21:57:18 +01002357 tty_port_init(&info->port);
Alan Cox31f35932009-01-02 13:45:05 +00002358 info->port.ops = &mxser_port_ops;
Jiri Slaby1c456072008-02-07 00:16:46 -08002359 info->board = brd;
2360 info->stop_rx = 0;
2361 info->ldisc_stop_rx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Jiri Slaby1c456072008-02-07 00:16:46 -08002363 /* Enhance mode enabled here */
Jiri Slaby292955a2021-06-18 08:14:13 +02002364 if (brd->must_hwid != MOXA_OTHER_UART)
Christoph Hellwig148ff862008-04-30 00:54:29 -07002365 mxser_enable_must_enchance_mode(info->ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Jiri Slaby58a2ddb2021-06-18 08:14:15 +02002367 info->type = PORT_16550A;
Jiri Slaby1c456072008-02-07 00:16:46 -08002368
2369 process_txrx_fifo(info);
2370
2371 info->custom_divisor = info->baud_base * 16;
Alan Cox44b7d1b2008-07-16 21:57:18 +01002372 info->port.close_delay = 5 * HZ / 10;
2373 info->port.closing_wait = 30 * HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002374 info->normal_termios = mxvar_sdriver->init_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -08002375 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2376 info->err_shadow = 0;
2377 spin_lock_init(&info->slock);
2378
2379 /* before set INT ISR, disable all int */
2380 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2381 info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
2383
Jiri Slaby1c456072008-02-07 00:16:46 -08002384 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2385 brd);
Jiri Slaby191c5f12012-11-15 09:49:56 +01002386 if (retval) {
2387 for (i = 0; i < brd->info->nports; i++)
2388 tty_port_destroy(&brd->ports[i].port);
Jiri Slaby1c456072008-02-07 00:16:46 -08002389 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2390 "conflict with another device.\n",
2391 brd->info->name, brd->irq);
Jiri Slaby191c5f12012-11-15 09:49:56 +01002392 }
Jiri Slabydf480512010-09-03 10:31:37 +02002393
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002394 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
Jiri Slaby191c5f12012-11-15 09:49:56 +01002397static void mxser_board_remove(struct mxser_board *brd)
2398{
2399 unsigned int i;
2400
2401 for (i = 0; i < brd->info->nports; i++) {
2402 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2403 tty_port_destroy(&brd->ports[i].port);
2404 }
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002405 free_irq(brd->irq, brd);
Jiri Slaby191c5f12012-11-15 09:49:56 +01002406}
2407
Bill Pemberton9671f092012-11-19 13:21:50 -05002408static int mxser_probe(struct pci_dev *pdev,
Jiri Slaby1c456072008-02-07 00:16:46 -08002409 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
Jiri Slaby1c456072008-02-07 00:16:46 -08002411 struct mxser_board *brd;
2412 unsigned int i, j;
2413 unsigned long ioaddress;
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002414 struct device *tty_dev;
Jiri Slaby1c456072008-02-07 00:16:46 -08002415 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Jiri Slaby1c456072008-02-07 00:16:46 -08002417 for (i = 0; i < MXSER_BOARDS; i++)
2418 if (mxser_boards[i].info == NULL)
2419 break;
2420
2421 if (i >= MXSER_BOARDS) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002422 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
2423 "not configured\n", MXSER_BOARDS);
Jiri Slaby1c456072008-02-07 00:16:46 -08002424 goto err;
2425 }
2426
2427 brd = &mxser_boards[i];
2428 brd->idx = i * MXSER_PORTS_PER_BOARD;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002429 dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
Jiri Slaby1c456072008-02-07 00:16:46 -08002430 mxser_cards[ent->driver_data].name,
2431 pdev->bus->number, PCI_SLOT(pdev->devfn));
2432
2433 retval = pci_enable_device(pdev);
2434 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002435 dev_err(&pdev->dev, "PCI enable failed\n");
Jiri Slaby1c456072008-02-07 00:16:46 -08002436 goto err;
2437 }
2438
2439 /* io address */
2440 ioaddress = pci_resource_start(pdev, 2);
2441 retval = pci_request_region(pdev, 2, "mxser(IO)");
2442 if (retval)
Jiri Slabydf480512010-09-03 10:31:37 +02002443 goto err_dis;
Jiri Slaby1c456072008-02-07 00:16:46 -08002444
2445 brd->info = &mxser_cards[ent->driver_data];
2446 for (i = 0; i < brd->info->nports; i++)
2447 brd->ports[i].ioaddr = ioaddress + 8 * i;
2448
2449 /* vector */
2450 ioaddress = pci_resource_start(pdev, 3);
2451 retval = pci_request_region(pdev, 3, "mxser(vector)");
2452 if (retval)
Jiri Slabydf480512010-09-03 10:31:37 +02002453 goto err_zero;
Jiri Slaby1c456072008-02-07 00:16:46 -08002454 brd->vector = ioaddress;
2455
2456 /* irq */
2457 brd->irq = pdev->irq;
2458
Jiri Slaby292955a2021-06-18 08:14:13 +02002459 brd->must_hwid = mxser_must_get_hwid(brd->ports[0].ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -08002460
Jiri Slaby928f9462021-06-18 08:14:16 +02002461 for (j = 0; j < UART_INFO_NUM; j++) {
2462 if (Gpci_uart_info[j].type == brd->must_hwid) {
2463 brd->max_baud = Gpci_uart_info[j].max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -08002464
Jiri Slaby928f9462021-06-18 08:14:16 +02002465 /* exception....CP-102 */
2466 if (brd->info->flags & MXSER_HIGHBAUD)
2467 brd->max_baud = 921600;
2468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002470 }
2471
Jiri Slaby292955a2021-06-18 08:14:13 +02002472 if (brd->must_hwid == MOXA_MUST_MU860_HWID) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002473 for (i = 0; i < brd->info->nports; i++) {
2474 if (i < 4)
2475 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2476 else
2477 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002479 outb(0, ioaddress + 4); /* default set to RS232 mode */
2480 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002482
2483 for (i = 0; i < brd->info->nports; i++) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002484 brd->ports[i].baud_base = 921600;
2485 }
2486
2487 /* mxser_initbrd will hook ISR. */
Jiri Slaby27997072017-09-12 12:39:55 +02002488 retval = mxser_initbrd(brd);
Jiri Slaby1c456072008-02-07 00:16:46 -08002489 if (retval)
Jiri Slabydf480512010-09-03 10:31:37 +02002490 goto err_rel3;
Jiri Slaby1c456072008-02-07 00:16:46 -08002491
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002492 for (i = 0; i < brd->info->nports; i++) {
2493 tty_dev = tty_port_register_device(&brd->ports[i].port,
2494 mxvar_sdriver, brd->idx + i, &pdev->dev);
2495 if (IS_ERR(tty_dev)) {
2496 retval = PTR_ERR(tty_dev);
Alexey Khoroshilov1b581f12013-04-07 23:46:47 +04002497 for (; i > 0; i--)
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002498 tty_unregister_device(mxvar_sdriver,
Alexey Khoroshilov1b581f12013-04-07 23:46:47 +04002499 brd->idx + i - 1);
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002500 goto err_relbrd;
2501 }
2502 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002503
2504 pci_set_drvdata(pdev, brd);
2505
2506 return 0;
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002507err_relbrd:
2508 for (i = 0; i < brd->info->nports; i++)
2509 tty_port_destroy(&brd->ports[i].port);
2510 free_irq(brd->irq, brd);
Jiri Slabydf480512010-09-03 10:31:37 +02002511err_rel3:
2512 pci_release_region(pdev, 3);
2513err_zero:
Jiri Slaby1c456072008-02-07 00:16:46 -08002514 brd->info = NULL;
Jiri Slabydf480512010-09-03 10:31:37 +02002515 pci_release_region(pdev, 2);
2516err_dis:
2517 pci_disable_device(pdev);
Jiri Slaby1c456072008-02-07 00:16:46 -08002518err:
2519 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
Bill Pembertonae8d8a12012-11-19 13:26:18 -05002522static void mxser_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Jiri Slaby1c456072008-02-07 00:16:46 -08002524 struct mxser_board *brd = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Jiri Slaby191c5f12012-11-15 09:49:56 +01002526 mxser_board_remove(brd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Jiri Slabydf480512010-09-03 10:31:37 +02002528 pci_release_region(pdev, 2);
2529 pci_release_region(pdev, 3);
2530 pci_disable_device(pdev);
Jiri Slaby1c456072008-02-07 00:16:46 -08002531 brd->info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
Jiri Slaby1c456072008-02-07 00:16:46 -08002534static struct pci_driver mxser_driver = {
2535 .name = "mxser",
2536 .id_table = mxser_pcibrds,
2537 .probe = mxser_probe,
Bill Pemberton91116cb2012-11-19 13:21:06 -05002538 .remove = mxser_remove
Jiri Slaby1c456072008-02-07 00:16:46 -08002539};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Jiri Slaby1c456072008-02-07 00:16:46 -08002541static int __init mxser_module_init(void)
2542{
Jiri Slaby1df00922008-07-25 01:48:22 -07002543 int retval;
Jiri Slaby1c456072008-02-07 00:16:46 -08002544
Jiri Slaby1c456072008-02-07 00:16:46 -08002545 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2546 if (!mxvar_sdriver)
2547 return -ENOMEM;
2548
Jiri Slaby1c456072008-02-07 00:16:46 -08002549 /* Initialize the tty_driver structure */
Jiri Slaby1c456072008-02-07 00:16:46 -08002550 mxvar_sdriver->name = "ttyMI";
2551 mxvar_sdriver->major = ttymajor;
2552 mxvar_sdriver->minor_start = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002553 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2554 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2555 mxvar_sdriver->init_termios = tty_std_termios;
2556 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2557 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2558 tty_set_operations(mxvar_sdriver, &mxser_ops);
2559
2560 retval = tty_register_driver(mxvar_sdriver);
2561 if (retval) {
2562 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2563 "tty driver !\n");
2564 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002566
Jiri Slaby1c456072008-02-07 00:16:46 -08002567 retval = pci_register_driver(&mxser_driver);
2568 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002569 printk(KERN_ERR "mxser: can't register pci driver\n");
Jiri Slaby29134362021-06-18 08:14:07 +02002570 goto err_unr;
Jiri Slaby1c456072008-02-07 00:16:46 -08002571 }
2572
Jiri Slaby1c456072008-02-07 00:16:46 -08002573 return 0;
2574err_unr:
2575 tty_unregister_driver(mxvar_sdriver);
2576err_put:
2577 put_tty_driver(mxvar_sdriver);
2578 return retval;
2579}
2580
2581static void __exit mxser_module_exit(void)
2582{
Jiri Slaby1c456072008-02-07 00:16:46 -08002583 pci_unregister_driver(&mxser_driver);
Jiri Slaby1c456072008-02-07 00:16:46 -08002584 tty_unregister_driver(mxvar_sdriver);
2585 put_tty_driver(mxvar_sdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
2587
2588module_init(mxser_module_init);
2589module_exit(mxser_module_exit);