blob: 93a95a135a71ae2049c59f8fc754eead2831b97b [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
4 *
Jiri Slaby80ff8a82008-02-07 00:16:51 -08005 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
6 * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Jiri Slaby1c456072008-02-07 00:16:46 -08008 * This code is loosely based on the 1.8 moxa driver which is based on
9 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
10 * others.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
Alan Cox8eb04cf2008-11-11 14:48:44 +000013 * <alan@lxorguk.ukuu.org.uk>. The original 1.8 code is available on
14 * www.moxa.com.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * - Fixed x86_64 cleanness
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/errno.h>
20#include <linux/signal.h>
21#include <linux/sched.h>
22#include <linux/timer.h>
23#include <linux/interrupt.h>
24#include <linux/tty.h>
25#include <linux/tty_flip.h>
26#include <linux/serial.h>
27#include <linux/serial_reg.h>
28#include <linux/major.h>
29#include <linux/string.h>
30#include <linux/fcntl.h>
31#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/ioport.h>
33#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/delay.h>
35#include <linux/pci.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070036#include <linux/bitops.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Manuel Zerpies5a3c6b2512011-06-16 14:07:22 +020038#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/io.h>
41#include <asm/irq.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080042#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jiri Slaby4463cc52021-06-18 08:14:10 +020044/*
45 * Semi-public control interfaces
46 */
47
48/*
49 * MOXA ioctls
50 */
51
52#define MOXA 0x400
Jiri Slaby4463cc52021-06-18 08:14:10 +020053#define MOXA_SET_OP_MODE (MOXA + 66)
54#define MOXA_GET_OP_MODE (MOXA + 67)
55
56#define RS232_MODE 0
57#define RS485_2WIRE_MODE 1
58#define RS422_MODE 2
59#define RS485_4WIRE_MODE 3
60#define OP_MODE_MASK 3
61
Jiri Slaby4463cc52021-06-18 08:14:10 +020062/* --------------------------------------------------- */
63
Jiri Slaby4463cc52021-06-18 08:14:10 +020064/*
65 * Follow just what Moxa Must chip defines.
66 *
Jiri Slaby464fbf62021-06-18 08:15:08 +020067 * When LCR register (offset 0x03) is written the following value, the Must chip
68 * will enter enhanced mode. And a write to EFR (offset 0x02) bit 6,7 will
Jiri Slaby4463cc52021-06-18 08:14:10 +020069 * change bank.
70 */
Jiri Slaby464fbf62021-06-18 08:15:08 +020071#define MOXA_MUST_ENTER_ENHANCED 0xBF
Jiri Slaby4463cc52021-06-18 08:14:10 +020072
Jiri Slaby464fbf62021-06-18 08:15:08 +020073/* when enhanced mode is enabled, access to general bank register */
Jiri Slaby4463cc52021-06-18 08:14:10 +020074#define MOXA_MUST_GDL_REGISTER 0x07
75#define MOXA_MUST_GDL_MASK 0x7F
76#define MOXA_MUST_GDL_HAS_BAD_DATA 0x80
77
78#define MOXA_MUST_LSR_RERR 0x80 /* error in receive FIFO */
Jiri Slaby464fbf62021-06-18 08:15:08 +020079/* enhanced register bank select and enhanced mode setting register */
80/* This works only when LCR register equals to 0xBF */
Jiri Slaby4463cc52021-06-18 08:14:10 +020081#define MOXA_MUST_EFR_REGISTER 0x02
Jiri Slaby464fbf62021-06-18 08:15:08 +020082#define MOXA_MUST_EFR_EFRB_ENABLE 0x10 /* enhanced mode enable */
83/* enhanced register bank set 0, 1, 2 */
Jiri Slaby4463cc52021-06-18 08:14:10 +020084#define MOXA_MUST_EFR_BANK0 0x00
85#define MOXA_MUST_EFR_BANK1 0x40
86#define MOXA_MUST_EFR_BANK2 0x80
87#define MOXA_MUST_EFR_BANK3 0xC0
88#define MOXA_MUST_EFR_BANK_MASK 0xC0
89
90/* set XON1 value register, when LCR=0xBF and change to bank0 */
91#define MOXA_MUST_XON1_REGISTER 0x04
92
93/* set XON2 value register, when LCR=0xBF and change to bank0 */
94#define MOXA_MUST_XON2_REGISTER 0x05
95
96/* set XOFF1 value register, when LCR=0xBF and change to bank0 */
97#define MOXA_MUST_XOFF1_REGISTER 0x06
98
99/* set XOFF2 value register, when LCR=0xBF and change to bank0 */
100#define MOXA_MUST_XOFF2_REGISTER 0x07
101
102#define MOXA_MUST_RBRTL_REGISTER 0x04
103#define MOXA_MUST_RBRTH_REGISTER 0x05
104#define MOXA_MUST_RBRTI_REGISTER 0x06
105#define MOXA_MUST_THRTL_REGISTER 0x07
106#define MOXA_MUST_ENUM_REGISTER 0x04
107#define MOXA_MUST_HWID_REGISTER 0x05
108#define MOXA_MUST_ECR_REGISTER 0x06
109#define MOXA_MUST_CSR_REGISTER 0x07
110
111#define MOXA_MUST_FCR_GDA_MODE_ENABLE 0x20 /* good data mode enable */
112#define MOXA_MUST_FCR_GDA_ONLY_ENABLE 0x10 /* only good data put into RxFIFO */
113
114#define MOXA_MUST_IER_ECTSI 0x80 /* enable CTS interrupt */
115#define MOXA_MUST_IER_ERTSI 0x40 /* enable RTS interrupt */
116#define MOXA_MUST_IER_XINT 0x20 /* enable Xon/Xoff interrupt */
117#define MOXA_MUST_IER_EGDAI 0x10 /* enable GDA interrupt */
118
119#define MOXA_MUST_RECV_ISR (UART_IER_RDI | MOXA_MUST_IER_EGDAI)
120
121/* GDA interrupt pending */
122#define MOXA_MUST_IIR_GDA 0x1C
123#define MOXA_MUST_IIR_RDA 0x04
124#define MOXA_MUST_IIR_RTO 0x0C
125#define MOXA_MUST_IIR_LSR 0x06
126
127/* received Xon/Xoff or specical interrupt pending */
128#define MOXA_MUST_IIR_XSC 0x10
129
130/* RTS/CTS change state interrupt pending */
131#define MOXA_MUST_IIR_RTSCTS 0x20
132#define MOXA_MUST_IIR_MASK 0x3E
133
134#define MOXA_MUST_MCR_XON_FLAG 0x40
135#define MOXA_MUST_MCR_XON_ANY 0x80
136#define MOXA_MUST_MCR_TX_XON 0x08
137
138#define MOXA_MUST_EFR_SF_MASK 0x0F /* software flow control on chip mask value */
139#define MOXA_MUST_EFR_SF_TX1 0x08 /* send Xon1/Xoff1 */
140#define MOXA_MUST_EFR_SF_TX2 0x04 /* send Xon2/Xoff2 */
141#define MOXA_MUST_EFR_SF_TX12 0x0C /* send Xon1,Xon2/Xoff1,Xoff2 */
142#define MOXA_MUST_EFR_SF_TX_NO 0x00 /* don't send Xon/Xoff */
143#define MOXA_MUST_EFR_SF_TX_MASK 0x0C /* Tx software flow control mask */
144#define MOXA_MUST_EFR_SF_RX_NO 0x00 /* don't receive Xon/Xoff */
145#define MOXA_MUST_EFR_SF_RX1 0x02 /* receive Xon1/Xoff1 */
146#define MOXA_MUST_EFR_SF_RX2 0x01 /* receive Xon2/Xoff2 */
147#define MOXA_MUST_EFR_SF_RX12 0x03 /* receive Xon1,Xon2/Xoff1,Xoff2 */
148#define MOXA_MUST_EFR_SF_RX_MASK 0x03 /* Rx software flow control mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#define MXSERMAJOR 174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define MXSER_BOARDS 4 /* Max. boards */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slaby1c456072008-02-07 00:16:46 -0800154#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
155#define MXSER_ISR_PASS_LIMIT 100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define WAKEUP_CHARS 256
158
Jiri Slabya6970c32021-06-18 08:14:41 +0200159#define MXSER_BAUD_BASE 921600
Jiri Slabyd811b262021-06-18 08:14:42 +0200160#define MXSER_CUSTOM_DIVISOR (MXSER_BAUD_BASE * 16)
Jiri Slabya6970c32021-06-18 08:14:41 +0200161
Jiri Slabye129def2008-07-22 11:20:34 +0100162#define PCI_DEVICE_ID_POS104UL 0x1044
Jiri Slaby1c456072008-02-07 00:16:46 -0800163#define PCI_DEVICE_ID_CB108 0x1080
Jiri Slabye129def2008-07-22 11:20:34 +0100164#define PCI_DEVICE_ID_CP102UF 0x1023
Jiri Slaby502f2952009-09-10 12:20:07 +0200165#define PCI_DEVICE_ID_CP112UL 0x1120
Jiri Slaby1c456072008-02-07 00:16:46 -0800166#define PCI_DEVICE_ID_CB114 0x1142
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800167#define PCI_DEVICE_ID_CP114UL 0x1143
Jiri Slaby1c456072008-02-07 00:16:46 -0800168#define PCI_DEVICE_ID_CB134I 0x1341
169#define PCI_DEVICE_ID_CP138U 0x1380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Jiri Slabyc24c31f2021-06-18 08:14:54 +0200171#define MXSER_NPORTS(ddata) ((ddata) & 0xffU)
172#define MXSER_HIGHBAUD 0x0100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Jiri Slabye4558362021-06-18 08:14:14 +0200174enum mxser_must_hwid {
175 MOXA_OTHER_UART = 0x00,
176 MOXA_MUST_MU150_HWID = 0x01,
177 MOXA_MUST_MU860_HWID = 0x02,
178};
179
Jiri Slaby1c456072008-02-07 00:16:46 -0800180static const struct {
Jiri Slabydc33f642021-06-18 08:14:11 +0200181 u8 type;
182 u8 fifo_size;
183 u8 rx_high_water;
184 u8 rx_low_water;
185 speed_t max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800186} Gpci_uart_info[] = {
Jiri Slabydc33f642021-06-18 08:14:11 +0200187 { MOXA_OTHER_UART, 16, 14, 1, 921600 },
188 { MOXA_MUST_MU150_HWID, 64, 48, 16, 230400 },
189 { MOXA_MUST_MU860_HWID, 128, 96, 32, 921600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190};
Jiri Slaby1c456072008-02-07 00:16:46 -0800191#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Jiri Slaby1c456072008-02-07 00:16:46 -0800193
194/* driver_data correspond to the lines in the structure above
195 see also ISA probe function before you change something */
Arvind Yadav3385ecf2017-07-23 15:31:01 +0530196static const struct pci_device_id mxser_pcibrds[] = {
Jiri Slabyc24c31f2021-06-18 08:14:54 +0200197 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 8 },
198 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
Jiri Slaby15254902021-06-18 08:14:08 +0200199 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 2 },
Jiri Slabyc24c31f2021-06-18 08:14:54 +0200200 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 4 },
Jiri Slaby15254902021-06-18 08:14:08 +0200201 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 4 },
Jiri Slabyc24c31f2021-06-18 08:14:54 +0200202 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 2 | MXSER_HIGHBAUD },
203 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 4 },
204 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 8 },
205 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 2 },
206 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 4 },
207 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 4 },
208 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 8 }, /* RC7000 */
209 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 8 },
210 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 2 },
211 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 2 },
212 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 8 },
213 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 8 },
214 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 4 },
215 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 8 },
216 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 4 },
217 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 4 },
218 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 8 },
219 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 4 },
220 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 4 },
221 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 2 },
222 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP112UL), .driver_data = 2 },
Jiri Slaby1c456072008-02-07 00:16:46 -0800223 { }
224};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static int ttymajor = MXSERMAJOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229/* Variables for insmod */
230
231MODULE_AUTHOR("Casper Yang");
232MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800233module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234MODULE_LICENSE("GPL");
235
Jiri Slaby1c456072008-02-07 00:16:46 -0800236struct mxser_board;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Jiri Slaby1c456072008-02-07 00:16:46 -0800238struct mxser_port {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100239 struct tty_port port;
Jiri Slaby1c456072008-02-07 00:16:46 -0800240 struct mxser_board *board;
Jiri Slaby1c456072008-02-07 00:16:46 -0800241
242 unsigned long ioaddr;
243 unsigned long opmode_ioaddr;
Jiri Slaby1c456072008-02-07 00:16:46 -0800244
Jiri Slabydc33f642021-06-18 08:14:11 +0200245 u8 rx_high_water;
246 u8 rx_low_water;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int type; /* UART type */
Jiri Slaby1c456072008-02-07 00:16:46 -0800248
Jiri Slabya93963e2021-06-18 08:14:24 +0200249 unsigned char x_char; /* xon/xoff character */
250 u8 IER; /* Interrupt Enable Register */
251 u8 MCR; /* Modem control register */
Jiri Slabyd249e662021-09-22 09:59:38 +0200252 u8 FCR; /* FIFO control register */
Jiri Slaby1c456072008-02-07 00:16:46 -0800253
Jiri Slaby19236282021-09-22 09:59:34 +0200254 bool ldisc_stop_rx;
Jiri Slaby1c456072008-02-07 00:16:46 -0800255
Jiri Slaby1c456072008-02-07 00:16:46 -0800256 struct async_icount icount; /* kernel counters for 4 input interrupts */
Jiri Slaby104583b2017-09-12 12:39:54 +0200257 unsigned int timeout;
Jiri Slaby1c456072008-02-07 00:16:46 -0800258
Jiri Slabya93963e2021-06-18 08:14:24 +0200259 u8 read_status_mask;
260 u8 ignore_status_mask;
Jiri Slabydc33f642021-06-18 08:14:11 +0200261 u8 xmit_fifo_size;
Jiri Slaby02e43142021-06-18 08:14:25 +0200262 unsigned int xmit_head;
263 unsigned int xmit_tail;
264 unsigned int xmit_cnt;
Peter Hurleycd7b4b32016-01-10 14:51:38 -0800265 int closing;
Jiri Slaby1c456072008-02-07 00:16:46 -0800266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 spinlock_t slock;
Jiri Slaby1c456072008-02-07 00:16:46 -0800268};
269
270struct mxser_board {
271 unsigned int idx;
Jiri Slabyc24c31f2021-06-18 08:14:54 +0200272 unsigned short nports;
Jiri Slaby1c456072008-02-07 00:16:46 -0800273 int irq;
Jiri Slaby1c456072008-02-07 00:16:46 -0800274 unsigned long vector;
Jiri Slaby1c456072008-02-07 00:16:46 -0800275
Jiri Slabye4558362021-06-18 08:14:14 +0200276 enum mxser_must_hwid must_hwid;
Jiri Slaby928f9462021-06-18 08:14:16 +0200277 speed_t max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800278
Jiri Slabyad1c92f2021-06-18 08:14:59 +0200279 struct mxser_port ports[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280};
281
Jiri Slabyf8b6b322021-06-18 08:14:58 +0200282static DECLARE_BITMAP(mxser_boards, MXSER_BOARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283static struct tty_driver *mxvar_sdriver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Jiri Slabyedb7d272021-06-18 08:14:46 +0200285static u8 __mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set,
286 bool restore_LCR)
Christoph Hellwig148ff862008-04-30 00:54:29 -0700287{
Jiri Slabyedb7d272021-06-18 08:14:46 +0200288 u8 oldlcr, efr;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700289
290 oldlcr = inb(baseio + UART_LCR);
Jiri Slaby464fbf62021-06-18 08:15:08 +0200291 outb(MOXA_MUST_ENTER_ENHANCED, baseio + UART_LCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700292
293 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
Jiri Slabyedb7d272021-06-18 08:14:46 +0200294 efr &= ~clear;
295 efr |= set;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700296
297 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700298
Jiri Slabyedb7d272021-06-18 08:14:46 +0200299 if (restore_LCR)
300 outb(oldlcr, baseio + UART_LCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700301
Jiri Slabyedb7d272021-06-18 08:14:46 +0200302 return oldlcr;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700303}
304
Jiri Slabyb2864842021-06-18 08:14:47 +0200305static u8 mxser_must_select_bank(unsigned long baseio, u8 bank)
306{
307 return __mxser_must_set_EFR(baseio, MOXA_MUST_EFR_BANK_MASK, bank,
308 false);
309}
310
Christoph Hellwig148ff862008-04-30 00:54:29 -0700311static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
312{
Jiri Slabyb2864842021-06-18 08:14:47 +0200313 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK0);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700314 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
315 outb(oldlcr, baseio + UART_LCR);
316}
317
318static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
319{
Jiri Slabyb2864842021-06-18 08:14:47 +0200320 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK0);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700321 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
322 outb(oldlcr, baseio + UART_LCR);
323}
324
325static void mxser_set_must_fifo_value(struct mxser_port *info)
326{
Jiri Slabyb2864842021-06-18 08:14:47 +0200327 u8 oldlcr = mxser_must_select_bank(info->ioaddr, MOXA_MUST_EFR_BANK1);
Jiri Slabydc33f642021-06-18 08:14:11 +0200328 outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
329 outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
330 outb(info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700331 outb(oldlcr, info->ioaddr + UART_LCR);
332}
333
334static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
335{
Jiri Slabyb2864842021-06-18 08:14:47 +0200336 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK2);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700337 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
338 outb(oldlcr, baseio + UART_LCR);
339}
340
Jiri Slabyb2864842021-06-18 08:14:47 +0200341static u8 mxser_get_must_hardware_id(unsigned long baseio)
Christoph Hellwig148ff862008-04-30 00:54:29 -0700342{
Jiri Slabyb2864842021-06-18 08:14:47 +0200343 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK2);
344 u8 id = inb(baseio + MOXA_MUST_HWID_REGISTER);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700345 outb(oldlcr, baseio + UART_LCR);
Jiri Slabyb2864842021-06-18 08:14:47 +0200346
347 return id;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700348}
349
Jiri Slabyedb7d272021-06-18 08:14:46 +0200350static void mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set)
351{
352 __mxser_must_set_EFR(baseio, clear, set, true);
353}
354
355static void mxser_must_set_enhance_mode(unsigned long baseio, bool enable)
356{
357 mxser_must_set_EFR(baseio,
358 enable ? 0 : MOXA_MUST_EFR_EFRB_ENABLE,
359 enable ? MOXA_MUST_EFR_EFRB_ENABLE : 0);
360}
361
Jiri Slabyb441eb02021-06-18 08:14:48 +0200362static void mxser_must_no_sw_flow_control(unsigned long baseio)
Christoph Hellwig148ff862008-04-30 00:54:29 -0700363{
Jiri Slabyb441eb02021-06-18 08:14:48 +0200364 mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_MASK, 0);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700365}
366
Jiri Slabyb441eb02021-06-18 08:14:48 +0200367static void mxser_must_set_tx_sw_flow_control(unsigned long baseio, bool enable)
Christoph Hellwig148ff862008-04-30 00:54:29 -0700368{
Jiri Slabyb441eb02021-06-18 08:14:48 +0200369 mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_TX_MASK,
370 enable ? MOXA_MUST_EFR_SF_TX1 : 0);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700371}
372
Jiri Slabyb441eb02021-06-18 08:14:48 +0200373static void mxser_must_set_rx_sw_flow_control(unsigned long baseio, bool enable)
Christoph Hellwig148ff862008-04-30 00:54:29 -0700374{
Jiri Slabyb441eb02021-06-18 08:14:48 +0200375 mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_RX_MASK,
376 enable ? MOXA_MUST_EFR_SF_RX1 : 0);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700377}
378
Jiri Slabye4558362021-06-18 08:14:14 +0200379static enum mxser_must_hwid mxser_must_get_hwid(unsigned long io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 u8 oldmcr, hwid;
382 int i;
383
384 outb(0, io + UART_LCR);
Jiri Slabyedb7d272021-06-18 08:14:46 +0200385 mxser_must_set_enhance_mode(io, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 oldmcr = inb(io + UART_MCR);
387 outb(0, io + UART_MCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700388 mxser_set_must_xon1_value(io, 0x11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if ((hwid = inb(io + UART_MCR)) != 0) {
390 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700391 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
Jiri Slabyb2864842021-06-18 08:14:47 +0200394 hwid = mxser_get_must_hardware_id(io);
Jiri Slabye4558362021-06-18 08:14:14 +0200395 for (i = 1; i < UART_INFO_NUM; i++) /* 0 = OTHER_UART */
Jiri Slaby1c456072008-02-07 00:16:46 -0800396 if (hwid == Gpci_uart_info[i].type)
Jiri Slabye4558362021-06-18 08:14:14 +0200397 return hwid;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return MOXA_OTHER_UART;
400}
401
Jiri Slaby5d1ea1a2021-06-18 08:15:12 +0200402static bool mxser_16550A_or_MUST(struct mxser_port *info)
403{
404 return info->type == PORT_16550A || info->board->must_hwid;
405}
406
Jiri Slabyc3db20c2021-06-18 08:14:55 +0200407static void mxser_process_txrx_fifo(struct mxser_port *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Jiri Slabyc3db20c2021-06-18 08:14:55 +0200409 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Jiri Slabyc3db20c2021-06-18 08:14:55 +0200411 if (info->type == PORT_16450 || info->type == PORT_8250) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 info->rx_high_water = 1;
413 info->rx_low_water = 1;
414 info->xmit_fifo_size = 1;
Jiri Slabyc3db20c2021-06-18 08:14:55 +0200415 return;
416 }
417
418 for (i = 0; i < UART_INFO_NUM; i++)
419 if (info->board->must_hwid == Gpci_uart_info[i].type) {
420 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
421 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
422 info->xmit_fifo_size = Gpci_uart_info[i].fifo_size;
423 break;
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Jiri Slaby740165f2021-06-18 08:15:05 +0200427static void __mxser_start_tx(struct mxser_port *info)
428{
429 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
430 info->IER |= UART_IER_THRI;
431 outb(info->IER, info->ioaddr + UART_IER);
432}
433
434static void mxser_start_tx(struct mxser_port *info)
435{
436 unsigned long flags;
437
438 spin_lock_irqsave(&info->slock, flags);
439 __mxser_start_tx(info);
440 spin_unlock_irqrestore(&info->slock, flags);
441}
442
443static void __mxser_stop_tx(struct mxser_port *info)
444{
445 info->IER &= ~UART_IER_THRI;
446 outb(info->IER, info->ioaddr + UART_IER);
447}
448
Alan Cox31f35932009-01-02 13:45:05 +0000449static int mxser_carrier_raised(struct tty_port *port)
450{
451 struct mxser_port *mp = container_of(port, struct mxser_port, port);
452 return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
453}
454
Alan Coxfcc8ac12009-06-11 12:24:17 +0100455static void mxser_dtr_rts(struct tty_port *port, int on)
Alan Cox5d951fb2009-01-02 13:45:19 +0000456{
457 struct mxser_port *mp = container_of(port, struct mxser_port, port);
458 unsigned long flags;
Jiri Slaby007bbdc2021-06-18 08:15:02 +0200459 u8 mcr;
Alan Cox5d951fb2009-01-02 13:45:19 +0000460
461 spin_lock_irqsave(&mp->slock, flags);
Jiri Slaby007bbdc2021-06-18 08:15:02 +0200462 mcr = inb(mp->ioaddr + UART_MCR);
Alan Coxfcc8ac12009-06-11 12:24:17 +0100463 if (on)
Jiri Slaby007bbdc2021-06-18 08:15:02 +0200464 mcr |= UART_MCR_DTR | UART_MCR_RTS;
Alan Coxfcc8ac12009-06-11 12:24:17 +0100465 else
Jiri Slaby007bbdc2021-06-18 08:15:02 +0200466 mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
467 outb(mcr, mp->ioaddr + UART_MCR);
Alan Cox5d951fb2009-01-02 13:45:19 +0000468 spin_unlock_irqrestore(&mp->slock, flags);
469}
470
Jiri Slabydc33f642021-06-18 08:14:11 +0200471static int mxser_set_baud(struct tty_struct *tty, speed_t newspd)
Jiri Slaby1c456072008-02-07 00:16:46 -0800472{
Alan Cox216ba022008-10-13 10:40:19 +0100473 struct mxser_port *info = tty->driver_data;
Jiri Slaby104583b2017-09-12 12:39:54 +0200474 unsigned int quot = 0, baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800475 unsigned char cval;
Jiri Slaby104583b2017-09-12 12:39:54 +0200476 u64 timeout;
Jiri Slaby1c456072008-02-07 00:16:46 -0800477
Jiri Slaby928f9462021-06-18 08:14:16 +0200478 if (newspd > info->board->max_baud)
Jiri Slaby1c456072008-02-07 00:16:46 -0800479 return -1;
480
481 if (newspd == 134) {
Jiri Slabya6970c32021-06-18 08:14:41 +0200482 quot = 2 * MXSER_BAUD_BASE / 269;
Alan Cox216ba022008-10-13 10:40:19 +0100483 tty_encode_baud_rate(tty, 134, 134);
Jiri Slaby1c456072008-02-07 00:16:46 -0800484 } else if (newspd) {
Jiri Slabya6970c32021-06-18 08:14:41 +0200485 quot = MXSER_BAUD_BASE / newspd;
Jiri Slaby1c456072008-02-07 00:16:46 -0800486 if (quot == 0)
487 quot = 1;
Jiri Slabya6970c32021-06-18 08:14:41 +0200488 baud = MXSER_BAUD_BASE / quot;
Alan Cox216ba022008-10-13 10:40:19 +0100489 tty_encode_baud_rate(tty, baud, baud);
Jiri Slaby1c456072008-02-07 00:16:46 -0800490 } else {
491 quot = 0;
492 }
493
Jiri Slaby104583b2017-09-12 12:39:54 +0200494 /*
495 * worst case (128 * 1000 * 10 * 18432) needs 35 bits, so divide in the
496 * u64 domain
497 */
498 timeout = (u64)info->xmit_fifo_size * HZ * 10 * quot;
Jiri Slabya6970c32021-06-18 08:14:41 +0200499 do_div(timeout, MXSER_BAUD_BASE);
Jiri Slaby104583b2017-09-12 12:39:54 +0200500 info->timeout = timeout + HZ / 50; /* Add .02 seconds of slop */
Jiri Slaby1c456072008-02-07 00:16:46 -0800501
502 if (quot) {
503 info->MCR |= UART_MCR_DTR;
504 outb(info->MCR, info->ioaddr + UART_MCR);
505 } else {
506 info->MCR &= ~UART_MCR_DTR;
507 outb(info->MCR, info->ioaddr + UART_MCR);
508 return 0;
509 }
510
511 cval = inb(info->ioaddr + UART_LCR);
512
513 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
514
515 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
516 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
517 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
518
519#ifdef BOTHER
Alan Cox216ba022008-10-13 10:40:19 +0100520 if (C_BAUD(tty) == BOTHER) {
Jiri Slabya6970c32021-06-18 08:14:41 +0200521 quot = MXSER_BAUD_BASE % newspd;
Jiri Slaby1c456072008-02-07 00:16:46 -0800522 quot *= 8;
523 if (quot % newspd > newspd / 2) {
524 quot /= newspd;
525 quot++;
526 } else
527 quot /= newspd;
528
Christoph Hellwig148ff862008-04-30 00:54:29 -0700529 mxser_set_must_enum_value(info->ioaddr, quot);
Jiri Slaby1c456072008-02-07 00:16:46 -0800530 } else
531#endif
Christoph Hellwig148ff862008-04-30 00:54:29 -0700532 mxser_set_must_enum_value(info->ioaddr, 0);
Jiri Slaby1c456072008-02-07 00:16:46 -0800533
534 return 0;
535}
536
Jiri Slabybe486662021-06-18 08:15:04 +0200537static void mxser_handle_cts(struct tty_struct *tty, struct mxser_port *info,
538 u8 msr)
539{
540 bool cts = msr & UART_MSR_CTS;
541
542 if (tty->hw_stopped) {
543 if (cts) {
544 tty->hw_stopped = 0;
545
Jiri Slaby5d1ea1a2021-06-18 08:15:12 +0200546 if (!mxser_16550A_or_MUST(info))
Jiri Slaby740165f2021-06-18 08:15:05 +0200547 __mxser_start_tx(info);
Jiri Slabybe486662021-06-18 08:15:04 +0200548 tty_wakeup(tty);
549 }
550 return;
551 } else if (cts)
552 return;
553
554 tty->hw_stopped = 1;
Jiri Slaby5d1ea1a2021-06-18 08:15:12 +0200555 if (!mxser_16550A_or_MUST(info))
Jiri Slaby740165f2021-06-18 08:15:05 +0200556 __mxser_stop_tx(info);
Jiri Slabybe486662021-06-18 08:15:04 +0200557}
558
Jiri Slaby1c456072008-02-07 00:16:46 -0800559/*
560 * This routine is called to set the UART divisor registers to match
561 * the specified baud rate for a serial port.
562 */
Jiri Slaby3fdfa162021-09-22 09:59:32 +0200563static void mxser_change_speed(struct tty_struct *tty, struct ktermios *old_termios)
Jiri Slaby1c456072008-02-07 00:16:46 -0800564{
Alan Cox216ba022008-10-13 10:40:19 +0100565 struct mxser_port *info = tty->driver_data;
Jiri Slabyd249e662021-09-22 09:59:38 +0200566 unsigned cflag, cval;
Jiri Slaby1c456072008-02-07 00:16:46 -0800567
Alan Coxadc8d742012-07-14 15:31:47 +0100568 cflag = tty->termios.c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -0800569
Jiri Slaby3fdfa162021-09-22 09:59:32 +0200570 if (mxser_set_baud(tty, tty_get_baud_rate(tty))) {
571 /* Use previous rate on a failure */
572 if (old_termios) {
573 speed_t baud = tty_termios_baud_rate(old_termios);
574 tty_encode_baud_rate(tty, baud, baud);
575 }
576 }
Jiri Slaby1c456072008-02-07 00:16:46 -0800577
578 /* byte size and parity */
579 switch (cflag & CSIZE) {
Jiri Slaby2c218322021-06-18 08:14:43 +0200580 default:
Jiri Slaby1c456072008-02-07 00:16:46 -0800581 case CS5:
Jiri Slaby2c218322021-06-18 08:14:43 +0200582 cval = UART_LCR_WLEN5;
Jiri Slaby1c456072008-02-07 00:16:46 -0800583 break;
584 case CS6:
Jiri Slaby2c218322021-06-18 08:14:43 +0200585 cval = UART_LCR_WLEN6;
Jiri Slaby1c456072008-02-07 00:16:46 -0800586 break;
587 case CS7:
Jiri Slaby2c218322021-06-18 08:14:43 +0200588 cval = UART_LCR_WLEN7;
Jiri Slaby1c456072008-02-07 00:16:46 -0800589 break;
590 case CS8:
Jiri Slaby2c218322021-06-18 08:14:43 +0200591 cval = UART_LCR_WLEN8;
Jiri Slaby1c456072008-02-07 00:16:46 -0800592 break;
Jiri Slaby1c456072008-02-07 00:16:46 -0800593 }
Jiri Slaby2c218322021-06-18 08:14:43 +0200594
Jiri Slaby1c456072008-02-07 00:16:46 -0800595 if (cflag & CSTOPB)
Jiri Slaby2c218322021-06-18 08:14:43 +0200596 cval |= UART_LCR_STOP;
Jiri Slaby1c456072008-02-07 00:16:46 -0800597 if (cflag & PARENB)
598 cval |= UART_LCR_PARITY;
599 if (!(cflag & PARODD))
600 cval |= UART_LCR_EPAR;
601 if (cflag & CMSPAR)
602 cval |= UART_LCR_SPAR;
603
Jiri Slabyd249e662021-09-22 09:59:38 +0200604 info->FCR = 0;
Jiri Slabybf1434c2021-09-22 09:59:35 +0200605 if (info->board->must_hwid) {
Jiri Slabyd249e662021-09-22 09:59:38 +0200606 info->FCR |= UART_FCR_ENABLE_FIFO |
Jiri Slabybf1434c2021-09-22 09:59:35 +0200607 MOXA_MUST_FCR_GDA_MODE_ENABLE;
608 mxser_set_must_fifo_value(info);
609 } else if (info->type != PORT_8250 && info->type != PORT_16450) {
Jiri Slabyd249e662021-09-22 09:59:38 +0200610 info->FCR |= UART_FCR_ENABLE_FIFO;
Jiri Slabybf1434c2021-09-22 09:59:35 +0200611 switch (info->rx_high_water) {
612 case 1:
Jiri Slabyd249e662021-09-22 09:59:38 +0200613 info->FCR |= UART_FCR_TRIGGER_1;
Jiri Slabybf1434c2021-09-22 09:59:35 +0200614 break;
615 case 4:
Jiri Slabyd249e662021-09-22 09:59:38 +0200616 info->FCR |= UART_FCR_TRIGGER_4;
Jiri Slabybf1434c2021-09-22 09:59:35 +0200617 break;
618 case 8:
Jiri Slabyd249e662021-09-22 09:59:38 +0200619 info->FCR |= UART_FCR_TRIGGER_8;
Jiri Slabybf1434c2021-09-22 09:59:35 +0200620 break;
621 default:
Jiri Slabyd249e662021-09-22 09:59:38 +0200622 info->FCR |= UART_FCR_TRIGGER_14;
Jiri Slabybf1434c2021-09-22 09:59:35 +0200623 break;
Jiri Slaby1c456072008-02-07 00:16:46 -0800624 }
625 }
626
627 /* CTS flow control flag and modem status interrupts */
628 info->IER &= ~UART_IER_MSI;
629 info->MCR &= ~UART_MCR_AFE;
Peter Hurley5604a982016-04-09 17:53:21 -0700630 tty_port_set_cts_flow(&info->port, cflag & CRTSCTS);
Jiri Slaby1c456072008-02-07 00:16:46 -0800631 if (cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800632 info->IER |= UART_IER_MSI;
Jiri Slaby5d1ea1a2021-06-18 08:15:12 +0200633 if (mxser_16550A_or_MUST(info)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800634 info->MCR |= UART_MCR_AFE;
635 } else {
Jiri Slabybe486662021-06-18 08:15:04 +0200636 mxser_handle_cts(tty, info,
637 inb(info->ioaddr + UART_MSR));
Jiri Slaby1c456072008-02-07 00:16:46 -0800638 }
Jiri Slaby1c456072008-02-07 00:16:46 -0800639 }
640 outb(info->MCR, info->ioaddr + UART_MCR);
Peter Hurley2d686552016-04-09 17:53:23 -0700641 tty_port_set_check_carrier(&info->port, ~cflag & CLOCAL);
642 if (~cflag & CLOCAL)
Jiri Slaby1c456072008-02-07 00:16:46 -0800643 info->IER |= UART_IER_MSI;
Jiri Slaby1c456072008-02-07 00:16:46 -0800644 outb(info->IER, info->ioaddr + UART_IER);
645
646 /*
647 * Set up parity check flag
648 */
649 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
Alan Cox216ba022008-10-13 10:40:19 +0100650 if (I_INPCK(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800651 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Alan Cox216ba022008-10-13 10:40:19 +0100652 if (I_BRKINT(tty) || I_PARMRK(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800653 info->read_status_mask |= UART_LSR_BI;
654
655 info->ignore_status_mask = 0;
656
Alan Cox216ba022008-10-13 10:40:19 +0100657 if (I_IGNBRK(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800658 info->ignore_status_mask |= UART_LSR_BI;
659 info->read_status_mask |= UART_LSR_BI;
660 /*
661 * If we're ignore parity and break indicators, ignore
662 * overruns too. (For real raw support).
663 */
Alan Cox216ba022008-10-13 10:40:19 +0100664 if (I_IGNPAR(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800665 info->ignore_status_mask |=
666 UART_LSR_OE |
667 UART_LSR_PE |
668 UART_LSR_FE;
669 info->read_status_mask |=
670 UART_LSR_OE |
671 UART_LSR_PE |
672 UART_LSR_FE;
673 }
674 }
Jiri Slaby292955a2021-06-18 08:14:13 +0200675 if (info->board->must_hwid) {
Alan Cox216ba022008-10-13 10:40:19 +0100676 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
677 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
Jiri Slabyb441eb02021-06-18 08:14:48 +0200678 mxser_must_set_rx_sw_flow_control(info->ioaddr, I_IXON(tty));
679 mxser_must_set_tx_sw_flow_control(info->ioaddr, I_IXOFF(tty));
Jiri Slaby1c456072008-02-07 00:16:46 -0800680 }
681
682
Jiri Slabyd249e662021-09-22 09:59:38 +0200683 outb(info->FCR, info->ioaddr + UART_FCR);
Jiri Slaby1c456072008-02-07 00:16:46 -0800684 outb(cval, info->ioaddr + UART_LCR);
Jiri Slaby1c456072008-02-07 00:16:46 -0800685}
686
Alan Cox216ba022008-10-13 10:40:19 +0100687static void mxser_check_modem_status(struct tty_struct *tty,
688 struct mxser_port *port, int status)
Jiri Slaby1c456072008-02-07 00:16:46 -0800689{
690 /* update input line counters */
691 if (status & UART_MSR_TERI)
692 port->icount.rng++;
693 if (status & UART_MSR_DDSR)
694 port->icount.dsr++;
695 if (status & UART_MSR_DDCD)
696 port->icount.dcd++;
697 if (status & UART_MSR_DCTS)
698 port->icount.cts++;
Alan Coxbdc04e32009-09-19 13:13:31 -0700699 wake_up_interruptible(&port->port.delta_msr_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800700
Peter Hurley2d686552016-04-09 17:53:23 -0700701 if (tty_port_check_carrier(&port->port) && (status & UART_MSR_DDCD)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800702 if (status & UART_MSR_DCD)
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100703 wake_up_interruptible(&port->port.open_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800704 }
705
Jiri Slabybe486662021-06-18 08:15:04 +0200706 if (tty_port_cts_enabled(&port->port))
707 mxser_handle_cts(tty, port, status);
Jiri Slaby1c456072008-02-07 00:16:46 -0800708}
709
Jiri Slabyee7e5e62021-09-22 09:59:36 +0200710static void mxser_disable_and_clear_FIFO(struct mxser_port *info)
711{
712 u8 fcr = UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
713
714 if (info->board->must_hwid)
715 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
716
717 outb(fcr, info->ioaddr + UART_FCR);
718}
719
Alan Cox67691402009-11-30 13:17:35 +0000720static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -0800721{
Alan Cox67691402009-11-30 13:17:35 +0000722 struct mxser_port *info = container_of(port, struct mxser_port, port);
Jiri Slaby1c456072008-02-07 00:16:46 -0800723 unsigned long page;
724 unsigned long flags;
725
726 page = __get_free_page(GFP_KERNEL);
727 if (!page)
728 return -ENOMEM;
729
730 spin_lock_irqsave(&info->slock, flags);
731
Jiri Slaby987a4cf2021-06-18 08:15:00 +0200732 if (!info->type) {
Alan Cox216ba022008-10-13 10:40:19 +0100733 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800734 free_page(page);
735 spin_unlock_irqrestore(&info->slock, flags);
736 return 0;
737 }
Alan Cox67691402009-11-30 13:17:35 +0000738 info->port.xmit_buf = (unsigned char *) page;
Jiri Slaby1c456072008-02-07 00:16:46 -0800739
740 /*
741 * Clear the FIFO buffers and disable them
742 * (they will be reenabled in mxser_change_speed())
743 */
Jiri Slabyee7e5e62021-09-22 09:59:36 +0200744 mxser_disable_and_clear_FIFO(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800745
746 /*
747 * At this point there's no way the LSR could still be 0xFF;
748 * if it is, then bail out, because there's likely no UART
749 * here.
750 */
751 if (inb(info->ioaddr + UART_LSR) == 0xff) {
752 spin_unlock_irqrestore(&info->slock, flags);
753 if (capable(CAP_SYS_ADMIN)) {
Jiri Slabyf43a510d2009-12-09 12:34:15 -0800754 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800755 return 0;
756 } else
757 return -ENODEV;
758 }
759
760 /*
761 * Clear the interrupt registers.
762 */
763 (void) inb(info->ioaddr + UART_LSR);
764 (void) inb(info->ioaddr + UART_RX);
765 (void) inb(info->ioaddr + UART_IIR);
766 (void) inb(info->ioaddr + UART_MSR);
767
768 /*
769 * Now, initialize the UART
770 */
771 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
772 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
773 outb(info->MCR, info->ioaddr + UART_MCR);
774
775 /*
776 * Finally, enable interrupts
777 */
778 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
779
Jiri Slaby292955a2021-06-18 08:14:13 +0200780 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -0800781 info->IER |= MOXA_MUST_IER_EGDAI;
782 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
783
784 /*
785 * And clear the interrupt registers again for luck.
786 */
787 (void) inb(info->ioaddr + UART_LSR);
788 (void) inb(info->ioaddr + UART_RX);
789 (void) inb(info->ioaddr + UART_IIR);
790 (void) inb(info->ioaddr + UART_MSR);
791
Alan Cox216ba022008-10-13 10:40:19 +0100792 clear_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800793 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
794
795 /*
796 * and set the speed of the serial port
797 */
Jiri Slaby3fdfa162021-09-22 09:59:32 +0200798 mxser_change_speed(tty, NULL);
Jiri Slaby1c456072008-02-07 00:16:46 -0800799 spin_unlock_irqrestore(&info->slock, flags);
800
801 return 0;
802}
803
804/*
Alan Cox67691402009-11-30 13:17:35 +0000805 * This routine will shutdown a serial port
Jiri Slaby1c456072008-02-07 00:16:46 -0800806 */
Alan Cox67691402009-11-30 13:17:35 +0000807static void mxser_shutdown_port(struct tty_port *port)
Jiri Slaby1c456072008-02-07 00:16:46 -0800808{
Alan Cox67691402009-11-30 13:17:35 +0000809 struct mxser_port *info = container_of(port, struct mxser_port, port);
Jiri Slaby1c456072008-02-07 00:16:46 -0800810 unsigned long flags;
811
Jiri Slaby1c456072008-02-07 00:16:46 -0800812 spin_lock_irqsave(&info->slock, flags);
813
814 /*
815 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
816 * here so the queue might never be waken up
817 */
Alan Coxbdc04e32009-09-19 13:13:31 -0700818 wake_up_interruptible(&info->port.delta_msr_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800819
820 /*
Alan Cox67691402009-11-30 13:17:35 +0000821 * Free the xmit buffer, if necessary
Jiri Slaby1c456072008-02-07 00:16:46 -0800822 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100823 if (info->port.xmit_buf) {
824 free_page((unsigned long) info->port.xmit_buf);
825 info->port.xmit_buf = NULL;
Jiri Slaby1c456072008-02-07 00:16:46 -0800826 }
827
828 info->IER = 0;
829 outb(0x00, info->ioaddr + UART_IER);
830
Jiri Slaby1c456072008-02-07 00:16:46 -0800831 /* clear Rx/Tx FIFO's */
Jiri Slabyee7e5e62021-09-22 09:59:36 +0200832 mxser_disable_and_clear_FIFO(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800833
834 /* read data port to reset things */
835 (void) inb(info->ioaddr + UART_RX);
836
Jiri Slaby1c456072008-02-07 00:16:46 -0800837
Jiri Slaby292955a2021-06-18 08:14:13 +0200838 if (info->board->must_hwid)
Jiri Slabyb441eb02021-06-18 08:14:48 +0200839 mxser_must_no_sw_flow_control(info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800840
841 spin_unlock_irqrestore(&info->slock, flags);
842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844/*
845 * This routine is called whenever a serial port is opened. It
846 * enables interrupts for a serial port, linking in its async structure into
847 * the IRQ chain. It also performs the serial-specific
848 * initialization for the tty structure.
849 */
850static int mxser_open(struct tty_struct *tty, struct file *filp)
851{
Jiri Slaby42ad25f2021-06-18 08:14:56 +0200852 struct tty_port *tport = tty->port;
853 struct mxser_port *port = container_of(tport, struct mxser_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Jiri Slaby42ad25f2021-06-18 08:14:56 +0200855 tty->driver_data = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Jiri Slaby42ad25f2021-06-18 08:14:56 +0200857 return tty_port_open(tport, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Alan Cox978e5952008-04-30 00:53:59 -0700860static void mxser_flush_buffer(struct tty_struct *tty)
861{
862 struct mxser_port *info = tty->driver_data;
Alan Cox978e5952008-04-30 00:53:59 -0700863 unsigned long flags;
864
865
866 spin_lock_irqsave(&info->slock, flags);
867 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
868
Jiri Slabyd249e662021-09-22 09:59:38 +0200869 outb(info->FCR | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
Alan Cox978e5952008-04-30 00:53:59 -0700870 info->ioaddr + UART_FCR);
Alan Cox978e5952008-04-30 00:53:59 -0700871
872 spin_unlock_irqrestore(&info->slock, flags);
873
874 tty_wakeup(tty);
875}
876
877
Alan Cox67691402009-11-30 13:17:35 +0000878static void mxser_close_port(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Alan Cox1e2b0252009-09-19 13:13:21 -0700880 struct mxser_port *info = container_of(port, struct mxser_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * At this point we stop accepting input. To do this, we
884 * disable the receive line status interrupts, and tell the
885 * interrupt driver to stop checking the data ready bit in the
886 * line status register.
887 */
888 info->IER &= ~UART_IER_RLSI;
Jiri Slaby292955a2021-06-18 08:14:13 +0200889 if (info->board->must_hwid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -0800891
Alan Cox67691402009-11-30 13:17:35 +0000892 outb(info->IER, info->ioaddr + UART_IER);
893 /*
894 * Before we drop DTR, make sure the UART transmitter
895 * has completely drained; this is especially
896 * important if there is a transmit FIFO!
897 */
898 timeout = jiffies + HZ;
899 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
900 schedule_timeout_interruptible(5);
901 if (time_after(jiffies, timeout))
902 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Alan Cox1e2b0252009-09-19 13:13:21 -0700904}
905
906/*
907 * This routine is called when the serial port gets closed. First, we
908 * wait for the last remaining data to be sent. Then, we unlink its
909 * async structure from the interrupt chain if necessary, and we free
910 * that IRQ if nothing is left in the chain.
911 */
912static void mxser_close(struct tty_struct *tty, struct file *filp)
913{
914 struct mxser_port *info = tty->driver_data;
915 struct tty_port *port = &info->port;
916
Jiri Slaby389fc822021-06-18 08:14:38 +0200917 if (info == NULL)
Alan Cox1e2b0252009-09-19 13:13:21 -0700918 return;
919 if (tty_port_close_start(port, tty, filp) == 0)
920 return;
Peter Hurleycd7b4b32016-01-10 14:51:38 -0800921 info->closing = 1;
Alan Cox67691402009-11-30 13:17:35 +0000922 mutex_lock(&port->mutex);
923 mxser_close_port(port);
Alan Cox1e2b0252009-09-19 13:13:21 -0700924 mxser_flush_buffer(tty);
Peter Hurleyd41861c2016-04-09 17:53:25 -0700925 if (tty_port_initialized(port) && C_HUPCL(tty))
926 tty_port_lower_dtr_rts(port);
Alan Cox67691402009-11-30 13:17:35 +0000927 mxser_shutdown_port(port);
Peter Hurleyd41861c2016-04-09 17:53:25 -0700928 tty_port_set_initialized(port, 0);
Alan Cox67691402009-11-30 13:17:35 +0000929 mutex_unlock(&port->mutex);
Peter Hurleycd7b4b32016-01-10 14:51:38 -0800930 info->closing = 0;
Alan Coxa6614992009-01-02 13:46:50 +0000931 /* Right now the tty_port set is done outside of the close_end helper
932 as we don't yet have everyone using refcounts */
933 tty_port_close_end(port, tty);
934 tty_port_tty_set(port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
938{
939 int c, total = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800940 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 unsigned long flags;
942
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100943 if (!info->port.xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700947 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
948 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (c <= 0)
950 break;
951
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100952 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700954 info->xmit_head = (info->xmit_head + c) &
955 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 info->xmit_cnt += c;
957 spin_unlock_irqrestore(&info->slock, flags);
958
959 buf += c;
960 count -= c;
961 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963
Jiri Slaby5d1ea1a2021-06-18 08:15:12 +0200964 if (info->xmit_cnt && !tty->flow.stopped)
965 if (!tty->hw_stopped || mxser_16550A_or_MUST(info))
Jiri Slaby740165f2021-06-18 08:15:05 +0200966 mxser_start_tx(info);
Jiri Slaby5d1ea1a2021-06-18 08:15:12 +0200967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return total;
969}
970
Alan Cox0be2ead2008-04-30 00:54:03 -0700971static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Jiri Slaby1c456072008-02-07 00:16:46 -0800973 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 unsigned long flags;
975
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100976 if (!info->port.xmit_buf)
Alan Cox0be2ead2008-04-30 00:54:03 -0700977 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
Alan Cox0be2ead2008-04-30 00:54:03 -0700980 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100983 info->port.xmit_buf[info->xmit_head++] = ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
985 info->xmit_cnt++;
986 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby8aff64e2021-06-18 08:15:03 +0200987
Alan Cox0be2ead2008-04-30 00:54:03 -0700988 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991
992static void mxser_flush_chars(struct tty_struct *tty)
993{
Jiri Slaby1c456072008-02-07 00:16:46 -0800994 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Jiri Slaby265ceff2021-06-18 08:15:06 +0200996 if (!info->xmit_cnt || tty->flow.stopped || !info->port.xmit_buf ||
Jiri Slaby5d1ea1a2021-06-18 08:15:12 +0200997 (tty->hw_stopped && !mxser_16550A_or_MUST(info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return;
999
Jiri Slaby740165f2021-06-18 08:15:05 +02001000 mxser_start_tx(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Jiri Slaby03b3b1a2021-05-05 11:19:15 +02001003static unsigned int mxser_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Jiri Slaby1c456072008-02-07 00:16:46 -08001005 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 int ret;
1007
1008 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
Jiri Slabyace7dd92008-07-25 01:48:22 -07001009 return ret < 0 ? 0 : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
Jiri Slabyfff4ef12021-05-05 11:19:19 +02001012static unsigned int mxser_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Jiri Slaby1c456072008-02-07 00:16:46 -08001014 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return info->xmit_cnt;
1016}
1017
Jiri Slaby1c456072008-02-07 00:16:46 -08001018/*
1019 * ------------------------------------------------------------
1020 * friends of mxser_ioctl()
1021 * ------------------------------------------------------------
1022 */
Alan Cox216ba022008-10-13 10:40:19 +01001023static int mxser_get_serial_info(struct tty_struct *tty,
Al Viro6da5b582018-09-11 22:22:06 -04001024 struct serial_struct *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Alan Cox216ba022008-10-13 10:40:19 +01001026 struct mxser_port *info = tty->driver_data;
Al Viro6da5b582018-09-11 22:22:06 -04001027 struct tty_port *port = &info->port;
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001028 unsigned int closing_wait, close_delay;
Al Viro6da5b582018-09-11 22:22:06 -04001029
Al Viro6da5b582018-09-11 22:22:06 -04001030 mutex_lock(&port->mutex);
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001031
1032 close_delay = jiffies_to_msecs(info->port.close_delay) / 10;
1033 closing_wait = info->port.closing_wait;
1034 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1035 closing_wait = jiffies_to_msecs(closing_wait) / 10;
1036
Dan Carpenter2285c492021-08-25 10:24:35 +03001037 ss->type = info->type;
1038 ss->line = tty->index;
1039 ss->port = info->ioaddr;
1040 ss->irq = info->board->irq;
1041 ss->flags = info->port.flags;
1042 ss->baud_base = MXSER_BAUD_BASE;
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001043 ss->close_delay = close_delay;
1044 ss->closing_wait = closing_wait;
Jiri Slabyd811b262021-06-18 08:14:42 +02001045 ss->custom_divisor = MXSER_CUSTOM_DIVISOR,
Al Viro6da5b582018-09-11 22:22:06 -04001046 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001047 return 0;
1048}
1049
Alan Cox216ba022008-10-13 10:40:19 +01001050static int mxser_set_serial_info(struct tty_struct *tty,
Al Viro6da5b582018-09-11 22:22:06 -04001051 struct serial_struct *ss)
Jiri Slaby1c456072008-02-07 00:16:46 -08001052{
Alan Cox216ba022008-10-13 10:40:19 +01001053 struct mxser_port *info = tty->driver_data;
Alan Cox07f86c02009-11-30 13:17:41 +00001054 struct tty_port *port = &info->port;
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001055 speed_t baud;
Jiri Slaby1c456072008-02-07 00:16:46 -08001056 unsigned long sl_flags;
Jiri Slaby06cc52e2021-06-18 08:15:11 +02001057 unsigned int old_speed, close_delay, closing_wait;
Jiri Slaby1c456072008-02-07 00:16:46 -08001058 int retval = 0;
1059
Al Viro6da5b582018-09-11 22:22:06 -04001060 if (tty_io_error(tty))
1061 return -EIO;
Jiri Slaby1c456072008-02-07 00:16:46 -08001062
Al Viro6da5b582018-09-11 22:22:06 -04001063 mutex_lock(&port->mutex);
Al Viro6da5b582018-09-11 22:22:06 -04001064
1065 if (ss->irq != info->board->irq ||
1066 ss->port != info->ioaddr) {
1067 mutex_unlock(&port->mutex);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001068 return -EINVAL;
Al Viro6da5b582018-09-11 22:22:06 -04001069 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001070
Jiri Slaby06cc52e2021-06-18 08:15:11 +02001071 old_speed = port->flags & ASYNC_SPD_MASK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001072
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001073 close_delay = msecs_to_jiffies(ss->close_delay * 10);
1074 closing_wait = ss->closing_wait;
1075 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1076 closing_wait = msecs_to_jiffies(closing_wait * 10);
1077
Jiri Slaby1c456072008-02-07 00:16:46 -08001078 if (!capable(CAP_SYS_ADMIN)) {
Jiri Slabya6970c32021-06-18 08:14:41 +02001079 if ((ss->baud_base != MXSER_BAUD_BASE) ||
Jiri Slaby1b3086b2021-06-18 08:15:10 +02001080 (close_delay != port->close_delay) ||
1081 (closing_wait != port->closing_wait) ||
1082 ((ss->flags & ~ASYNC_USR_MASK) != (port->flags & ~ASYNC_USR_MASK))) {
Al Viro6da5b582018-09-11 22:22:06 -04001083 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001084 return -EPERM;
Al Viro6da5b582018-09-11 22:22:06 -04001085 }
Jiri Slaby1b3086b2021-06-18 08:15:10 +02001086 port->flags = (port->flags & ~ASYNC_USR_MASK) |
1087 (ss->flags & ASYNC_USR_MASK);
Jiri Slaby1c456072008-02-07 00:16:46 -08001088 } else {
1089 /*
1090 * OK, past this point, all the error checking has been done.
1091 * At this point, we start making changes.....
1092 */
Alan Cox07f86c02009-11-30 13:17:41 +00001093 port->flags = ((port->flags & ~ASYNC_FLAGS) |
Al Viro6da5b582018-09-11 22:22:06 -04001094 (ss->flags & ASYNC_FLAGS));
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001095 port->close_delay = close_delay;
1096 port->closing_wait = closing_wait;
Alan Cox07f86c02009-11-30 13:17:41 +00001097 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
Jiri Slabya6970c32021-06-18 08:14:41 +02001098 (ss->baud_base != MXSER_BAUD_BASE ||
Al Viro6da5b582018-09-11 22:22:06 -04001099 ss->custom_divisor !=
Jiri Slabyd811b262021-06-18 08:14:42 +02001100 MXSER_CUSTOM_DIVISOR)) {
Al Viro6da5b582018-09-11 22:22:06 -04001101 if (ss->custom_divisor == 0) {
1102 mutex_unlock(&port->mutex);
Alan Cox07f86c02009-11-30 13:17:41 +00001103 return -EINVAL;
Al Viro6da5b582018-09-11 22:22:06 -04001104 }
1105 baud = ss->baud_base / ss->custom_divisor;
Alan Cox216ba022008-10-13 10:40:19 +01001106 tty_encode_baud_rate(tty, baud, baud);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001107 }
Johan Hovoldb91cfb22021-04-07 12:23:32 +02001108
1109 info->type = ss->type;
1110
Jiri Slabyc3db20c2021-06-18 08:14:55 +02001111 mxser_process_txrx_fifo(info);
Jiri Slaby1c456072008-02-07 00:16:46 -08001112 }
1113
Peter Hurleyd41861c2016-04-09 17:53:25 -07001114 if (tty_port_initialized(port)) {
Jiri Slaby06cc52e2021-06-18 08:15:11 +02001115 if (old_speed != (port->flags & ASYNC_SPD_MASK)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001116 spin_lock_irqsave(&info->slock, sl_flags);
Jiri Slaby3fdfa162021-09-22 09:59:32 +02001117 mxser_change_speed(tty, NULL);
Jiri Slaby1c456072008-02-07 00:16:46 -08001118 spin_unlock_irqrestore(&info->slock, sl_flags);
1119 }
Alan Cox67691402009-11-30 13:17:35 +00001120 } else {
Alan Cox07f86c02009-11-30 13:17:41 +00001121 retval = mxser_activate(port, tty);
Alan Cox67691402009-11-30 13:17:35 +00001122 if (retval == 0)
Peter Hurleyd41861c2016-04-09 17:53:25 -07001123 tty_port_set_initialized(port, 1);
Alan Cox67691402009-11-30 13:17:35 +00001124 }
Al Viro6da5b582018-09-11 22:22:06 -04001125 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001126 return retval;
1127}
1128
1129/*
1130 * mxser_get_lsr_info - get line status register info
1131 *
1132 * Purpose: Let user call ioctl() to get info when the UART physically
1133 * is emptied. On bus types like RS485, the transmitter must
1134 * release the bus after transmitting. This must be done when
1135 * the transmit shift register is empty, not be done when the
1136 * transmit holding register is empty. This functionality
1137 * allows an RS485 driver to be written in user space.
1138 */
1139static int mxser_get_lsr_info(struct mxser_port *info,
1140 unsigned int __user *value)
1141{
1142 unsigned char status;
1143 unsigned int result;
1144 unsigned long flags;
1145
1146 spin_lock_irqsave(&info->slock, flags);
1147 status = inb(info->ioaddr + UART_LSR);
1148 spin_unlock_irqrestore(&info->slock, flags);
1149 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1150 return put_user(result, value);
1151}
1152
Alan Cox60b33c12011-02-14 16:26:14 +00001153static int mxser_tiocmget(struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -08001154{
1155 struct mxser_port *info = tty->driver_data;
1156 unsigned char control, status;
1157 unsigned long flags;
1158
Peter Hurley18900ca2016-04-09 17:06:48 -07001159 if (tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001160 return -EIO;
1161
Jiri Slaby1c456072008-02-07 00:16:46 -08001162 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby202acda2021-06-18 08:15:09 +02001163 control = info->MCR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001164 status = inb(info->ioaddr + UART_MSR);
1165 if (status & UART_MSR_ANY_DELTA)
Alan Cox216ba022008-10-13 10:40:19 +01001166 mxser_check_modem_status(tty, info, status);
Jiri Slaby1c456072008-02-07 00:16:46 -08001167 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby202acda2021-06-18 08:15:09 +02001168
Jiri Slaby1c456072008-02-07 00:16:46 -08001169 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1170 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1171 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1172 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1173 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1174 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1175}
1176
Alan Cox20b9d172011-02-14 16:26:50 +00001177static int mxser_tiocmset(struct tty_struct *tty,
Jiri Slaby1c456072008-02-07 00:16:46 -08001178 unsigned int set, unsigned int clear)
1179{
1180 struct mxser_port *info = tty->driver_data;
1181 unsigned long flags;
1182
Peter Hurley18900ca2016-04-09 17:06:48 -07001183 if (tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001184 return -EIO;
1185
1186 spin_lock_irqsave(&info->slock, flags);
1187
1188 if (set & TIOCM_RTS)
1189 info->MCR |= UART_MCR_RTS;
1190 if (set & TIOCM_DTR)
1191 info->MCR |= UART_MCR_DTR;
1192
1193 if (clear & TIOCM_RTS)
1194 info->MCR &= ~UART_MCR_RTS;
1195 if (clear & TIOCM_DTR)
1196 info->MCR &= ~UART_MCR_DTR;
1197
1198 outb(info->MCR, info->ioaddr + UART_MCR);
1199 spin_unlock_irqrestore(&info->slock, flags);
1200 return 0;
1201}
1202
Jiri Slaby1c456072008-02-07 00:16:46 -08001203static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1204 struct async_icount *cprev)
1205{
1206 struct async_icount cnow;
1207 unsigned long flags;
1208 int ret;
1209
1210 spin_lock_irqsave(&info->slock, flags);
1211 cnow = info->icount; /* atomic copy */
1212 spin_unlock_irqrestore(&info->slock, flags);
1213
1214 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1215 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1216 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1217 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1218
1219 *cprev = cnow;
1220
1221 return ret;
1222}
1223
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001224/* We should likely switch to TIOCGRS485/TIOCSRS485. */
1225static int mxser_ioctl_op_mode(struct mxser_port *port, int index, bool set,
1226 int __user *u_opmode)
1227{
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001228 int opmode, p = index % 4;
1229 int shiftbit = p * 2;
Jiri Slaby238d1172021-06-18 08:14:45 +02001230 u8 val;
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001231
1232 if (port->board->must_hwid != MOXA_MUST_MU860_HWID)
1233 return -EFAULT;
1234
1235 if (set) {
1236 if (get_user(opmode, u_opmode))
1237 return -EFAULT;
1238
Jiri Slaby238d1172021-06-18 08:14:45 +02001239 if (opmode & ~OP_MODE_MASK)
1240 return -EINVAL;
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001241
1242 spin_lock_irq(&port->slock);
1243 val = inb(port->opmode_ioaddr);
Jiri Slaby238d1172021-06-18 08:14:45 +02001244 val &= ~(OP_MODE_MASK << shiftbit);
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001245 val |= (opmode << shiftbit);
1246 outb(val, port->opmode_ioaddr);
1247 spin_unlock_irq(&port->slock);
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001248
Jiri Slaby238d1172021-06-18 08:14:45 +02001249 return 0;
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001250 }
1251
Jiri Slaby238d1172021-06-18 08:14:45 +02001252 spin_lock_irq(&port->slock);
1253 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1254 spin_unlock_irq(&port->slock);
1255
1256 return put_user(opmode & OP_MODE_MASK, u_opmode);
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001257}
1258
Alan Cox6caa76b2011-02-14 16:27:22 +00001259static int mxser_ioctl(struct tty_struct *tty,
Jiri Slaby1c456072008-02-07 00:16:46 -08001260 unsigned int cmd, unsigned long arg)
1261{
1262 struct mxser_port *info = tty->driver_data;
1263 struct async_icount cnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 unsigned long flags;
1265 void __user *argp = (void __user *)arg;
1266
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001267 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE)
1268 return mxser_ioctl_op_mode(info, tty->index,
1269 cmd == MOXA_SET_OP_MODE, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Al Viro6da5b582018-09-11 22:22:06 -04001271 if (cmd != TIOCMIWAIT && tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001272 return -EIO;
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 case TIOCSERGETLSR: /* Get line status register */
Alan Cox9d6d1622008-04-30 00:53:20 -07001276 return mxser_get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /*
1278 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1279 * - mask passed in arg for lines of interest
1280 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1281 * Caller should use TIOCGICOUNT to see which one it was
1282 */
Jiri Slabyfc838152007-04-23 14:41:04 -07001283 case TIOCMIWAIT:
1284 spin_lock_irqsave(&info->slock, flags);
1285 cnow = info->icount; /* note the counters on entry */
1286 spin_unlock_irqrestore(&info->slock, flags);
1287
Alan Coxbdc04e32009-09-19 13:13:31 -07001288 return wait_event_interruptible(info->port.delta_msr_wait,
Jiri Slaby1c456072008-02-07 00:16:46 -08001289 mxser_cflags_changed(info, arg, &cnow));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 default:
1291 return -ENOIOCTLCMD;
1292 }
1293 return 0;
1294}
1295
Alan Cox05871022010-09-16 18:21:52 +01001296 /*
1297 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1298 * Return: write counters to the user passed counter struct
1299 * NB: both 1->0 and 0->1 transitions are counted except for
1300 * RI where only 0->1 is counted.
1301 */
1302
1303static int mxser_get_icount(struct tty_struct *tty,
1304 struct serial_icounter_struct *icount)
1305
1306{
1307 struct mxser_port *info = tty->driver_data;
1308 struct async_icount cnow;
1309 unsigned long flags;
1310
1311 spin_lock_irqsave(&info->slock, flags);
1312 cnow = info->icount;
1313 spin_unlock_irqrestore(&info->slock, flags);
1314
1315 icount->frame = cnow.frame;
1316 icount->brk = cnow.brk;
1317 icount->overrun = cnow.overrun;
1318 icount->buf_overrun = cnow.buf_overrun;
1319 icount->parity = cnow.parity;
1320 icount->rx = cnow.rx;
1321 icount->tx = cnow.tx;
1322 icount->cts = cnow.cts;
1323 icount->dsr = cnow.dsr;
1324 icount->rng = cnow.rng;
1325 icount->dcd = cnow.dcd;
1326 return 0;
1327}
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329static void mxser_stoprx(struct tty_struct *tty)
1330{
Jiri Slaby1c456072008-02-07 00:16:46 -08001331 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Jiri Slaby19236282021-09-22 09:59:34 +02001333 info->ldisc_stop_rx = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (I_IXOFF(tty)) {
Jiri Slaby292955a2021-06-18 08:14:13 +02001335 if (info->board->must_hwid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001337 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 info->x_char = STOP_CHAR(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001340 outb(0, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001342 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344 }
1345
Peter Hurley9db276f2016-01-10 20:36:15 -08001346 if (C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001347 info->MCR &= ~UART_MCR_RTS;
1348 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350}
1351
1352/*
1353 * This routine is called by the upper-layer tty layer to signal that
1354 * incoming characters should be throttled.
1355 */
1356static void mxser_throttle(struct tty_struct *tty)
1357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
1361static void mxser_unthrottle(struct tty_struct *tty)
1362{
Jiri Slaby1c456072008-02-07 00:16:46 -08001363 struct mxser_port *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001364
Jiri Slaby1c456072008-02-07 00:16:46 -08001365 /* startrx */
Jiri Slaby19236282021-09-22 09:59:34 +02001366 info->ldisc_stop_rx = false;
Jiri Slaby1c456072008-02-07 00:16:46 -08001367 if (I_IXOFF(tty)) {
1368 if (info->x_char)
1369 info->x_char = 0;
1370 else {
Jiri Slaby292955a2021-06-18 08:14:13 +02001371 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001372 info->IER |= MOXA_MUST_RECV_ISR;
1373 outb(info->IER, info->ioaddr + UART_IER);
1374 } else {
1375 info->x_char = START_CHAR(tty);
1376 outb(0, info->ioaddr + UART_IER);
1377 info->IER |= UART_IER_THRI;
1378 outb(info->IER, info->ioaddr + UART_IER);
1379 }
1380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
Peter Hurley9db276f2016-01-10 20:36:15 -08001383 if (C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001384 info->MCR |= UART_MCR_RTS;
1385 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387}
1388
1389/*
1390 * mxser_stop() and mxser_start()
1391 *
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001392 * This routines are called before setting or resetting tty->flow.stopped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 * They enable or disable transmitter interrupts, as necessary.
1394 */
1395static void mxser_stop(struct tty_struct *tty)
1396{
Jiri Slaby1c456072008-02-07 00:16:46 -08001397 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 unsigned long flags;
1399
1400 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby740165f2021-06-18 08:15:05 +02001401 if (info->IER & UART_IER_THRI)
1402 __mxser_stop_tx(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 spin_unlock_irqrestore(&info->slock, flags);
1404}
1405
1406static void mxser_start(struct tty_struct *tty)
1407{
Jiri Slaby1c456072008-02-07 00:16:46 -08001408 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 unsigned long flags;
1410
1411 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby740165f2021-06-18 08:15:05 +02001412 if (info->xmit_cnt && info->port.xmit_buf)
1413 __mxser_start_tx(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 spin_unlock_irqrestore(&info->slock, flags);
1415}
1416
Jiri Slaby1c456072008-02-07 00:16:46 -08001417static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1418{
1419 struct mxser_port *info = tty->driver_data;
1420 unsigned long flags;
1421
1422 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby3fdfa162021-09-22 09:59:32 +02001423 mxser_change_speed(tty, old_termios);
Jiri Slaby1c456072008-02-07 00:16:46 -08001424 spin_unlock_irqrestore(&info->slock, flags);
1425
Peter Hurley9db276f2016-01-10 20:36:15 -08001426 if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001427 tty->hw_stopped = 0;
1428 mxser_start(tty);
1429 }
1430
1431 /* Handle sw stopped */
Peter Hurley9db276f2016-01-10 20:36:15 -08001432 if ((old_termios->c_iflag & IXON) && !I_IXON(tty)) {
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001433 tty->flow.stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001434
Jiri Slaby292955a2021-06-18 08:14:13 +02001435 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001436 spin_lock_irqsave(&info->slock, flags);
Jiri Slabyb441eb02021-06-18 08:14:48 +02001437 mxser_must_set_rx_sw_flow_control(info->ioaddr, false);
Jiri Slaby1c456072008-02-07 00:16:46 -08001438 spin_unlock_irqrestore(&info->slock, flags);
1439 }
1440
1441 mxser_start(tty);
1442 }
1443}
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445/*
1446 * mxser_wait_until_sent() --- wait until the transmitter is empty
1447 */
1448static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1449{
Jiri Slaby1c456072008-02-07 00:16:46 -08001450 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 unsigned long orig_jiffies, char_time;
Alan Cox07f86c02009-11-30 13:17:41 +00001452 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 int lsr;
1454
1455 if (info->type == PORT_UNKNOWN)
1456 return;
1457
1458 if (info->xmit_fifo_size == 0)
1459 return; /* Just in case.... */
1460
1461 orig_jiffies = jiffies;
1462 /*
1463 * Set the check interval to be 1/5 of the estimated time to
1464 * send a single character, and make it at least 1. The check
1465 * interval should also be less than the timeout.
1466 *
1467 * Note: we have to use pretty tight timings here to satisfy
1468 * the NIST-PCTS.
1469 */
1470 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1471 char_time = char_time / 5;
1472 if (char_time == 0)
1473 char_time = 1;
1474 if (timeout && timeout < char_time)
1475 char_time = timeout;
1476 /*
1477 * If the transmitter hasn't cleared in twice the approximate
1478 * amount of time to send the entire FIFO, it probably won't
1479 * ever clear. This assumes the UART isn't doing flow
1480 * control, which is currently the case. Hence, if it ever
1481 * takes longer than info->timeout, this is probably due to a
1482 * UART bug of some kind. So, we clamp the timeout parameter at
1483 * 2*info->timeout.
1484 */
1485 if (!timeout || timeout > 2 * info->timeout)
1486 timeout = 2 * info->timeout;
Jiri Slaby8bab5342011-07-14 14:35:15 +02001487
Alan Cox07f86c02009-11-30 13:17:41 +00001488 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001489 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Alan Cox07f86c02009-11-30 13:17:41 +00001490 spin_unlock_irqrestore(&info->slock, flags);
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001491 schedule_timeout_interruptible(char_time);
Alan Cox07f86c02009-11-30 13:17:41 +00001492 spin_lock_irqsave(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (signal_pending(current))
1494 break;
1495 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1496 break;
1497 }
Alan Cox07f86c02009-11-30 13:17:41 +00001498 spin_unlock_irqrestore(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502/*
1503 * This routine is called by tty_hangup() when a hangup is signaled.
1504 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001505static void mxser_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Jiri Slaby1c456072008-02-07 00:16:46 -08001507 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 mxser_flush_buffer(tty);
Alan Cox3b6826b2009-01-02 13:45:58 +00001510 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513/*
1514 * mxser_rs_break() --- routine which turns the break handling on or off
1515 */
Alan Cox9e98966c2008-07-22 11:18:03 +01001516static int mxser_rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Jiri Slaby1c456072008-02-07 00:16:46 -08001518 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 unsigned long flags;
Jiri Slaby59908432021-06-18 08:15:01 +02001520 u8 lcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby59908432021-06-18 08:15:01 +02001523 lcr = inb(info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (break_state == -1)
Jiri Slaby59908432021-06-18 08:15:01 +02001525 lcr |= UART_LCR_SBC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 else
Jiri Slaby59908432021-06-18 08:15:01 +02001527 lcr &= ~UART_LCR_SBC;
1528 outb(lcr, info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby59908432021-06-18 08:15:01 +02001530
Alan Cox9e98966c2008-07-22 11:18:03 +01001531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001534static bool mxser_receive_chars_new(struct tty_struct *tty,
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001535 struct mxser_port *port, u8 status)
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001536{
1537 enum mxser_must_hwid hwid = port->board->must_hwid;
1538 u8 gdl;
1539
1540 if (hwid == MOXA_OTHER_UART)
1541 return false;
Jiri Slaby7d5006d2021-09-22 09:59:33 +02001542 if (status & (UART_LSR_BRK_ERROR_BITS | MOXA_MUST_LSR_RERR))
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001543 return false;
1544
1545 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
1546 if (hwid == MOXA_MUST_MU150_HWID)
1547 gdl &= MOXA_MUST_GDL_MASK;
1548
1549 if (gdl >= tty->receive_room && !port->ldisc_stop_rx)
1550 mxser_stoprx(tty);
1551
1552 while (gdl--) {
1553 u8 ch = inb(port->ioaddr + UART_RX);
1554 tty_insert_flip_char(&port->port, ch, 0);
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001555 }
1556
1557 return true;
1558}
1559
Jiri Slaby0c419422021-06-18 08:14:22 +02001560static u8 mxser_receive_chars_old(struct tty_struct *tty,
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001561 struct mxser_port *port, u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Jiri Slaby0c419422021-06-18 08:14:22 +02001563 enum mxser_must_hwid hwid = port->board->must_hwid;
1564 int recv_room = tty->receive_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 int ignored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 int max = 256;
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001567 int cnt = 0;
Jiri Slaby0c419422021-06-18 08:14:22 +02001568 u8 ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 do {
1571 if (max-- < 0)
1572 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Jiri Slaby1c456072008-02-07 00:16:46 -08001574 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby0c419422021-06-18 08:14:22 +02001575 if (hwid && (status & UART_LSR_OE))
Jiri Slabyd249e662021-09-22 09:59:38 +02001576 outb(port->FCR | UART_FCR_CLEAR_RCVR,
Jiri Slabyaaa28e92021-06-18 08:15:07 +02001577 port->ioaddr + UART_FCR);
Jiri Slaby15517802021-06-18 08:14:18 +02001578 status &= port->read_status_mask;
1579 if (status & port->ignore_status_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (++ignored > 100)
1581 break;
1582 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001583 char flag = 0;
Jiri Slaby70640052021-06-18 08:14:26 +02001584 if (status & UART_LSR_BRK_ERROR_BITS) {
Jiri Slaby15517802021-06-18 08:14:18 +02001585 if (status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001586 flag = TTY_BREAK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001587 port->icount.brk++;
1588
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001589 if (port->port.flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 do_SAK(tty);
Jiri Slaby15517802021-06-18 08:14:18 +02001591 } else if (status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001592 flag = TTY_PARITY;
Jiri Slaby1c456072008-02-07 00:16:46 -08001593 port->icount.parity++;
Jiri Slaby15517802021-06-18 08:14:18 +02001594 } else if (status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001595 flag = TTY_FRAME;
Jiri Slaby1c456072008-02-07 00:16:46 -08001596 port->icount.frame++;
Jiri Slaby15517802021-06-18 08:14:18 +02001597 } else if (status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001598 flag = TTY_OVERRUN;
Jiri Slaby1c456072008-02-07 00:16:46 -08001599 port->icount.overrun++;
Jiri Slaby6de6e5c2021-06-18 08:14:23 +02001600 }
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001601 }
Jiri Slaby92a19f92013-01-03 15:53:03 +01001602 tty_insert_flip_char(&port->port, ch, flag);
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001603 cnt++;
1604 if (cnt >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001605 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 break;
1608 }
1609
1610 }
1611
Jiri Slaby0c419422021-06-18 08:14:22 +02001612 if (hwid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Jiri Slaby15517802021-06-18 08:14:18 +02001615 status = inb(port->ioaddr + UART_LSR);
1616 } while (status & UART_LSR_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Jiri Slaby0c419422021-06-18 08:14:22 +02001618 return status;
1619}
1620
1621static u8 mxser_receive_chars(struct tty_struct *tty,
1622 struct mxser_port *port, u8 status)
1623{
Jiri Slaby0c419422021-06-18 08:14:22 +02001624 if (tty->receive_room == 0 && !port->ldisc_stop_rx)
1625 mxser_stoprx(tty);
1626
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001627 if (!mxser_receive_chars_new(tty, port, status))
1628 status = mxser_receive_chars_old(tty, port, status);
Jiri Slaby0c419422021-06-18 08:14:22 +02001629
Jiri Slaby2e124b42013-01-03 15:53:06 +01001630 tty_flip_buffer_push(&port->port);
Jiri Slaby15517802021-06-18 08:14:18 +02001631
1632 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
Alan Cox216ba022008-10-13 10:40:19 +01001635static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 int count, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Jiri Slaby1c456072008-02-07 00:16:46 -08001639 if (port->x_char) {
1640 outb(port->x_char, port->ioaddr + UART_TX);
1641 port->x_char = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001642 port->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return;
1644 }
1645
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001646 if (port->port.xmit_buf == NULL)
Jiri Slaby1c456072008-02-07 00:16:46 -08001647 return;
1648
Jiri Slaby265ceff2021-06-18 08:15:06 +02001649 if (!port->xmit_cnt || tty->flow.stopped ||
Jiri Slaby5d1ea1a2021-06-18 08:15:12 +02001650 (tty->hw_stopped && !mxser_16550A_or_MUST(port))) {
Jiri Slaby740165f2021-06-18 08:15:05 +02001651 __mxser_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return;
1653 }
1654
Jiri Slaby1c456072008-02-07 00:16:46 -08001655 cnt = port->xmit_cnt;
1656 count = port->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 do {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001658 outb(port->port.xmit_buf[port->xmit_tail++],
Jiri Slaby1c456072008-02-07 00:16:46 -08001659 port->ioaddr + UART_TX);
1660 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
Jiri Slaby265ceff2021-06-18 08:15:06 +02001661 if (!--port->xmit_cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 break;
1663 } while (--count > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Jiri Slaby1c456072008-02-07 00:16:46 -08001665 port->icount.tx += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Kulikov Vasiliy464eb8f2010-07-23 20:34:53 +04001667 if (port->xmit_cnt < WAKEUP_CHARS)
Alan Cox216ba022008-10-13 10:40:19 +01001668 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Jiri Slaby265ceff2021-06-18 08:15:06 +02001670 if (!port->xmit_cnt)
Jiri Slaby740165f2021-06-18 08:15:05 +02001671 __mxser_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
Jiri Slaby9e40ea12021-06-18 08:14:19 +02001674static bool mxser_port_isr(struct mxser_port *port)
1675{
1676 struct tty_struct *tty;
1677 u8 iir, msr, status;
1678 bool error = false;
1679
1680 iir = inb(port->ioaddr + UART_IIR);
1681 if (iir & UART_IIR_NO_INT)
1682 return true;
1683
1684 iir &= MOXA_MUST_IIR_MASK;
1685 tty = tty_port_tty_get(&port->port);
1686 if (!tty || port->closing || !tty_port_initialized(&port->port)) {
1687 status = inb(port->ioaddr + UART_LSR);
Jiri Slabyd249e662021-09-22 09:59:38 +02001688 outb(port->FCR | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
Jiri Slabyaaa28e92021-06-18 08:15:07 +02001689 port->ioaddr + UART_FCR);
Jiri Slaby9e40ea12021-06-18 08:14:19 +02001690 inb(port->ioaddr + UART_MSR);
1691
1692 error = true;
1693 goto put_tty;
1694 }
1695
1696 status = inb(port->ioaddr + UART_LSR);
1697
Jiri Slaby9e40ea12021-06-18 08:14:19 +02001698 if (port->board->must_hwid) {
1699 if (iir == MOXA_MUST_IIR_GDA ||
1700 iir == MOXA_MUST_IIR_RDA ||
1701 iir == MOXA_MUST_IIR_RTO ||
1702 iir == MOXA_MUST_IIR_LSR)
1703 status = mxser_receive_chars(tty, port, status);
1704 } else {
1705 status &= port->read_status_mask;
1706 if (status & UART_LSR_DR)
1707 status = mxser_receive_chars(tty, port, status);
1708 }
1709
1710 msr = inb(port->ioaddr + UART_MSR);
1711 if (msr & UART_MSR_ANY_DELTA)
1712 mxser_check_modem_status(tty, port, msr);
1713
1714 if (port->board->must_hwid) {
1715 if (iir == 0x02 && (status & UART_LSR_THRE))
1716 mxser_transmit_chars(tty, port);
1717 } else {
1718 if (status & UART_LSR_THRE)
1719 mxser_transmit_chars(tty, port);
1720 }
1721
1722put_tty:
1723 tty_kref_put(tty);
1724
1725 return error;
1726}
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728/*
Jiri Slaby1c456072008-02-07 00:16:46 -08001729 * This is the serial driver's generic interrupt routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001731static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Jiri Slabycef222c2021-06-18 08:14:17 +02001733 struct mxser_board *brd = dev_id;
Jiri Slaby1c456072008-02-07 00:16:46 -08001734 struct mxser_port *port;
Jiri Slaby1c456072008-02-07 00:16:46 -08001735 unsigned int int_cnt, pass_counter = 0;
Jiri Slabyc24c31f2021-06-18 08:14:54 +02001736 unsigned int i, max = brd->nports;
Jiri Slaby1c456072008-02-07 00:16:46 -08001737 int handled = IRQ_NONE;
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001738 u8 irqbits, bits, mask = BIT(max) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Jiri Slaby1c456072008-02-07 00:16:46 -08001740 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001741 irqbits = inb(brd->vector) & mask;
1742 if (irqbits == mask)
Jiri Slaby1c456072008-02-07 00:16:46 -08001743 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Jiri Slaby1c456072008-02-07 00:16:46 -08001745 handled = IRQ_HANDLED;
1746 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001747 if (irqbits == mask)
Jiri Slaby1c456072008-02-07 00:16:46 -08001748 break;
1749 if (bits & irqbits)
1750 continue;
1751 port = &brd->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Jiri Slaby1c456072008-02-07 00:16:46 -08001753 int_cnt = 0;
1754 spin_lock(&port->slock);
1755 do {
Jiri Slaby9e40ea12021-06-18 08:14:19 +02001756 if (mxser_port_isr(port))
Jiri Slaby1c456072008-02-07 00:16:46 -08001757 break;
Jiri Slaby1c456072008-02-07 00:16:46 -08001758 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
1759 spin_unlock(&port->slock);
1760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762
Jiri Slaby1c456072008-02-07 00:16:46 -08001763 return handled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
Jiri Slaby1c456072008-02-07 00:16:46 -08001766static const struct tty_operations mxser_ops = {
1767 .open = mxser_open,
1768 .close = mxser_close,
1769 .write = mxser_write,
1770 .put_char = mxser_put_char,
1771 .flush_chars = mxser_flush_chars,
1772 .write_room = mxser_write_room,
1773 .chars_in_buffer = mxser_chars_in_buffer,
1774 .flush_buffer = mxser_flush_buffer,
1775 .ioctl = mxser_ioctl,
1776 .throttle = mxser_throttle,
1777 .unthrottle = mxser_unthrottle,
1778 .set_termios = mxser_set_termios,
1779 .stop = mxser_stop,
1780 .start = mxser_start,
1781 .hangup = mxser_hangup,
1782 .break_ctl = mxser_rs_break,
1783 .wait_until_sent = mxser_wait_until_sent,
1784 .tiocmget = mxser_tiocmget,
1785 .tiocmset = mxser_tiocmset,
Al Viro6da5b582018-09-11 22:22:06 -04001786 .set_serial = mxser_set_serial_info,
1787 .get_serial = mxser_get_serial_info,
Alan Cox05871022010-09-16 18:21:52 +01001788 .get_icount = mxser_get_icount,
Jiri Slaby1c456072008-02-07 00:16:46 -08001789};
1790
Aya Mahfouz04b757d2015-12-15 01:53:36 +02001791static const struct tty_port_operations mxser_port_ops = {
Alan Cox31f35932009-01-02 13:45:05 +00001792 .carrier_raised = mxser_carrier_raised,
Alan Coxfcc8ac12009-06-11 12:24:17 +01001793 .dtr_rts = mxser_dtr_rts,
Alan Cox67691402009-11-30 13:17:35 +00001794 .activate = mxser_activate,
1795 .shutdown = mxser_shutdown_port,
Alan Cox31f35932009-01-02 13:45:05 +00001796};
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798/*
Jiri Slaby1c456072008-02-07 00:16:46 -08001799 * The MOXA Smartio/Industio serial driver boot-time initialization code!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001801
Jiri Slabyc24c31f2021-06-18 08:14:54 +02001802static void mxser_initbrd(struct mxser_board *brd, bool high_baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Jiri Slaby1c456072008-02-07 00:16:46 -08001804 struct mxser_port *info;
1805 unsigned int i;
Jiri Slaby57faa7d2021-06-18 08:14:49 +02001806 bool is_mu860;
1807
1808 brd->must_hwid = mxser_must_get_hwid(brd->ports[0].ioaddr);
1809 is_mu860 = brd->must_hwid == MOXA_MUST_MU860_HWID;
1810
1811 for (i = 0; i < UART_INFO_NUM; i++) {
1812 if (Gpci_uart_info[i].type == brd->must_hwid) {
1813 brd->max_baud = Gpci_uart_info[i].max_baud;
1814
1815 /* exception....CP-102 */
Jiri Slabyc24c31f2021-06-18 08:14:54 +02001816 if (high_baud)
Jiri Slaby57faa7d2021-06-18 08:14:49 +02001817 brd->max_baud = 921600;
1818 break;
1819 }
1820 }
1821
1822 if (is_mu860) {
1823 /* set to RS232 mode by default */
1824 outb(0, brd->vector + 4);
1825 outb(0, brd->vector + 0x0c);
1826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Jiri Slabyc24c31f2021-06-18 08:14:54 +02001828 for (i = 0; i < brd->nports; i++) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001829 info = &brd->ports[i];
Jiri Slaby57faa7d2021-06-18 08:14:49 +02001830 if (is_mu860) {
1831 if (i < 4)
1832 info->opmode_ioaddr = brd->vector + 4;
1833 else
1834 info->opmode_ioaddr = brd->vector + 0x0c;
1835 }
Alan Cox44b7d1b2008-07-16 21:57:18 +01001836 tty_port_init(&info->port);
Alan Cox31f35932009-01-02 13:45:05 +00001837 info->port.ops = &mxser_port_ops;
Jiri Slaby1c456072008-02-07 00:16:46 -08001838 info->board = brd;
Jiri Slaby19236282021-09-22 09:59:34 +02001839 info->ldisc_stop_rx = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Jiri Slaby1c456072008-02-07 00:16:46 -08001841 /* Enhance mode enabled here */
Jiri Slaby292955a2021-06-18 08:14:13 +02001842 if (brd->must_hwid != MOXA_OTHER_UART)
Jiri Slabyedb7d272021-06-18 08:14:46 +02001843 mxser_must_set_enhance_mode(info->ioaddr, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Jiri Slaby58a2ddb2021-06-18 08:14:15 +02001845 info->type = PORT_16550A;
Jiri Slaby1c456072008-02-07 00:16:46 -08001846
Jiri Slabyc3db20c2021-06-18 08:14:55 +02001847 mxser_process_txrx_fifo(info);
Jiri Slaby1c456072008-02-07 00:16:46 -08001848
Alan Cox44b7d1b2008-07-16 21:57:18 +01001849 info->port.close_delay = 5 * HZ / 10;
1850 info->port.closing_wait = 30 * HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08001851 spin_lock_init(&info->slock);
1852
1853 /* before set INT ISR, disable all int */
1854 outb(inb(info->ioaddr + UART_IER) & 0xf0,
1855 info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858
Bill Pemberton9671f092012-11-19 13:21:50 -05001859static int mxser_probe(struct pci_dev *pdev,
Jiri Slaby1c456072008-02-07 00:16:46 -08001860 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Jiri Slaby1c456072008-02-07 00:16:46 -08001862 struct mxser_board *brd;
Jiri Slaby13d4aba2021-06-18 08:14:57 +02001863 unsigned int i, base;
Jiri Slaby1c456072008-02-07 00:16:46 -08001864 unsigned long ioaddress;
Jiri Slabyc24c31f2021-06-18 08:14:54 +02001865 unsigned short nports = MXSER_NPORTS(ent->driver_data);
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04001866 struct device *tty_dev;
Jiri Slaby1c456072008-02-07 00:16:46 -08001867 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Jiri Slabyf8b6b322021-06-18 08:14:58 +02001869 i = find_first_zero_bit(mxser_boards, MXSER_BOARDS);
Jiri Slaby1c456072008-02-07 00:16:46 -08001870 if (i >= MXSER_BOARDS) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07001871 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
1872 "not configured\n", MXSER_BOARDS);
Jiri Slaby1c456072008-02-07 00:16:46 -08001873 goto err;
1874 }
1875
Jiri Slabyad1c92f2021-06-18 08:14:59 +02001876 brd = devm_kzalloc(&pdev->dev, struct_size(brd, ports, nports),
1877 GFP_KERNEL);
Jiri Slabyf8b6b322021-06-18 08:14:58 +02001878 if (!brd)
1879 goto err;
1880
Jiri Slaby13d4aba2021-06-18 08:14:57 +02001881 brd->idx = i;
Jiri Slabyf8b6b322021-06-18 08:14:58 +02001882 __set_bit(brd->idx, mxser_boards);
Jiri Slaby13d4aba2021-06-18 08:14:57 +02001883 base = i * MXSER_PORTS_PER_BOARD;
Jiri Slaby1c456072008-02-07 00:16:46 -08001884
Jiri Slabydcb04e22021-06-18 08:14:51 +02001885 retval = pcim_enable_device(pdev);
Jiri Slaby1c456072008-02-07 00:16:46 -08001886 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07001887 dev_err(&pdev->dev, "PCI enable failed\n");
Jiri Slabyf8b6b322021-06-18 08:14:58 +02001888 goto err_zero;
Jiri Slaby1c456072008-02-07 00:16:46 -08001889 }
1890
1891 /* io address */
1892 ioaddress = pci_resource_start(pdev, 2);
1893 retval = pci_request_region(pdev, 2, "mxser(IO)");
1894 if (retval)
Jiri Slabyf8b6b322021-06-18 08:14:58 +02001895 goto err_zero;
Jiri Slaby1c456072008-02-07 00:16:46 -08001896
Jiri Slabyc24c31f2021-06-18 08:14:54 +02001897 brd->nports = nports;
1898 for (i = 0; i < nports; i++)
Jiri Slaby1c456072008-02-07 00:16:46 -08001899 brd->ports[i].ioaddr = ioaddress + 8 * i;
1900
1901 /* vector */
1902 ioaddress = pci_resource_start(pdev, 3);
1903 retval = pci_request_region(pdev, 3, "mxser(vector)");
1904 if (retval)
Jiri Slabydf480512010-09-03 10:31:37 +02001905 goto err_zero;
Jiri Slaby1c456072008-02-07 00:16:46 -08001906 brd->vector = ioaddress;
1907
1908 /* irq */
1909 brd->irq = pdev->irq;
1910
Jiri Slabyc24c31f2021-06-18 08:14:54 +02001911 mxser_initbrd(brd, ent->driver_data & MXSER_HIGHBAUD);
Jiri Slaby7f0e79d2021-06-18 08:14:52 +02001912
1913 retval = devm_request_irq(&pdev->dev, brd->irq, mxser_interrupt,
1914 IRQF_SHARED, "mxser", brd);
1915 if (retval) {
1916 dev_err(&pdev->dev, "request irq failed");
1917 goto err_relbrd;
1918 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001919
Jiri Slabyc24c31f2021-06-18 08:14:54 +02001920 for (i = 0; i < nports; i++) {
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04001921 tty_dev = tty_port_register_device(&brd->ports[i].port,
Jiri Slaby13d4aba2021-06-18 08:14:57 +02001922 mxvar_sdriver, base + i, &pdev->dev);
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04001923 if (IS_ERR(tty_dev)) {
1924 retval = PTR_ERR(tty_dev);
Alexey Khoroshilov1b581f12013-04-07 23:46:47 +04001925 for (; i > 0; i--)
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04001926 tty_unregister_device(mxvar_sdriver,
Jiri Slaby13d4aba2021-06-18 08:14:57 +02001927 base + i - 1);
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04001928 goto err_relbrd;
1929 }
1930 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001931
1932 pci_set_drvdata(pdev, brd);
1933
1934 return 0;
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04001935err_relbrd:
Jiri Slabyc24c31f2021-06-18 08:14:54 +02001936 for (i = 0; i < nports; i++)
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04001937 tty_port_destroy(&brd->ports[i].port);
Jiri Slabydf480512010-09-03 10:31:37 +02001938err_zero:
Jiri Slabyf8b6b322021-06-18 08:14:58 +02001939 __clear_bit(brd->idx, mxser_boards);
Jiri Slaby1c456072008-02-07 00:16:46 -08001940err:
1941 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001944static void mxser_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Jiri Slaby1c456072008-02-07 00:16:46 -08001946 struct mxser_board *brd = pci_get_drvdata(pdev);
Jiri Slaby13d4aba2021-06-18 08:14:57 +02001947 unsigned int i, base = brd->idx * MXSER_PORTS_PER_BOARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Jiri Slabyc24c31f2021-06-18 08:14:54 +02001949 for (i = 0; i < brd->nports; i++) {
Jiri Slaby13d4aba2021-06-18 08:14:57 +02001950 tty_unregister_device(mxvar_sdriver, base + i);
Jiri Slabyd450f082021-06-18 08:14:50 +02001951 tty_port_destroy(&brd->ports[i].port);
1952 }
1953
Jiri Slabyf8b6b322021-06-18 08:14:58 +02001954 __clear_bit(brd->idx, mxser_boards);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Jiri Slaby1c456072008-02-07 00:16:46 -08001957static struct pci_driver mxser_driver = {
1958 .name = "mxser",
1959 .id_table = mxser_pcibrds,
1960 .probe = mxser_probe,
Bill Pemberton91116cb2012-11-19 13:21:06 -05001961 .remove = mxser_remove
Jiri Slaby1c456072008-02-07 00:16:46 -08001962};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Jiri Slaby1c456072008-02-07 00:16:46 -08001964static int __init mxser_module_init(void)
1965{
Jiri Slaby1df00922008-07-25 01:48:22 -07001966 int retval;
Jiri Slaby1c456072008-02-07 00:16:46 -08001967
Jiri Slaby39b7b422021-07-23 09:43:13 +02001968 mxvar_sdriver = tty_alloc_driver(MXSER_PORTS, TTY_DRIVER_REAL_RAW |
1969 TTY_DRIVER_DYNAMIC_DEV);
1970 if (IS_ERR(mxvar_sdriver))
1971 return PTR_ERR(mxvar_sdriver);
Jiri Slaby1c456072008-02-07 00:16:46 -08001972
Jiri Slaby1c456072008-02-07 00:16:46 -08001973 /* Initialize the tty_driver structure */
Jiri Slaby1c456072008-02-07 00:16:46 -08001974 mxvar_sdriver->name = "ttyMI";
1975 mxvar_sdriver->major = ttymajor;
1976 mxvar_sdriver->minor_start = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001977 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
1978 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
1979 mxvar_sdriver->init_termios = tty_std_termios;
1980 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Jiri Slaby1c456072008-02-07 00:16:46 -08001981 tty_set_operations(mxvar_sdriver, &mxser_ops);
1982
1983 retval = tty_register_driver(mxvar_sdriver);
1984 if (retval) {
1985 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
1986 "tty driver !\n");
1987 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001989
Jiri Slaby1c456072008-02-07 00:16:46 -08001990 retval = pci_register_driver(&mxser_driver);
1991 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07001992 printk(KERN_ERR "mxser: can't register pci driver\n");
Jiri Slaby29134362021-06-18 08:14:07 +02001993 goto err_unr;
Jiri Slaby1c456072008-02-07 00:16:46 -08001994 }
1995
Jiri Slaby1c456072008-02-07 00:16:46 -08001996 return 0;
1997err_unr:
1998 tty_unregister_driver(mxvar_sdriver);
1999err_put:
Jiri Slaby9f90a4d2021-07-23 09:43:16 +02002000 tty_driver_kref_put(mxvar_sdriver);
Jiri Slaby1c456072008-02-07 00:16:46 -08002001 return retval;
2002}
2003
2004static void __exit mxser_module_exit(void)
2005{
Jiri Slaby1c456072008-02-07 00:16:46 -08002006 pci_unregister_driver(&mxser_driver);
Jiri Slaby1c456072008-02-07 00:16:46 -08002007 tty_unregister_driver(mxvar_sdriver);
Jiri Slaby9f90a4d2021-07-23 09:43:16 +02002008 tty_driver_kref_put(mxvar_sdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
2011module_init(mxser_module_init);
2012module_exit(mxser_module_exit);