xref: /openbmc/linux/drivers/tty/mxser.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2a6afd9f3SGreg Kroah-Hartman /*
3a6afd9f3SGreg Kroah-Hartman  *          mxser.c  -- MOXA Smartio/Industio family multiport serial driver.
4a6afd9f3SGreg Kroah-Hartman  *
5a6afd9f3SGreg Kroah-Hartman  *      Copyright (C) 1999-2006  Moxa Technologies (support@moxa.com).
6a6afd9f3SGreg Kroah-Hartman  *	Copyright (C) 2006-2008  Jiri Slaby <jirislaby@gmail.com>
7a6afd9f3SGreg Kroah-Hartman  *
8a6afd9f3SGreg Kroah-Hartman  *      This code is loosely based on the 1.8 moxa driver which is based on
9a6afd9f3SGreg Kroah-Hartman  *	Linux serial driver, written by Linus Torvalds, Theodore T'so and
10a6afd9f3SGreg Kroah-Hartman  *	others.
11a6afd9f3SGreg Kroah-Hartman  *
12a6afd9f3SGreg Kroah-Hartman  *	Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
13a6afd9f3SGreg Kroah-Hartman  *	<alan@lxorguk.ukuu.org.uk>. The original 1.8 code is available on
14a6afd9f3SGreg Kroah-Hartman  *	www.moxa.com.
15a6afd9f3SGreg Kroah-Hartman  *	- Fixed x86_64 cleanness
16a6afd9f3SGreg Kroah-Hartman  */
17a6afd9f3SGreg Kroah-Hartman 
18a6afd9f3SGreg Kroah-Hartman #include <linux/module.h>
19a6afd9f3SGreg Kroah-Hartman #include <linux/errno.h>
20a6afd9f3SGreg Kroah-Hartman #include <linux/signal.h>
21a6afd9f3SGreg Kroah-Hartman #include <linux/sched.h>
22a6afd9f3SGreg Kroah-Hartman #include <linux/timer.h>
23a6afd9f3SGreg Kroah-Hartman #include <linux/interrupt.h>
24a6afd9f3SGreg Kroah-Hartman #include <linux/tty.h>
25a6afd9f3SGreg Kroah-Hartman #include <linux/tty_flip.h>
26a6afd9f3SGreg Kroah-Hartman #include <linux/serial.h>
27a6afd9f3SGreg Kroah-Hartman #include <linux/serial_reg.h>
28a6afd9f3SGreg Kroah-Hartman #include <linux/major.h>
29a6afd9f3SGreg Kroah-Hartman #include <linux/string.h>
30a6afd9f3SGreg Kroah-Hartman #include <linux/fcntl.h>
31a6afd9f3SGreg Kroah-Hartman #include <linux/ptrace.h>
32a6afd9f3SGreg Kroah-Hartman #include <linux/ioport.h>
33a6afd9f3SGreg Kroah-Hartman #include <linux/mm.h>
34a6afd9f3SGreg Kroah-Hartman #include <linux/delay.h>
35a6afd9f3SGreg Kroah-Hartman #include <linux/pci.h>
36a6afd9f3SGreg Kroah-Hartman #include <linux/bitops.h>
37a6afd9f3SGreg Kroah-Hartman #include <linux/slab.h>
385a3c6b25SManuel Zerpies #include <linux/ratelimit.h>
39a6afd9f3SGreg Kroah-Hartman 
40a6afd9f3SGreg Kroah-Hartman #include <asm/io.h>
41a6afd9f3SGreg Kroah-Hartman #include <asm/irq.h>
427c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
43a6afd9f3SGreg Kroah-Hartman 
444463cc5bSJiri Slaby /*
454463cc5bSJiri Slaby  *	Semi-public control interfaces
464463cc5bSJiri Slaby  */
474463cc5bSJiri Slaby 
484463cc5bSJiri Slaby /*
494463cc5bSJiri Slaby  *	MOXA ioctls
504463cc5bSJiri Slaby  */
514463cc5bSJiri Slaby 
524463cc5bSJiri Slaby #define MOXA			0x400
534463cc5bSJiri Slaby #define MOXA_SET_OP_MODE	(MOXA + 66)
544463cc5bSJiri Slaby #define MOXA_GET_OP_MODE	(MOXA + 67)
554463cc5bSJiri Slaby 
564463cc5bSJiri Slaby #define RS232_MODE		0
574463cc5bSJiri Slaby #define RS485_2WIRE_MODE	1
584463cc5bSJiri Slaby #define RS422_MODE		2
594463cc5bSJiri Slaby #define RS485_4WIRE_MODE	3
604463cc5bSJiri Slaby #define OP_MODE_MASK		3
614463cc5bSJiri Slaby 
624463cc5bSJiri Slaby /* --------------------------------------------------- */
634463cc5bSJiri Slaby 
644463cc5bSJiri Slaby /*
654463cc5bSJiri Slaby  * Follow just what Moxa Must chip defines.
664463cc5bSJiri Slaby  *
67464fbf6cSJiri Slaby  * When LCR register (offset 0x03) is written the following value, the Must chip
68464fbf6cSJiri Slaby  * will enter enhanced mode. And a write to EFR (offset 0x02) bit 6,7 will
694463cc5bSJiri Slaby  * change bank.
704463cc5bSJiri Slaby  */
71464fbf6cSJiri Slaby #define MOXA_MUST_ENTER_ENHANCED	0xBF
724463cc5bSJiri Slaby 
73464fbf6cSJiri Slaby /* when enhanced mode is enabled, access to general bank register */
744463cc5bSJiri Slaby #define MOXA_MUST_GDL_REGISTER		0x07
754463cc5bSJiri Slaby #define MOXA_MUST_GDL_MASK		0x7F
764463cc5bSJiri Slaby #define MOXA_MUST_GDL_HAS_BAD_DATA	0x80
774463cc5bSJiri Slaby 
784463cc5bSJiri Slaby #define MOXA_MUST_LSR_RERR		0x80	/* error in receive FIFO */
79464fbf6cSJiri Slaby /* enhanced register bank select and enhanced mode setting register */
80464fbf6cSJiri Slaby /* This works only when LCR register equals to 0xBF */
814463cc5bSJiri Slaby #define MOXA_MUST_EFR_REGISTER		0x02
82464fbf6cSJiri Slaby #define MOXA_MUST_EFR_EFRB_ENABLE	0x10 /* enhanced mode enable */
83464fbf6cSJiri Slaby /* enhanced register bank set 0, 1, 2 */
844463cc5bSJiri Slaby #define MOXA_MUST_EFR_BANK0		0x00
854463cc5bSJiri Slaby #define MOXA_MUST_EFR_BANK1		0x40
864463cc5bSJiri Slaby #define MOXA_MUST_EFR_BANK2		0x80
874463cc5bSJiri Slaby #define MOXA_MUST_EFR_BANK3		0xC0
884463cc5bSJiri Slaby #define MOXA_MUST_EFR_BANK_MASK		0xC0
894463cc5bSJiri Slaby 
904463cc5bSJiri Slaby /* set XON1 value register, when LCR=0xBF and change to bank0 */
914463cc5bSJiri Slaby #define MOXA_MUST_XON1_REGISTER		0x04
924463cc5bSJiri Slaby 
934463cc5bSJiri Slaby /* set XON2 value register, when LCR=0xBF and change to bank0 */
944463cc5bSJiri Slaby #define MOXA_MUST_XON2_REGISTER		0x05
954463cc5bSJiri Slaby 
964463cc5bSJiri Slaby /* set XOFF1 value register, when LCR=0xBF and change to bank0 */
974463cc5bSJiri Slaby #define MOXA_MUST_XOFF1_REGISTER	0x06
984463cc5bSJiri Slaby 
994463cc5bSJiri Slaby /* set XOFF2 value register, when LCR=0xBF and change to bank0 */
1004463cc5bSJiri Slaby #define MOXA_MUST_XOFF2_REGISTER	0x07
1014463cc5bSJiri Slaby 
1024463cc5bSJiri Slaby #define MOXA_MUST_RBRTL_REGISTER	0x04
1034463cc5bSJiri Slaby #define MOXA_MUST_RBRTH_REGISTER	0x05
1044463cc5bSJiri Slaby #define MOXA_MUST_RBRTI_REGISTER	0x06
1054463cc5bSJiri Slaby #define MOXA_MUST_THRTL_REGISTER	0x07
1064463cc5bSJiri Slaby #define MOXA_MUST_ENUM_REGISTER		0x04
1074463cc5bSJiri Slaby #define MOXA_MUST_HWID_REGISTER		0x05
1084463cc5bSJiri Slaby #define MOXA_MUST_ECR_REGISTER		0x06
1094463cc5bSJiri Slaby #define MOXA_MUST_CSR_REGISTER		0x07
1104463cc5bSJiri Slaby 
1114463cc5bSJiri Slaby #define MOXA_MUST_FCR_GDA_MODE_ENABLE	0x20 /* good data mode enable */
1124463cc5bSJiri Slaby #define MOXA_MUST_FCR_GDA_ONLY_ENABLE	0x10 /* only good data put into RxFIFO */
1134463cc5bSJiri Slaby 
1144463cc5bSJiri Slaby #define MOXA_MUST_IER_ECTSI		0x80 /* enable CTS interrupt */
1154463cc5bSJiri Slaby #define MOXA_MUST_IER_ERTSI		0x40 /* enable RTS interrupt */
1164463cc5bSJiri Slaby #define MOXA_MUST_IER_XINT		0x20 /* enable Xon/Xoff interrupt */
1174463cc5bSJiri Slaby #define MOXA_MUST_IER_EGDAI		0x10 /* enable GDA interrupt */
1184463cc5bSJiri Slaby 
1194463cc5bSJiri Slaby #define MOXA_MUST_RECV_ISR		(UART_IER_RDI | MOXA_MUST_IER_EGDAI)
1204463cc5bSJiri Slaby 
1214463cc5bSJiri Slaby /* GDA interrupt pending */
1224463cc5bSJiri Slaby #define MOXA_MUST_IIR_GDA		0x1C
1234463cc5bSJiri Slaby #define MOXA_MUST_IIR_RDA		0x04
1244463cc5bSJiri Slaby #define MOXA_MUST_IIR_RTO		0x0C
1254463cc5bSJiri Slaby #define MOXA_MUST_IIR_LSR		0x06
1264463cc5bSJiri Slaby 
1274463cc5bSJiri Slaby /* received Xon/Xoff or specical interrupt pending */
1284463cc5bSJiri Slaby #define MOXA_MUST_IIR_XSC		0x10
1294463cc5bSJiri Slaby 
1304463cc5bSJiri Slaby /* RTS/CTS change state interrupt pending */
1314463cc5bSJiri Slaby #define MOXA_MUST_IIR_RTSCTS		0x20
1324463cc5bSJiri Slaby #define MOXA_MUST_IIR_MASK		0x3E
1334463cc5bSJiri Slaby 
1344463cc5bSJiri Slaby #define MOXA_MUST_MCR_XON_FLAG		0x40
1354463cc5bSJiri Slaby #define MOXA_MUST_MCR_XON_ANY		0x80
1364463cc5bSJiri Slaby #define MOXA_MUST_MCR_TX_XON		0x08
1374463cc5bSJiri Slaby 
1384463cc5bSJiri Slaby #define MOXA_MUST_EFR_SF_MASK		0x0F /* software flow control on chip mask value */
1394463cc5bSJiri Slaby #define MOXA_MUST_EFR_SF_TX1		0x08 /* send Xon1/Xoff1 */
1404463cc5bSJiri Slaby #define MOXA_MUST_EFR_SF_TX2		0x04 /* send Xon2/Xoff2 */
1414463cc5bSJiri Slaby #define MOXA_MUST_EFR_SF_TX12		0x0C /* send Xon1,Xon2/Xoff1,Xoff2 */
1424463cc5bSJiri Slaby #define MOXA_MUST_EFR_SF_TX_NO		0x00 /* don't send Xon/Xoff */
1434463cc5bSJiri Slaby #define MOXA_MUST_EFR_SF_TX_MASK	0x0C /* Tx software flow control mask */
1444463cc5bSJiri Slaby #define MOXA_MUST_EFR_SF_RX_NO		0x00 /* don't receive Xon/Xoff */
1454463cc5bSJiri Slaby #define MOXA_MUST_EFR_SF_RX1		0x02 /* receive Xon1/Xoff1 */
1464463cc5bSJiri Slaby #define MOXA_MUST_EFR_SF_RX2		0x01 /* receive Xon2/Xoff2 */
1474463cc5bSJiri Slaby #define MOXA_MUST_EFR_SF_RX12		0x03 /* receive Xon1,Xon2/Xoff1,Xoff2 */
1484463cc5bSJiri Slaby #define MOXA_MUST_EFR_SF_RX_MASK	0x03 /* Rx software flow control mask */
149a6afd9f3SGreg Kroah-Hartman 
150a6afd9f3SGreg Kroah-Hartman #define	MXSERMAJOR	 174
151a6afd9f3SGreg Kroah-Hartman 
152a6afd9f3SGreg Kroah-Hartman #define MXSER_BOARDS		4	/* Max. boards */
153a6afd9f3SGreg Kroah-Hartman #define MXSER_PORTS_PER_BOARD	8	/* Max. ports per board */
154a6afd9f3SGreg Kroah-Hartman #define MXSER_PORTS		(MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
155a6afd9f3SGreg Kroah-Hartman #define MXSER_ISR_PASS_LIMIT	100
156a6afd9f3SGreg Kroah-Hartman 
157a6afd9f3SGreg Kroah-Hartman #define WAKEUP_CHARS		256
158a6afd9f3SGreg Kroah-Hartman 
159a6970c39SJiri Slaby #define MXSER_BAUD_BASE		921600
160d811b26bSJiri Slaby #define MXSER_CUSTOM_DIVISOR	(MXSER_BAUD_BASE * 16)
161a6970c39SJiri Slaby 
1624167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_RC7000	0x0001
1634167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP102	0x1020
1644167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP102UL	0x1021
1654167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP102U	0x1022
1664167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP102UF	0x1023
1674167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_C104		0x1040
1684167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP104U	0x1041
1694167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP104JU	0x1042
1704167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP104EL	0x1043
17116add04fSJiri Slaby #define PCI_DEVICE_ID_MOXA_POS104UL	0x1044
17216add04fSJiri Slaby #define PCI_DEVICE_ID_MOXA_CB108	0x1080
17316add04fSJiri Slaby #define PCI_DEVICE_ID_MOXA_CP112UL	0x1120
1744167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CT114	0x1140
1754167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP114	0x1141
17616add04fSJiri Slaby #define PCI_DEVICE_ID_MOXA_CB114	0x1142
17716add04fSJiri Slaby #define PCI_DEVICE_ID_MOXA_CP114UL	0x1143
1784167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP118U	0x1180
1794167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP118EL	0x1181
1804167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP132	0x1320
1814167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP132U	0x1321
1824167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP134U	0x1340
18316add04fSJiri Slaby #define PCI_DEVICE_ID_MOXA_CB134I	0x1341
18416add04fSJiri Slaby #define PCI_DEVICE_ID_MOXA_CP138U	0x1380
1854167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_C168		0x1680
1864167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP168U	0x1681
1874167bd25SJiri Slaby #define PCI_DEVICE_ID_MOXA_CP168EL	0x1682
188a6afd9f3SGreg Kroah-Hartman 
189c24c31ffSJiri Slaby #define MXSER_NPORTS(ddata)		((ddata) & 0xffU)
190c24c31ffSJiri Slaby #define MXSER_HIGHBAUD			0x0100
191a6afd9f3SGreg Kroah-Hartman 
192e4558366SJiri Slaby enum mxser_must_hwid {
193e4558366SJiri Slaby 	MOXA_OTHER_UART		= 0x00,
194e4558366SJiri Slaby 	MOXA_MUST_MU150_HWID	= 0x01,
195e4558366SJiri Slaby 	MOXA_MUST_MU860_HWID	= 0x02,
196e4558366SJiri Slaby };
197e4558366SJiri Slaby 
198a6afd9f3SGreg Kroah-Hartman static const struct {
199dc33f644SJiri Slaby 	u8 type;
200dc33f644SJiri Slaby 	u8 fifo_size;
201dc33f644SJiri Slaby 	u8 rx_high_water;
202dc33f644SJiri Slaby 	u8 rx_low_water;
203dc33f644SJiri Slaby 	speed_t max_baud;
204a6afd9f3SGreg Kroah-Hartman } Gpci_uart_info[] = {
205dc33f644SJiri Slaby 	{ MOXA_OTHER_UART,	 16, 14,  1, 921600 },
206dc33f644SJiri Slaby 	{ MOXA_MUST_MU150_HWID,	 64, 48, 16, 230400 },
207dc33f644SJiri Slaby 	{ MOXA_MUST_MU860_HWID, 128, 96, 32, 921600 }
208a6afd9f3SGreg Kroah-Hartman };
209a6afd9f3SGreg Kroah-Hartman #define UART_INFO_NUM	ARRAY_SIZE(Gpci_uart_info)
210a6afd9f3SGreg Kroah-Hartman 
211a6afd9f3SGreg Kroah-Hartman 
212a6afd9f3SGreg Kroah-Hartman /* driver_data correspond to the lines in the structure above
213a6afd9f3SGreg Kroah-Hartman    see also ISA probe function before you change something */
2143385ecf8SArvind Yadav static const struct pci_device_id mxser_pcibrds[] = {
215c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, C168,		8) },
216c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, C104,		4) },
217c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP132,		2) },
218c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP114,		4) },
219c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CT114,		4) },
220c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP102,		2 | MXSER_HIGHBAUD) },
221c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP104U,		4) },
222c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP168U,		8) },
223c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP132U,		2) },
224c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP134U,		4) },
225c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP104JU,	4) },
226c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, RC7000,		8) }, /* RC7000 */
227c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP118U,		8) },
228c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP102UL,	2) },
229c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP102U,		2) },
230c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP118EL,	8) },
231c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP168EL,	8) },
232c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP104EL,	4) },
233c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CB108,		8) },
234c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CB114,		4) },
235c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CB134I,		4) },
236c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP138U,		8) },
237c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, POS104UL,	4) },
238c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP114UL,	4) },
239c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP102UF,	2) },
240c668d567SJiri Slaby 	{ PCI_DEVICE_DATA(MOXA, CP112UL,	2) },
241a6afd9f3SGreg Kroah-Hartman 	{ }
242a6afd9f3SGreg Kroah-Hartman };
243a6afd9f3SGreg Kroah-Hartman MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
244a6afd9f3SGreg Kroah-Hartman 
245a6afd9f3SGreg Kroah-Hartman static int ttymajor = MXSERMAJOR;
246a6afd9f3SGreg Kroah-Hartman 
247a6afd9f3SGreg Kroah-Hartman /* Variables for insmod */
248a6afd9f3SGreg Kroah-Hartman 
249a6afd9f3SGreg Kroah-Hartman MODULE_AUTHOR("Casper Yang");
250a6afd9f3SGreg Kroah-Hartman MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
251a6afd9f3SGreg Kroah-Hartman module_param(ttymajor, int, 0);
252a6afd9f3SGreg Kroah-Hartman MODULE_LICENSE("GPL");
253a6afd9f3SGreg Kroah-Hartman 
254a6afd9f3SGreg Kroah-Hartman struct mxser_board;
255a6afd9f3SGreg Kroah-Hartman 
256a6afd9f3SGreg Kroah-Hartman struct mxser_port {
257a6afd9f3SGreg Kroah-Hartman 	struct tty_port port;
258a6afd9f3SGreg Kroah-Hartman 	struct mxser_board *board;
259a6afd9f3SGreg Kroah-Hartman 
260a6afd9f3SGreg Kroah-Hartman 	unsigned long ioaddr;
261a6afd9f3SGreg Kroah-Hartman 	unsigned long opmode_ioaddr;
262a6afd9f3SGreg Kroah-Hartman 
263dc33f644SJiri Slaby 	u8 rx_high_water;
264dc33f644SJiri Slaby 	u8 rx_low_water;
265a6afd9f3SGreg Kroah-Hartman 	int type;		/* UART type */
266a6afd9f3SGreg Kroah-Hartman 
267a93963e4SJiri Slaby 	unsigned char x_char;	/* xon/xoff character */
268a93963e4SJiri Slaby 	u8 IER;			/* Interrupt Enable Register */
269a93963e4SJiri Slaby 	u8 MCR;			/* Modem control register */
270d249e662SJiri Slaby 	u8 FCR;			/* FIFO control register */
271a6afd9f3SGreg Kroah-Hartman 
272a6afd9f3SGreg Kroah-Hartman 	struct async_icount icount; /* kernel counters for 4 input interrupts */
273104583b5SJiri Slaby 	unsigned int timeout;
274a6afd9f3SGreg Kroah-Hartman 
275a93963e4SJiri Slaby 	u8 read_status_mask;
276a93963e4SJiri Slaby 	u8 ignore_status_mask;
277dc33f644SJiri Slaby 	u8 xmit_fifo_size;
278a6afd9f3SGreg Kroah-Hartman 
279a6afd9f3SGreg Kroah-Hartman 	spinlock_t slock;
280a6afd9f3SGreg Kroah-Hartman };
281a6afd9f3SGreg Kroah-Hartman 
282a6afd9f3SGreg Kroah-Hartman struct mxser_board {
283a6afd9f3SGreg Kroah-Hartman 	unsigned int idx;
284c24c31ffSJiri Slaby 	unsigned short nports;
285a6afd9f3SGreg Kroah-Hartman 	int irq;
286a6afd9f3SGreg Kroah-Hartman 	unsigned long vector;
287a6afd9f3SGreg Kroah-Hartman 
288e4558366SJiri Slaby 	enum mxser_must_hwid must_hwid;
289928f9464SJiri Slaby 	speed_t max_baud;
290a6afd9f3SGreg Kroah-Hartman 
291ad1c92ffSJiri Slaby 	struct mxser_port ports[];
292a6afd9f3SGreg Kroah-Hartman };
293a6afd9f3SGreg Kroah-Hartman 
294f8b6b327SJiri Slaby static DECLARE_BITMAP(mxser_boards, MXSER_BOARDS);
295a6afd9f3SGreg Kroah-Hartman static struct tty_driver *mxvar_sdriver;
296a6afd9f3SGreg Kroah-Hartman 
__mxser_must_set_EFR(unsigned long baseio,u8 clear,u8 set,bool restore_LCR)297edb7d27cSJiri Slaby static u8 __mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set,
298edb7d27cSJiri Slaby 		bool restore_LCR)
299a6afd9f3SGreg Kroah-Hartman {
300edb7d27cSJiri Slaby 	u8 oldlcr, efr;
301a6afd9f3SGreg Kroah-Hartman 
302a6afd9f3SGreg Kroah-Hartman 	oldlcr = inb(baseio + UART_LCR);
303464fbf6cSJiri Slaby 	outb(MOXA_MUST_ENTER_ENHANCED, baseio + UART_LCR);
304a6afd9f3SGreg Kroah-Hartman 
305a6afd9f3SGreg Kroah-Hartman 	efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
306edb7d27cSJiri Slaby 	efr &= ~clear;
307edb7d27cSJiri Slaby 	efr |= set;
308a6afd9f3SGreg Kroah-Hartman 
309a6afd9f3SGreg Kroah-Hartman 	outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
310edb7d27cSJiri Slaby 
311edb7d27cSJiri Slaby 	if (restore_LCR)
312a6afd9f3SGreg Kroah-Hartman 		outb(oldlcr, baseio + UART_LCR);
313a6afd9f3SGreg Kroah-Hartman 
314edb7d27cSJiri Slaby 	return oldlcr;
315a6afd9f3SGreg Kroah-Hartman }
316a6afd9f3SGreg Kroah-Hartman 
mxser_must_select_bank(unsigned long baseio,u8 bank)317b286484bSJiri Slaby static u8 mxser_must_select_bank(unsigned long baseio, u8 bank)
318b286484bSJiri Slaby {
319b286484bSJiri Slaby 	return __mxser_must_set_EFR(baseio, MOXA_MUST_EFR_BANK_MASK, bank,
320b286484bSJiri Slaby 			false);
321b286484bSJiri Slaby }
322b286484bSJiri Slaby 
mxser_set_must_xon1_value(unsigned long baseio,u8 value)323a6afd9f3SGreg Kroah-Hartman static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
324a6afd9f3SGreg Kroah-Hartman {
325b286484bSJiri Slaby 	u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK0);
326a6afd9f3SGreg Kroah-Hartman 	outb(value, baseio + MOXA_MUST_XON1_REGISTER);
327a6afd9f3SGreg Kroah-Hartman 	outb(oldlcr, baseio + UART_LCR);
328a6afd9f3SGreg Kroah-Hartman }
329a6afd9f3SGreg Kroah-Hartman 
mxser_set_must_xoff1_value(unsigned long baseio,u8 value)330a6afd9f3SGreg Kroah-Hartman static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
331a6afd9f3SGreg Kroah-Hartman {
332b286484bSJiri Slaby 	u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK0);
333a6afd9f3SGreg Kroah-Hartman 	outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
334a6afd9f3SGreg Kroah-Hartman 	outb(oldlcr, baseio + UART_LCR);
335a6afd9f3SGreg Kroah-Hartman }
336a6afd9f3SGreg Kroah-Hartman 
mxser_set_must_fifo_value(struct mxser_port * info)337a6afd9f3SGreg Kroah-Hartman static void mxser_set_must_fifo_value(struct mxser_port *info)
338a6afd9f3SGreg Kroah-Hartman {
339b286484bSJiri Slaby 	u8 oldlcr = mxser_must_select_bank(info->ioaddr, MOXA_MUST_EFR_BANK1);
340dc33f644SJiri Slaby 	outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
341dc33f644SJiri Slaby 	outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
342dc33f644SJiri Slaby 	outb(info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
343a6afd9f3SGreg Kroah-Hartman 	outb(oldlcr, info->ioaddr + UART_LCR);
344a6afd9f3SGreg Kroah-Hartman }
345a6afd9f3SGreg Kroah-Hartman 
mxser_set_must_enum_value(unsigned long baseio,u8 value)346a6afd9f3SGreg Kroah-Hartman static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
347a6afd9f3SGreg Kroah-Hartman {
348b286484bSJiri Slaby 	u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK2);
349a6afd9f3SGreg Kroah-Hartman 	outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
350a6afd9f3SGreg Kroah-Hartman 	outb(oldlcr, baseio + UART_LCR);
351a6afd9f3SGreg Kroah-Hartman }
352a6afd9f3SGreg Kroah-Hartman 
mxser_get_must_hardware_id(unsigned long baseio)353b286484bSJiri Slaby static u8 mxser_get_must_hardware_id(unsigned long baseio)
354a6afd9f3SGreg Kroah-Hartman {
355b286484bSJiri Slaby 	u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK2);
356b286484bSJiri Slaby 	u8 id = inb(baseio + MOXA_MUST_HWID_REGISTER);
357a6afd9f3SGreg Kroah-Hartman 	outb(oldlcr, baseio + UART_LCR);
358b286484bSJiri Slaby 
359b286484bSJiri Slaby 	return id;
360a6afd9f3SGreg Kroah-Hartman }
361a6afd9f3SGreg Kroah-Hartman 
mxser_must_set_EFR(unsigned long baseio,u8 clear,u8 set)362edb7d27cSJiri Slaby static void mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set)
363edb7d27cSJiri Slaby {
364edb7d27cSJiri Slaby 	__mxser_must_set_EFR(baseio, clear, set, true);
365edb7d27cSJiri Slaby }
366edb7d27cSJiri Slaby 
mxser_must_set_enhance_mode(unsigned long baseio,bool enable)367edb7d27cSJiri Slaby static void mxser_must_set_enhance_mode(unsigned long baseio, bool enable)
368edb7d27cSJiri Slaby {
369edb7d27cSJiri Slaby 	mxser_must_set_EFR(baseio,
370edb7d27cSJiri Slaby 			enable ? 0 : MOXA_MUST_EFR_EFRB_ENABLE,
371edb7d27cSJiri Slaby 			enable ? MOXA_MUST_EFR_EFRB_ENABLE : 0);
372edb7d27cSJiri Slaby }
373edb7d27cSJiri Slaby 
mxser_must_no_sw_flow_control(unsigned long baseio)374b441eb0fSJiri Slaby static void mxser_must_no_sw_flow_control(unsigned long baseio)
375a6afd9f3SGreg Kroah-Hartman {
376b441eb0fSJiri Slaby 	mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_MASK, 0);
377a6afd9f3SGreg Kroah-Hartman }
378a6afd9f3SGreg Kroah-Hartman 
mxser_must_set_tx_sw_flow_control(unsigned long baseio,bool enable)379b441eb0fSJiri Slaby static void mxser_must_set_tx_sw_flow_control(unsigned long baseio, bool enable)
380a6afd9f3SGreg Kroah-Hartman {
381b441eb0fSJiri Slaby 	mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_TX_MASK,
382b441eb0fSJiri Slaby 			enable ? MOXA_MUST_EFR_SF_TX1 : 0);
383a6afd9f3SGreg Kroah-Hartman }
384a6afd9f3SGreg Kroah-Hartman 
mxser_must_set_rx_sw_flow_control(unsigned long baseio,bool enable)385b441eb0fSJiri Slaby static void mxser_must_set_rx_sw_flow_control(unsigned long baseio, bool enable)
386a6afd9f3SGreg Kroah-Hartman {
387b441eb0fSJiri Slaby 	mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_RX_MASK,
388b441eb0fSJiri Slaby 			enable ? MOXA_MUST_EFR_SF_RX1 : 0);
389a6afd9f3SGreg Kroah-Hartman }
390a6afd9f3SGreg Kroah-Hartman 
mxser_must_get_hwid(unsigned long io)391e4558366SJiri Slaby static enum mxser_must_hwid mxser_must_get_hwid(unsigned long io)
392a6afd9f3SGreg Kroah-Hartman {
393a6afd9f3SGreg Kroah-Hartman 	u8 oldmcr, hwid;
394a6afd9f3SGreg Kroah-Hartman 	int i;
395a6afd9f3SGreg Kroah-Hartman 
396a6afd9f3SGreg Kroah-Hartman 	outb(0, io + UART_LCR);
397edb7d27cSJiri Slaby 	mxser_must_set_enhance_mode(io, false);
398a6afd9f3SGreg Kroah-Hartman 	oldmcr = inb(io + UART_MCR);
399a6afd9f3SGreg Kroah-Hartman 	outb(0, io + UART_MCR);
400a6afd9f3SGreg Kroah-Hartman 	mxser_set_must_xon1_value(io, 0x11);
401e4cdd25cSColin Ian King 	if (inb(io + UART_MCR) != 0) {
402a6afd9f3SGreg Kroah-Hartman 		outb(oldmcr, io + UART_MCR);
403a6afd9f3SGreg Kroah-Hartman 		return MOXA_OTHER_UART;
404a6afd9f3SGreg Kroah-Hartman 	}
405a6afd9f3SGreg Kroah-Hartman 
406b286484bSJiri Slaby 	hwid = mxser_get_must_hardware_id(io);
407e4558366SJiri Slaby 	for (i = 1; i < UART_INFO_NUM; i++) /* 0 = OTHER_UART */
408a6afd9f3SGreg Kroah-Hartman 		if (hwid == Gpci_uart_info[i].type)
409e4558366SJiri Slaby 			return hwid;
410e4558366SJiri Slaby 
411a6afd9f3SGreg Kroah-Hartman 	return MOXA_OTHER_UART;
412a6afd9f3SGreg Kroah-Hartman }
413a6afd9f3SGreg Kroah-Hartman 
mxser_16550A_or_MUST(struct mxser_port * info)4145d1ea1adSJiri Slaby static bool mxser_16550A_or_MUST(struct mxser_port *info)
4155d1ea1adSJiri Slaby {
4165d1ea1adSJiri Slaby 	return info->type == PORT_16550A || info->board->must_hwid;
4175d1ea1adSJiri Slaby }
4185d1ea1adSJiri Slaby 
mxser_process_txrx_fifo(struct mxser_port * info)419c3db20c3SJiri Slaby static void mxser_process_txrx_fifo(struct mxser_port *info)
420a6afd9f3SGreg Kroah-Hartman {
421c3db20c3SJiri Slaby 	unsigned int i;
422a6afd9f3SGreg Kroah-Hartman 
423c3db20c3SJiri Slaby 	if (info->type == PORT_16450 || info->type == PORT_8250) {
424a6afd9f3SGreg Kroah-Hartman 		info->rx_high_water = 1;
425a6afd9f3SGreg Kroah-Hartman 		info->rx_low_water = 1;
426a6afd9f3SGreg Kroah-Hartman 		info->xmit_fifo_size = 1;
427c3db20c3SJiri Slaby 		return;
428c3db20c3SJiri Slaby 	}
429c3db20c3SJiri Slaby 
430a6afd9f3SGreg Kroah-Hartman 	for (i = 0; i < UART_INFO_NUM; i++)
431292955a7SJiri Slaby 		if (info->board->must_hwid == Gpci_uart_info[i].type) {
432a6afd9f3SGreg Kroah-Hartman 			info->rx_low_water = Gpci_uart_info[i].rx_low_water;
433a6afd9f3SGreg Kroah-Hartman 			info->rx_high_water = Gpci_uart_info[i].rx_high_water;
434dc33f644SJiri Slaby 			info->xmit_fifo_size = Gpci_uart_info[i].fifo_size;
435a6afd9f3SGreg Kroah-Hartman 			break;
436a6afd9f3SGreg Kroah-Hartman 		}
437a6afd9f3SGreg Kroah-Hartman }
438a6afd9f3SGreg Kroah-Hartman 
__mxser_start_tx(struct mxser_port * info)439740165f7SJiri Slaby static void __mxser_start_tx(struct mxser_port *info)
440740165f7SJiri Slaby {
441740165f7SJiri Slaby 	outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
442740165f7SJiri Slaby 	info->IER |= UART_IER_THRI;
443740165f7SJiri Slaby 	outb(info->IER, info->ioaddr + UART_IER);
444740165f7SJiri Slaby }
445740165f7SJiri Slaby 
mxser_start_tx(struct mxser_port * info)446740165f7SJiri Slaby static void mxser_start_tx(struct mxser_port *info)
447740165f7SJiri Slaby {
448740165f7SJiri Slaby 	unsigned long flags;
449740165f7SJiri Slaby 
450740165f7SJiri Slaby 	spin_lock_irqsave(&info->slock, flags);
451740165f7SJiri Slaby 	__mxser_start_tx(info);
452740165f7SJiri Slaby 	spin_unlock_irqrestore(&info->slock, flags);
453740165f7SJiri Slaby }
454740165f7SJiri Slaby 
__mxser_stop_tx(struct mxser_port * info)455740165f7SJiri Slaby static void __mxser_stop_tx(struct mxser_port *info)
456740165f7SJiri Slaby {
457740165f7SJiri Slaby 	info->IER &= ~UART_IER_THRI;
458740165f7SJiri Slaby 	outb(info->IER, info->ioaddr + UART_IER);
459740165f7SJiri Slaby }
460740165f7SJiri Slaby 
mxser_carrier_raised(struct tty_port * port)461b300fb26SIlpo Järvinen static bool mxser_carrier_raised(struct tty_port *port)
462a6afd9f3SGreg Kroah-Hartman {
463a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *mp = container_of(port, struct mxser_port, port);
464b300fb26SIlpo Järvinen 
465b300fb26SIlpo Järvinen 	return inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD;
466a6afd9f3SGreg Kroah-Hartman }
467a6afd9f3SGreg Kroah-Hartman 
mxser_dtr_rts(struct tty_port * port,bool active)4685701cb8bSIlpo Järvinen static void mxser_dtr_rts(struct tty_port *port, bool active)
469a6afd9f3SGreg Kroah-Hartman {
470a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *mp = container_of(port, struct mxser_port, port);
471a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
472007bbdc8SJiri Slaby 	u8 mcr;
473a6afd9f3SGreg Kroah-Hartman 
474a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&mp->slock, flags);
475007bbdc8SJiri Slaby 	mcr = inb(mp->ioaddr + UART_MCR);
4765701cb8bSIlpo Järvinen 	if (active)
477007bbdc8SJiri Slaby 		mcr |= UART_MCR_DTR | UART_MCR_RTS;
478a6afd9f3SGreg Kroah-Hartman 	else
479007bbdc8SJiri Slaby 		mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
480007bbdc8SJiri Slaby 	outb(mcr, mp->ioaddr + UART_MCR);
481a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&mp->slock, flags);
482a6afd9f3SGreg Kroah-Hartman }
483a6afd9f3SGreg Kroah-Hartman 
mxser_set_baud(struct tty_struct * tty,speed_t newspd)484dc33f644SJiri Slaby static int mxser_set_baud(struct tty_struct *tty, speed_t newspd)
485a6afd9f3SGreg Kroah-Hartman {
486a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
487104583b5SJiri Slaby 	unsigned int quot = 0, baud;
488a6afd9f3SGreg Kroah-Hartman 	unsigned char cval;
489104583b5SJiri Slaby 	u64 timeout;
490a6afd9f3SGreg Kroah-Hartman 
491928f9464SJiri Slaby 	if (newspd > info->board->max_baud)
492a6afd9f3SGreg Kroah-Hartman 		return -1;
493a6afd9f3SGreg Kroah-Hartman 
494a6afd9f3SGreg Kroah-Hartman 	if (newspd == 134) {
495a6970c39SJiri Slaby 		quot = 2 * MXSER_BAUD_BASE / 269;
496a6afd9f3SGreg Kroah-Hartman 		tty_encode_baud_rate(tty, 134, 134);
497a6afd9f3SGreg Kroah-Hartman 	} else if (newspd) {
498a6970c39SJiri Slaby 		quot = MXSER_BAUD_BASE / newspd;
499a6afd9f3SGreg Kroah-Hartman 		if (quot == 0)
500a6afd9f3SGreg Kroah-Hartman 			quot = 1;
501a6970c39SJiri Slaby 		baud = MXSER_BAUD_BASE / quot;
502a6afd9f3SGreg Kroah-Hartman 		tty_encode_baud_rate(tty, baud, baud);
503a6afd9f3SGreg Kroah-Hartman 	} else {
504a6afd9f3SGreg Kroah-Hartman 		quot = 0;
505a6afd9f3SGreg Kroah-Hartman 	}
506a6afd9f3SGreg Kroah-Hartman 
507104583b5SJiri Slaby 	/*
508104583b5SJiri Slaby 	 * worst case (128 * 1000 * 10 * 18432) needs 35 bits, so divide in the
509104583b5SJiri Slaby 	 * u64 domain
510104583b5SJiri Slaby 	 */
511104583b5SJiri Slaby 	timeout = (u64)info->xmit_fifo_size * HZ * 10 * quot;
512a6970c39SJiri Slaby 	do_div(timeout, MXSER_BAUD_BASE);
513104583b5SJiri Slaby 	info->timeout = timeout + HZ / 50; /* Add .02 seconds of slop */
514a6afd9f3SGreg Kroah-Hartman 
515a6afd9f3SGreg Kroah-Hartman 	if (quot) {
516a6afd9f3SGreg Kroah-Hartman 		info->MCR |= UART_MCR_DTR;
517a6afd9f3SGreg Kroah-Hartman 		outb(info->MCR, info->ioaddr + UART_MCR);
518a6afd9f3SGreg Kroah-Hartman 	} else {
519a6afd9f3SGreg Kroah-Hartman 		info->MCR &= ~UART_MCR_DTR;
520a6afd9f3SGreg Kroah-Hartman 		outb(info->MCR, info->ioaddr + UART_MCR);
521a6afd9f3SGreg Kroah-Hartman 		return 0;
522a6afd9f3SGreg Kroah-Hartman 	}
523a6afd9f3SGreg Kroah-Hartman 
524a6afd9f3SGreg Kroah-Hartman 	cval = inb(info->ioaddr + UART_LCR);
525a6afd9f3SGreg Kroah-Hartman 
526a6afd9f3SGreg Kroah-Hartman 	outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR);	/* set DLAB */
527a6afd9f3SGreg Kroah-Hartman 
528a6afd9f3SGreg Kroah-Hartman 	outb(quot & 0xff, info->ioaddr + UART_DLL);	/* LS of divisor */
529a6afd9f3SGreg Kroah-Hartman 	outb(quot >> 8, info->ioaddr + UART_DLM);	/* MS of divisor */
530a6afd9f3SGreg Kroah-Hartman 	outb(cval, info->ioaddr + UART_LCR);	/* reset DLAB */
531a6afd9f3SGreg Kroah-Hartman 
532a6afd9f3SGreg Kroah-Hartman 	if (C_BAUD(tty) == BOTHER) {
533a6970c39SJiri Slaby 		quot = MXSER_BAUD_BASE % newspd;
534a6afd9f3SGreg Kroah-Hartman 		quot *= 8;
535a6afd9f3SGreg Kroah-Hartman 		if (quot % newspd > newspd / 2) {
536a6afd9f3SGreg Kroah-Hartman 			quot /= newspd;
537a6afd9f3SGreg Kroah-Hartman 			quot++;
538a6afd9f3SGreg Kroah-Hartman 		} else
539a6afd9f3SGreg Kroah-Hartman 			quot /= newspd;
540a6afd9f3SGreg Kroah-Hartman 
541a6afd9f3SGreg Kroah-Hartman 		mxser_set_must_enum_value(info->ioaddr, quot);
54269648d7bSIlpo Järvinen 	} else {
543a6afd9f3SGreg Kroah-Hartman 		mxser_set_must_enum_value(info->ioaddr, 0);
54469648d7bSIlpo Järvinen 	}
545a6afd9f3SGreg Kroah-Hartman 
546a6afd9f3SGreg Kroah-Hartman 	return 0;
547a6afd9f3SGreg Kroah-Hartman }
548a6afd9f3SGreg Kroah-Hartman 
mxser_handle_cts(struct tty_struct * tty,struct mxser_port * info,u8 msr)549be486667SJiri Slaby static void mxser_handle_cts(struct tty_struct *tty, struct mxser_port *info,
550be486667SJiri Slaby 		u8 msr)
551be486667SJiri Slaby {
552be486667SJiri Slaby 	bool cts = msr & UART_MSR_CTS;
553be486667SJiri Slaby 
554be486667SJiri Slaby 	if (tty->hw_stopped) {
555be486667SJiri Slaby 		if (cts) {
556035173c9SIlpo Järvinen 			tty->hw_stopped = false;
557be486667SJiri Slaby 
5585d1ea1adSJiri Slaby 			if (!mxser_16550A_or_MUST(info))
559740165f7SJiri Slaby 				__mxser_start_tx(info);
560be486667SJiri Slaby 			tty_wakeup(tty);
561be486667SJiri Slaby 		}
562be486667SJiri Slaby 		return;
563be486667SJiri Slaby 	} else if (cts)
564be486667SJiri Slaby 		return;
565be486667SJiri Slaby 
566035173c9SIlpo Järvinen 	tty->hw_stopped = true;
5675d1ea1adSJiri Slaby 	if (!mxser_16550A_or_MUST(info))
568740165f7SJiri Slaby 		__mxser_stop_tx(info);
569be486667SJiri Slaby }
570be486667SJiri Slaby 
571a6afd9f3SGreg Kroah-Hartman /*
572a6afd9f3SGreg Kroah-Hartman  * This routine is called to set the UART divisor registers to match
573a6afd9f3SGreg Kroah-Hartman  * the specified baud rate for a serial port.
574a6afd9f3SGreg Kroah-Hartman  */
mxser_change_speed(struct tty_struct * tty,const struct ktermios * old_termios)575a8c11c15SIlpo Järvinen static void mxser_change_speed(struct tty_struct *tty,
576a8c11c15SIlpo Järvinen 			       const struct ktermios *old_termios)
577a6afd9f3SGreg Kroah-Hartman {
578a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
579d249e662SJiri Slaby 	unsigned cflag, cval;
580a6afd9f3SGreg Kroah-Hartman 
581adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
582a6afd9f3SGreg Kroah-Hartman 
5833fdfa165SJiri Slaby 	if (mxser_set_baud(tty, tty_get_baud_rate(tty))) {
5843fdfa165SJiri Slaby 		/* Use previous rate on a failure */
5853fdfa165SJiri Slaby 		if (old_termios) {
5863fdfa165SJiri Slaby 			speed_t baud = tty_termios_baud_rate(old_termios);
5873fdfa165SJiri Slaby 			tty_encode_baud_rate(tty, baud, baud);
5883fdfa165SJiri Slaby 		}
5893fdfa165SJiri Slaby 	}
590a6afd9f3SGreg Kroah-Hartman 
591a6afd9f3SGreg Kroah-Hartman 	/* byte size and parity */
592e7d6f84cSJiri Slaby 	cval = UART_LCR_WLEN(tty_get_char_size(tty->termios.c_cflag));
5932c21832bSJiri Slaby 
594a6afd9f3SGreg Kroah-Hartman 	if (cflag & CSTOPB)
5952c21832bSJiri Slaby 		cval |= UART_LCR_STOP;
596a6afd9f3SGreg Kroah-Hartman 	if (cflag & PARENB)
597a6afd9f3SGreg Kroah-Hartman 		cval |= UART_LCR_PARITY;
598a6afd9f3SGreg Kroah-Hartman 	if (!(cflag & PARODD))
599a6afd9f3SGreg Kroah-Hartman 		cval |= UART_LCR_EPAR;
600a6afd9f3SGreg Kroah-Hartman 	if (cflag & CMSPAR)
601a6afd9f3SGreg Kroah-Hartman 		cval |= UART_LCR_SPAR;
602a6afd9f3SGreg Kroah-Hartman 
603d249e662SJiri Slaby 	info->FCR = 0;
604292955a7SJiri Slaby 	if (info->board->must_hwid) {
605d249e662SJiri Slaby 		info->FCR |= UART_FCR_ENABLE_FIFO |
606bf1434c1SJiri Slaby 			MOXA_MUST_FCR_GDA_MODE_ENABLE;
607a6afd9f3SGreg Kroah-Hartman 		mxser_set_must_fifo_value(info);
608bf1434c1SJiri Slaby 	} else if (info->type != PORT_8250 && info->type != PORT_16450) {
609d249e662SJiri Slaby 		info->FCR |= UART_FCR_ENABLE_FIFO;
610dc33f644SJiri Slaby 		switch (info->rx_high_water) {
611a6afd9f3SGreg Kroah-Hartman 		case 1:
612d249e662SJiri Slaby 			info->FCR |= UART_FCR_TRIGGER_1;
613a6afd9f3SGreg Kroah-Hartman 			break;
614a6afd9f3SGreg Kroah-Hartman 		case 4:
615d249e662SJiri Slaby 			info->FCR |= UART_FCR_TRIGGER_4;
616a6afd9f3SGreg Kroah-Hartman 			break;
617a6afd9f3SGreg Kroah-Hartman 		case 8:
618d249e662SJiri Slaby 			info->FCR |= UART_FCR_TRIGGER_8;
619a6afd9f3SGreg Kroah-Hartman 			break;
620a6afd9f3SGreg Kroah-Hartman 		default:
621d249e662SJiri Slaby 			info->FCR |= UART_FCR_TRIGGER_14;
622a6afd9f3SGreg Kroah-Hartman 			break;
623a6afd9f3SGreg Kroah-Hartman 		}
624a6afd9f3SGreg Kroah-Hartman 	}
625a6afd9f3SGreg Kroah-Hartman 
626a6afd9f3SGreg Kroah-Hartman 	/* CTS flow control flag and modem status interrupts */
627a6afd9f3SGreg Kroah-Hartman 	info->IER &= ~UART_IER_MSI;
628a6afd9f3SGreg Kroah-Hartman 	info->MCR &= ~UART_MCR_AFE;
6295604a98eSPeter Hurley 	tty_port_set_cts_flow(&info->port, cflag & CRTSCTS);
630a6afd9f3SGreg Kroah-Hartman 	if (cflag & CRTSCTS) {
631a6afd9f3SGreg Kroah-Hartman 		info->IER |= UART_IER_MSI;
6325d1ea1adSJiri Slaby 		if (mxser_16550A_or_MUST(info)) {
633a6afd9f3SGreg Kroah-Hartman 			info->MCR |= UART_MCR_AFE;
634a6afd9f3SGreg Kroah-Hartman 		} else {
635be486667SJiri Slaby 			mxser_handle_cts(tty, info,
636be486667SJiri Slaby 					inb(info->ioaddr + UART_MSR));
637a6afd9f3SGreg Kroah-Hartman 		}
638a6afd9f3SGreg Kroah-Hartman 	}
639a6afd9f3SGreg Kroah-Hartman 	outb(info->MCR, info->ioaddr + UART_MCR);
6402d68655dSPeter Hurley 	tty_port_set_check_carrier(&info->port, ~cflag & CLOCAL);
6412d68655dSPeter Hurley 	if (~cflag & CLOCAL)
642a6afd9f3SGreg Kroah-Hartman 		info->IER |= UART_IER_MSI;
643a6afd9f3SGreg Kroah-Hartman 	outb(info->IER, info->ioaddr + UART_IER);
644a6afd9f3SGreg Kroah-Hartman 
645a6afd9f3SGreg Kroah-Hartman 	/*
646a6afd9f3SGreg Kroah-Hartman 	 * Set up parity check flag
647a6afd9f3SGreg Kroah-Hartman 	 */
648a6afd9f3SGreg Kroah-Hartman 	info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
649a6afd9f3SGreg Kroah-Hartman 	if (I_INPCK(tty))
650a6afd9f3SGreg Kroah-Hartman 		info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
651a6afd9f3SGreg Kroah-Hartman 	if (I_BRKINT(tty) || I_PARMRK(tty))
652a6afd9f3SGreg Kroah-Hartman 		info->read_status_mask |= UART_LSR_BI;
653a6afd9f3SGreg Kroah-Hartman 
654a6afd9f3SGreg Kroah-Hartman 	info->ignore_status_mask = 0;
655a6afd9f3SGreg Kroah-Hartman 
656a6afd9f3SGreg Kroah-Hartman 	if (I_IGNBRK(tty)) {
657a6afd9f3SGreg Kroah-Hartman 		info->ignore_status_mask |= UART_LSR_BI;
658a6afd9f3SGreg Kroah-Hartman 		info->read_status_mask |= UART_LSR_BI;
659a6afd9f3SGreg Kroah-Hartman 		/*
660a6afd9f3SGreg Kroah-Hartman 		 * If we're ignore parity and break indicators, ignore
661a6afd9f3SGreg Kroah-Hartman 		 * overruns too.  (For real raw support).
662a6afd9f3SGreg Kroah-Hartman 		 */
663a6afd9f3SGreg Kroah-Hartman 		if (I_IGNPAR(tty)) {
664a6afd9f3SGreg Kroah-Hartman 			info->ignore_status_mask |=
665a6afd9f3SGreg Kroah-Hartman 						UART_LSR_OE |
666a6afd9f3SGreg Kroah-Hartman 						UART_LSR_PE |
667a6afd9f3SGreg Kroah-Hartman 						UART_LSR_FE;
668a6afd9f3SGreg Kroah-Hartman 			info->read_status_mask |=
669a6afd9f3SGreg Kroah-Hartman 						UART_LSR_OE |
670a6afd9f3SGreg Kroah-Hartman 						UART_LSR_PE |
671a6afd9f3SGreg Kroah-Hartman 						UART_LSR_FE;
672a6afd9f3SGreg Kroah-Hartman 		}
673a6afd9f3SGreg Kroah-Hartman 	}
674292955a7SJiri Slaby 	if (info->board->must_hwid) {
675a6afd9f3SGreg Kroah-Hartman 		mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
676a6afd9f3SGreg Kroah-Hartman 		mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
677b441eb0fSJiri Slaby 		mxser_must_set_rx_sw_flow_control(info->ioaddr, I_IXON(tty));
678b441eb0fSJiri Slaby 		mxser_must_set_tx_sw_flow_control(info->ioaddr, I_IXOFF(tty));
679a6afd9f3SGreg Kroah-Hartman 	}
680a6afd9f3SGreg Kroah-Hartman 
681a6afd9f3SGreg Kroah-Hartman 
682d249e662SJiri Slaby 	outb(info->FCR, info->ioaddr + UART_FCR);
683a6afd9f3SGreg Kroah-Hartman 	outb(cval, info->ioaddr + UART_LCR);
684a6afd9f3SGreg Kroah-Hartman }
685a6afd9f3SGreg Kroah-Hartman 
mxser_check_modem_status(struct tty_struct * tty,struct mxser_port * port)68630f6027fSJiri Slaby static u8 mxser_check_modem_status(struct tty_struct *tty,
68730f6027fSJiri Slaby 				struct mxser_port *port)
688a6afd9f3SGreg Kroah-Hartman {
68930f6027fSJiri Slaby 	u8 msr = inb(port->ioaddr + UART_MSR);
69030f6027fSJiri Slaby 
69130f6027fSJiri Slaby 	if (!(msr & UART_MSR_ANY_DELTA))
69230f6027fSJiri Slaby 		return msr;
69330f6027fSJiri Slaby 
694a6afd9f3SGreg Kroah-Hartman 	/* update input line counters */
69530f6027fSJiri Slaby 	if (msr & UART_MSR_TERI)
696a6afd9f3SGreg Kroah-Hartman 		port->icount.rng++;
69730f6027fSJiri Slaby 	if (msr & UART_MSR_DDSR)
698a6afd9f3SGreg Kroah-Hartman 		port->icount.dsr++;
69930f6027fSJiri Slaby 	if (msr & UART_MSR_DDCD)
700a6afd9f3SGreg Kroah-Hartman 		port->icount.dcd++;
70130f6027fSJiri Slaby 	if (msr & UART_MSR_DCTS)
702a6afd9f3SGreg Kroah-Hartman 		port->icount.cts++;
703a6afd9f3SGreg Kroah-Hartman 	wake_up_interruptible(&port->port.delta_msr_wait);
704a6afd9f3SGreg Kroah-Hartman 
70530f6027fSJiri Slaby 	if (tty_port_check_carrier(&port->port) && (msr & UART_MSR_DDCD)) {
70630f6027fSJiri Slaby 		if (msr & UART_MSR_DCD)
707a6afd9f3SGreg Kroah-Hartman 			wake_up_interruptible(&port->port.open_wait);
708a6afd9f3SGreg Kroah-Hartman 	}
709a6afd9f3SGreg Kroah-Hartman 
710be486667SJiri Slaby 	if (tty_port_cts_enabled(&port->port))
71130f6027fSJiri Slaby 		mxser_handle_cts(tty, port, msr);
71230f6027fSJiri Slaby 
71330f6027fSJiri Slaby 	return msr;
714a6afd9f3SGreg Kroah-Hartman }
715a6afd9f3SGreg Kroah-Hartman 
mxser_disable_and_clear_FIFO(struct mxser_port * info)716ee7e5e66SJiri Slaby static void mxser_disable_and_clear_FIFO(struct mxser_port *info)
717ee7e5e66SJiri Slaby {
718ee7e5e66SJiri Slaby 	u8 fcr = UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
719ee7e5e66SJiri Slaby 
720ee7e5e66SJiri Slaby 	if (info->board->must_hwid)
721ee7e5e66SJiri Slaby 		fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
722ee7e5e66SJiri Slaby 
723ee7e5e66SJiri Slaby 	outb(fcr, info->ioaddr + UART_FCR);
724ee7e5e66SJiri Slaby }
725ee7e5e66SJiri Slaby 
mxser_activate(struct tty_port * port,struct tty_struct * tty)726a6afd9f3SGreg Kroah-Hartman static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
727a6afd9f3SGreg Kroah-Hartman {
728a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = container_of(port, struct mxser_port, port);
729a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
730cd3a4907SJiri Slaby 	int ret;
731a6afd9f3SGreg Kroah-Hartman 
73292cc9d1dSJiri Slaby 	ret = tty_port_alloc_xmit_buf(port);
73392cc9d1dSJiri Slaby 	if (ret < 0)
73492cc9d1dSJiri Slaby 		return ret;
735a6afd9f3SGreg Kroah-Hartman 
736a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&info->slock, flags);
737a6afd9f3SGreg Kroah-Hartman 
738987a4cfeSJiri Slaby 	if (!info->type) {
739a6afd9f3SGreg Kroah-Hartman 		set_bit(TTY_IO_ERROR, &tty->flags);
740a6afd9f3SGreg Kroah-Hartman 		spin_unlock_irqrestore(&info->slock, flags);
741cd3a4907SJiri Slaby 		ret = 0;
742cd3a4907SJiri Slaby 		goto err_free_xmit;
743a6afd9f3SGreg Kroah-Hartman 	}
744a6afd9f3SGreg Kroah-Hartman 
745a6afd9f3SGreg Kroah-Hartman 	/*
746a6afd9f3SGreg Kroah-Hartman 	 * Clear the FIFO buffers and disable them
747a6afd9f3SGreg Kroah-Hartman 	 * (they will be reenabled in mxser_change_speed())
748a6afd9f3SGreg Kroah-Hartman 	 */
749ee7e5e66SJiri Slaby 	mxser_disable_and_clear_FIFO(info);
750a6afd9f3SGreg Kroah-Hartman 
751a6afd9f3SGreg Kroah-Hartman 	/*
752a6afd9f3SGreg Kroah-Hartman 	 * At this point there's no way the LSR could still be 0xFF;
753a6afd9f3SGreg Kroah-Hartman 	 * if it is, then bail out, because there's likely no UART
754a6afd9f3SGreg Kroah-Hartman 	 * here.
755a6afd9f3SGreg Kroah-Hartman 	 */
756a6afd9f3SGreg Kroah-Hartman 	if (inb(info->ioaddr + UART_LSR) == 0xff) {
757a6afd9f3SGreg Kroah-Hartman 		spin_unlock_irqrestore(&info->slock, flags);
758a6afd9f3SGreg Kroah-Hartman 		if (capable(CAP_SYS_ADMIN)) {
759a6afd9f3SGreg Kroah-Hartman 			set_bit(TTY_IO_ERROR, &tty->flags);
760a6afd9f3SGreg Kroah-Hartman 			return 0;
761cd3a4907SJiri Slaby 		}
762cd3a4907SJiri Slaby 
763cd3a4907SJiri Slaby 		ret = -ENODEV;
764cd3a4907SJiri Slaby 		goto err_free_xmit;
765a6afd9f3SGreg Kroah-Hartman 	}
766a6afd9f3SGreg Kroah-Hartman 
767a6afd9f3SGreg Kroah-Hartman 	/*
768a6afd9f3SGreg Kroah-Hartman 	 * Clear the interrupt registers.
769a6afd9f3SGreg Kroah-Hartman 	 */
770a6afd9f3SGreg Kroah-Hartman 	(void) inb(info->ioaddr + UART_LSR);
771a6afd9f3SGreg Kroah-Hartman 	(void) inb(info->ioaddr + UART_RX);
772a6afd9f3SGreg Kroah-Hartman 	(void) inb(info->ioaddr + UART_IIR);
773a6afd9f3SGreg Kroah-Hartman 	(void) inb(info->ioaddr + UART_MSR);
774a6afd9f3SGreg Kroah-Hartman 
775a6afd9f3SGreg Kroah-Hartman 	/*
776a6afd9f3SGreg Kroah-Hartman 	 * Now, initialize the UART
777a6afd9f3SGreg Kroah-Hartman 	 */
778a6afd9f3SGreg Kroah-Hartman 	outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR);	/* reset DLAB */
779a6afd9f3SGreg Kroah-Hartman 	info->MCR = UART_MCR_DTR | UART_MCR_RTS;
780a6afd9f3SGreg Kroah-Hartman 	outb(info->MCR, info->ioaddr + UART_MCR);
781a6afd9f3SGreg Kroah-Hartman 
782a6afd9f3SGreg Kroah-Hartman 	/*
783a6afd9f3SGreg Kroah-Hartman 	 * Finally, enable interrupts
784a6afd9f3SGreg Kroah-Hartman 	 */
785a6afd9f3SGreg Kroah-Hartman 	info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
786a6afd9f3SGreg Kroah-Hartman 
787292955a7SJiri Slaby 	if (info->board->must_hwid)
788a6afd9f3SGreg Kroah-Hartman 		info->IER |= MOXA_MUST_IER_EGDAI;
789a6afd9f3SGreg Kroah-Hartman 	outb(info->IER, info->ioaddr + UART_IER);	/* enable interrupts */
790a6afd9f3SGreg Kroah-Hartman 
791a6afd9f3SGreg Kroah-Hartman 	/*
792a6afd9f3SGreg Kroah-Hartman 	 * And clear the interrupt registers again for luck.
793a6afd9f3SGreg Kroah-Hartman 	 */
794a6afd9f3SGreg Kroah-Hartman 	(void) inb(info->ioaddr + UART_LSR);
795a6afd9f3SGreg Kroah-Hartman 	(void) inb(info->ioaddr + UART_RX);
796a6afd9f3SGreg Kroah-Hartman 	(void) inb(info->ioaddr + UART_IIR);
797a6afd9f3SGreg Kroah-Hartman 	(void) inb(info->ioaddr + UART_MSR);
798a6afd9f3SGreg Kroah-Hartman 
799a6afd9f3SGreg Kroah-Hartman 	clear_bit(TTY_IO_ERROR, &tty->flags);
80032330c83SJiri Slaby 	kfifo_reset(&port->xmit_fifo);
801a6afd9f3SGreg Kroah-Hartman 
802a6afd9f3SGreg Kroah-Hartman 	/*
803a6afd9f3SGreg Kroah-Hartman 	 * and set the speed of the serial port
804a6afd9f3SGreg Kroah-Hartman 	 */
8053fdfa165SJiri Slaby 	mxser_change_speed(tty, NULL);
806a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&info->slock, flags);
807a6afd9f3SGreg Kroah-Hartman 
808a6afd9f3SGreg Kroah-Hartman 	return 0;
809cd3a4907SJiri Slaby err_free_xmit:
81092cc9d1dSJiri Slaby 	tty_port_free_xmit_buf(port);
811cd3a4907SJiri Slaby 	return ret;
812a6afd9f3SGreg Kroah-Hartman }
813a6afd9f3SGreg Kroah-Hartman 
814a6afd9f3SGreg Kroah-Hartman /*
81547b722d4SJiri Slaby  * To stop accepting input, we disable the receive line status interrupts, and
81647b722d4SJiri Slaby  * tell the interrupt driver to stop checking the data ready bit in the line
81747b722d4SJiri Slaby  * status register.
81847b722d4SJiri Slaby  */
mxser_stop_rx(struct mxser_port * info)81947b722d4SJiri Slaby static void mxser_stop_rx(struct mxser_port *info)
82047b722d4SJiri Slaby {
82147b722d4SJiri Slaby 	info->IER &= ~UART_IER_RLSI;
82247b722d4SJiri Slaby 	if (info->board->must_hwid)
82347b722d4SJiri Slaby 		info->IER &= ~MOXA_MUST_RECV_ISR;
82447b722d4SJiri Slaby 
82547b722d4SJiri Slaby 	outb(info->IER, info->ioaddr + UART_IER);
82647b722d4SJiri Slaby }
82747b722d4SJiri Slaby 
82847b722d4SJiri Slaby /*
829a6afd9f3SGreg Kroah-Hartman  * This routine will shutdown a serial port
830a6afd9f3SGreg Kroah-Hartman  */
mxser_shutdown_port(struct tty_port * port)831a6afd9f3SGreg Kroah-Hartman static void mxser_shutdown_port(struct tty_port *port)
832a6afd9f3SGreg Kroah-Hartman {
833a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = container_of(port, struct mxser_port, port);
834a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
835a6afd9f3SGreg Kroah-Hartman 
836a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&info->slock, flags);
837a6afd9f3SGreg Kroah-Hartman 
83847b722d4SJiri Slaby 	mxser_stop_rx(info);
83947b722d4SJiri Slaby 
840a6afd9f3SGreg Kroah-Hartman 	/*
841a6afd9f3SGreg Kroah-Hartman 	 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
842a6afd9f3SGreg Kroah-Hartman 	 * here so the queue might never be waken up
843a6afd9f3SGreg Kroah-Hartman 	 */
844a6afd9f3SGreg Kroah-Hartman 	wake_up_interruptible(&info->port.delta_msr_wait);
845a6afd9f3SGreg Kroah-Hartman 
846a6afd9f3SGreg Kroah-Hartman 	info->IER = 0;
847a6afd9f3SGreg Kroah-Hartman 	outb(0x00, info->ioaddr + UART_IER);
848a6afd9f3SGreg Kroah-Hartman 
849a6afd9f3SGreg Kroah-Hartman 	/* clear Rx/Tx FIFO's */
850ee7e5e66SJiri Slaby 	mxser_disable_and_clear_FIFO(info);
851a6afd9f3SGreg Kroah-Hartman 
852a6afd9f3SGreg Kroah-Hartman 	/* read data port to reset things */
853a6afd9f3SGreg Kroah-Hartman 	(void) inb(info->ioaddr + UART_RX);
854a6afd9f3SGreg Kroah-Hartman 
855a6afd9f3SGreg Kroah-Hartman 
856292955a7SJiri Slaby 	if (info->board->must_hwid)
857b441eb0fSJiri Slaby 		mxser_must_no_sw_flow_control(info->ioaddr);
858a6afd9f3SGreg Kroah-Hartman 
859a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&info->slock, flags);
86092cc9d1dSJiri Slaby 
86192cc9d1dSJiri Slaby 	/* make sure ISR is not running while we free the buffer */
86292cc9d1dSJiri Slaby 	synchronize_irq(info->board->irq);
86392cc9d1dSJiri Slaby 
86492cc9d1dSJiri Slaby 	tty_port_free_xmit_buf(port);
865a6afd9f3SGreg Kroah-Hartman }
866a6afd9f3SGreg Kroah-Hartman 
867a6afd9f3SGreg Kroah-Hartman /*
868a6afd9f3SGreg Kroah-Hartman  * This routine is called whenever a serial port is opened.  It
869a6afd9f3SGreg Kroah-Hartman  * enables interrupts for a serial port, linking in its async structure into
870a6afd9f3SGreg Kroah-Hartman  * the IRQ chain.   It also performs the serial-specific
871a6afd9f3SGreg Kroah-Hartman  * initialization for the tty structure.
872a6afd9f3SGreg Kroah-Hartman  */
mxser_open(struct tty_struct * tty,struct file * filp)873a6afd9f3SGreg Kroah-Hartman static int mxser_open(struct tty_struct *tty, struct file *filp)
874a6afd9f3SGreg Kroah-Hartman {
87542ad25fcSJiri Slaby 	struct tty_port *tport = tty->port;
87642ad25fcSJiri Slaby 	struct mxser_port *port = container_of(tport, struct mxser_port, port);
877a6afd9f3SGreg Kroah-Hartman 
87842ad25fcSJiri Slaby 	tty->driver_data = port;
879a6afd9f3SGreg Kroah-Hartman 
88042ad25fcSJiri Slaby 	return tty_port_open(tport, tty, filp);
881a6afd9f3SGreg Kroah-Hartman }
882a6afd9f3SGreg Kroah-Hartman 
mxser_flush_buffer(struct tty_struct * tty)883a6afd9f3SGreg Kroah-Hartman static void mxser_flush_buffer(struct tty_struct *tty)
884a6afd9f3SGreg Kroah-Hartman {
885a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
886a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
887a6afd9f3SGreg Kroah-Hartman 
888a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&info->slock, flags);
88932330c83SJiri Slaby 	kfifo_reset(&info->port.xmit_fifo);
890a6afd9f3SGreg Kroah-Hartman 
891d249e662SJiri Slaby 	outb(info->FCR | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
892a6afd9f3SGreg Kroah-Hartman 		info->ioaddr + UART_FCR);
893a6afd9f3SGreg Kroah-Hartman 
894a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&info->slock, flags);
895a6afd9f3SGreg Kroah-Hartman 
896a6afd9f3SGreg Kroah-Hartman 	tty_wakeup(tty);
897a6afd9f3SGreg Kroah-Hartman }
898a6afd9f3SGreg Kroah-Hartman 
mxser_close(struct tty_struct * tty,struct file * filp)899a6afd9f3SGreg Kroah-Hartman static void mxser_close(struct tty_struct *tty, struct file *filp)
900a6afd9f3SGreg Kroah-Hartman {
901c7ec012fSJiri Slaby 	tty_port_close(tty->port, tty, filp);
902a6afd9f3SGreg Kroah-Hartman }
903a6afd9f3SGreg Kroah-Hartman 
mxser_write(struct tty_struct * tty,const u8 * buf,size_t count)904*95713967SJiri Slaby (SUSE) static ssize_t mxser_write(struct tty_struct *tty, const u8 *buf, size_t count)
905a6afd9f3SGreg Kroah-Hartman {
906a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
907a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
90832330c83SJiri Slaby 	int written;
90932330c83SJiri Slaby 	bool is_empty;
910a6afd9f3SGreg Kroah-Hartman 
911a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&info->slock, flags);
91232330c83SJiri Slaby 	written = kfifo_in(&info->port.xmit_fifo, buf, count);
91332330c83SJiri Slaby 	is_empty = kfifo_is_empty(&info->port.xmit_fifo);
914a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&info->slock, flags);
915a6afd9f3SGreg Kroah-Hartman 
91632330c83SJiri Slaby 	if (!is_empty && !tty->flow.stopped)
9175d1ea1adSJiri Slaby 		if (!tty->hw_stopped || mxser_16550A_or_MUST(info))
918740165f7SJiri Slaby 			mxser_start_tx(info);
9195d1ea1adSJiri Slaby 
92032330c83SJiri Slaby 	return written;
921a6afd9f3SGreg Kroah-Hartman }
922a6afd9f3SGreg Kroah-Hartman 
mxser_put_char(struct tty_struct * tty,u8 ch)923dcaafbe6SJiri Slaby (SUSE) static int mxser_put_char(struct tty_struct *tty, u8 ch)
924a6afd9f3SGreg Kroah-Hartman {
925a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
926a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
92732330c83SJiri Slaby 	int ret;
928a6afd9f3SGreg Kroah-Hartman 
929a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&info->slock, flags);
93032330c83SJiri Slaby 	ret = kfifo_put(&info->port.xmit_fifo, ch);
931a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&info->slock, flags);
9328aff64e0SJiri Slaby 
93332330c83SJiri Slaby 	return ret;
934a6afd9f3SGreg Kroah-Hartman }
935a6afd9f3SGreg Kroah-Hartman 
936a6afd9f3SGreg Kroah-Hartman 
mxser_flush_chars(struct tty_struct * tty)937a6afd9f3SGreg Kroah-Hartman static void mxser_flush_chars(struct tty_struct *tty)
938a6afd9f3SGreg Kroah-Hartman {
939a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
940a6afd9f3SGreg Kroah-Hartman 
94132330c83SJiri Slaby 	if (kfifo_is_empty(&info->port.xmit_fifo) || tty->flow.stopped ||
9425d1ea1adSJiri Slaby 			(tty->hw_stopped && !mxser_16550A_or_MUST(info)))
943a6afd9f3SGreg Kroah-Hartman 		return;
944a6afd9f3SGreg Kroah-Hartman 
945740165f7SJiri Slaby 	mxser_start_tx(info);
946a6afd9f3SGreg Kroah-Hartman }
947a6afd9f3SGreg Kroah-Hartman 
mxser_write_room(struct tty_struct * tty)94803b3b1a2SJiri Slaby static unsigned int mxser_write_room(struct tty_struct *tty)
949a6afd9f3SGreg Kroah-Hartman {
950a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
951a6afd9f3SGreg Kroah-Hartman 
95232330c83SJiri Slaby 	return kfifo_avail(&info->port.xmit_fifo);
953a6afd9f3SGreg Kroah-Hartman }
954a6afd9f3SGreg Kroah-Hartman 
mxser_chars_in_buffer(struct tty_struct * tty)955fff4ef17SJiri Slaby static unsigned int mxser_chars_in_buffer(struct tty_struct *tty)
956a6afd9f3SGreg Kroah-Hartman {
957a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
95832330c83SJiri Slaby 
95932330c83SJiri Slaby 	return kfifo_len(&info->port.xmit_fifo);
960a6afd9f3SGreg Kroah-Hartman }
961a6afd9f3SGreg Kroah-Hartman 
962a6afd9f3SGreg Kroah-Hartman /*
963a6afd9f3SGreg Kroah-Hartman  * ------------------------------------------------------------
964a6afd9f3SGreg Kroah-Hartman  * friends of mxser_ioctl()
965a6afd9f3SGreg Kroah-Hartman  * ------------------------------------------------------------
966a6afd9f3SGreg Kroah-Hartman  */
mxser_get_serial_info(struct tty_struct * tty,struct serial_struct * ss)967a6afd9f3SGreg Kroah-Hartman static int mxser_get_serial_info(struct tty_struct *tty,
9686da5b587SAl Viro 		struct serial_struct *ss)
969a6afd9f3SGreg Kroah-Hartman {
970a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
9716da5b587SAl Viro 	struct tty_port *port = &info->port;
972be6cf583SJohan Hovold 	unsigned int closing_wait, close_delay;
9736da5b587SAl Viro 
9746da5b587SAl Viro 	mutex_lock(&port->mutex);
975be6cf583SJohan Hovold 
976be6cf583SJohan Hovold 	close_delay = jiffies_to_msecs(info->port.close_delay) / 10;
977be6cf583SJohan Hovold 	closing_wait = info->port.closing_wait;
978be6cf583SJohan Hovold 	if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
979be6cf583SJohan Hovold 		closing_wait = jiffies_to_msecs(closing_wait) / 10;
980be6cf583SJohan Hovold 
9812285c496SDan Carpenter 	ss->type = info->type;
9822285c496SDan Carpenter 	ss->line = tty->index;
9832285c496SDan Carpenter 	ss->port = info->ioaddr;
9842285c496SDan Carpenter 	ss->irq = info->board->irq;
9852285c496SDan Carpenter 	ss->flags = info->port.flags;
9862285c496SDan Carpenter 	ss->baud_base = MXSER_BAUD_BASE;
987be6cf583SJohan Hovold 	ss->close_delay = close_delay;
988be6cf583SJohan Hovold 	ss->closing_wait = closing_wait;
989d811b26bSJiri Slaby 	ss->custom_divisor = MXSER_CUSTOM_DIVISOR,
9906da5b587SAl Viro 	mutex_unlock(&port->mutex);
991a6afd9f3SGreg Kroah-Hartman 	return 0;
992a6afd9f3SGreg Kroah-Hartman }
993a6afd9f3SGreg Kroah-Hartman 
mxser_set_serial_info(struct tty_struct * tty,struct serial_struct * ss)994a6afd9f3SGreg Kroah-Hartman static int mxser_set_serial_info(struct tty_struct *tty,
9956da5b587SAl Viro 		struct serial_struct *ss)
996a6afd9f3SGreg Kroah-Hartman {
997a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
998a6afd9f3SGreg Kroah-Hartman 	struct tty_port *port = &info->port;
999a6afd9f3SGreg Kroah-Hartman 	speed_t baud;
1000a6afd9f3SGreg Kroah-Hartman 	unsigned long sl_flags;
100106cc52efSJiri Slaby 	unsigned int old_speed, close_delay, closing_wait;
1002a6afd9f3SGreg Kroah-Hartman 	int retval = 0;
1003a6afd9f3SGreg Kroah-Hartman 
10046da5b587SAl Viro 	if (tty_io_error(tty))
10056da5b587SAl Viro 		return -EIO;
1006a6afd9f3SGreg Kroah-Hartman 
10076da5b587SAl Viro 	mutex_lock(&port->mutex);
10086da5b587SAl Viro 
10096da5b587SAl Viro 	if (ss->irq != info->board->irq ||
10106da5b587SAl Viro 			ss->port != info->ioaddr) {
10116da5b587SAl Viro 		mutex_unlock(&port->mutex);
1012a6afd9f3SGreg Kroah-Hartman 		return -EINVAL;
10136da5b587SAl Viro 	}
1014a6afd9f3SGreg Kroah-Hartman 
101506cc52efSJiri Slaby 	old_speed = port->flags & ASYNC_SPD_MASK;
1016a6afd9f3SGreg Kroah-Hartman 
1017be6cf583SJohan Hovold 	close_delay = msecs_to_jiffies(ss->close_delay * 10);
1018be6cf583SJohan Hovold 	closing_wait = ss->closing_wait;
1019be6cf583SJohan Hovold 	if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1020be6cf583SJohan Hovold 		closing_wait = msecs_to_jiffies(closing_wait * 10);
1021be6cf583SJohan Hovold 
1022a6afd9f3SGreg Kroah-Hartman 	if (!capable(CAP_SYS_ADMIN)) {
1023a6970c39SJiri Slaby 		if ((ss->baud_base != MXSER_BAUD_BASE) ||
10241b3086b6SJiri Slaby 				(close_delay != port->close_delay) ||
10251b3086b6SJiri Slaby 				(closing_wait != port->closing_wait) ||
10261b3086b6SJiri Slaby 				((ss->flags & ~ASYNC_USR_MASK) != (port->flags & ~ASYNC_USR_MASK))) {
10276da5b587SAl Viro 			mutex_unlock(&port->mutex);
1028a6afd9f3SGreg Kroah-Hartman 			return -EPERM;
10296da5b587SAl Viro 		}
10301b3086b6SJiri Slaby 		port->flags = (port->flags & ~ASYNC_USR_MASK) |
10311b3086b6SJiri Slaby 				(ss->flags & ASYNC_USR_MASK);
1032a6afd9f3SGreg Kroah-Hartman 	} else {
1033a6afd9f3SGreg Kroah-Hartman 		/*
1034a6afd9f3SGreg Kroah-Hartman 		 * OK, past this point, all the error checking has been done.
1035a6afd9f3SGreg Kroah-Hartman 		 * At this point, we start making changes.....
1036a6afd9f3SGreg Kroah-Hartman 		 */
1037a6afd9f3SGreg Kroah-Hartman 		port->flags = ((port->flags & ~ASYNC_FLAGS) |
10386da5b587SAl Viro 				(ss->flags & ASYNC_FLAGS));
1039be6cf583SJohan Hovold 		port->close_delay = close_delay;
1040be6cf583SJohan Hovold 		port->closing_wait = closing_wait;
1041a6afd9f3SGreg Kroah-Hartman 		if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
1042a6970c39SJiri Slaby 				(ss->baud_base != MXSER_BAUD_BASE ||
10436da5b587SAl Viro 				ss->custom_divisor !=
1044d811b26bSJiri Slaby 				MXSER_CUSTOM_DIVISOR)) {
10456da5b587SAl Viro 			if (ss->custom_divisor == 0) {
10466da5b587SAl Viro 				mutex_unlock(&port->mutex);
1047a6afd9f3SGreg Kroah-Hartman 				return -EINVAL;
10486da5b587SAl Viro 			}
10496da5b587SAl Viro 			baud = ss->baud_base / ss->custom_divisor;
1050a6afd9f3SGreg Kroah-Hartman 			tty_encode_baud_rate(tty, baud, baud);
1051a6afd9f3SGreg Kroah-Hartman 		}
1052a6afd9f3SGreg Kroah-Hartman 
10536da5b587SAl Viro 		info->type = ss->type;
1054a6afd9f3SGreg Kroah-Hartman 
1055c3db20c3SJiri Slaby 		mxser_process_txrx_fifo(info);
1056b91cfb25SJohan Hovold 	}
1057a6afd9f3SGreg Kroah-Hartman 
1058d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
105906cc52efSJiri Slaby 		if (old_speed != (port->flags & ASYNC_SPD_MASK)) {
1060a6afd9f3SGreg Kroah-Hartman 			spin_lock_irqsave(&info->slock, sl_flags);
10613fdfa165SJiri Slaby 			mxser_change_speed(tty, NULL);
1062a6afd9f3SGreg Kroah-Hartman 			spin_unlock_irqrestore(&info->slock, sl_flags);
1063a6afd9f3SGreg Kroah-Hartman 		}
1064a6afd9f3SGreg Kroah-Hartman 	} else {
1065a6afd9f3SGreg Kroah-Hartman 		retval = mxser_activate(port, tty);
1066a6afd9f3SGreg Kroah-Hartman 		if (retval == 0)
1067515be7baSIlpo Järvinen 			tty_port_set_initialized(port, true);
1068a6afd9f3SGreg Kroah-Hartman 	}
10696da5b587SAl Viro 	mutex_unlock(&port->mutex);
1070a6afd9f3SGreg Kroah-Hartman 	return retval;
1071a6afd9f3SGreg Kroah-Hartman }
1072a6afd9f3SGreg Kroah-Hartman 
1073a6afd9f3SGreg Kroah-Hartman /*
1074a6afd9f3SGreg Kroah-Hartman  * mxser_get_lsr_info - get line status register info
1075a6afd9f3SGreg Kroah-Hartman  *
1076a6afd9f3SGreg Kroah-Hartman  * Purpose: Let user call ioctl() to get info when the UART physically
1077a6afd9f3SGreg Kroah-Hartman  *	    is emptied.  On bus types like RS485, the transmitter must
1078a6afd9f3SGreg Kroah-Hartman  *	    release the bus after transmitting. This must be done when
1079a6afd9f3SGreg Kroah-Hartman  *	    the transmit shift register is empty, not be done when the
1080a6afd9f3SGreg Kroah-Hartman  *	    transmit holding register is empty.  This functionality
1081a6afd9f3SGreg Kroah-Hartman  *	    allows an RS485 driver to be written in user space.
1082a6afd9f3SGreg Kroah-Hartman  */
mxser_get_lsr_info(struct mxser_port * info,unsigned int __user * value)1083a6afd9f3SGreg Kroah-Hartman static int mxser_get_lsr_info(struct mxser_port *info,
1084a6afd9f3SGreg Kroah-Hartman 		unsigned int __user *value)
1085a6afd9f3SGreg Kroah-Hartman {
1086a6afd9f3SGreg Kroah-Hartman 	unsigned char status;
1087a6afd9f3SGreg Kroah-Hartman 	unsigned int result;
1088a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1089a6afd9f3SGreg Kroah-Hartman 
1090a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&info->slock, flags);
1091a6afd9f3SGreg Kroah-Hartman 	status = inb(info->ioaddr + UART_LSR);
1092a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&info->slock, flags);
1093a6afd9f3SGreg Kroah-Hartman 	result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1094a6afd9f3SGreg Kroah-Hartman 	return put_user(result, value);
1095a6afd9f3SGreg Kroah-Hartman }
1096a6afd9f3SGreg Kroah-Hartman 
mxser_tiocmget(struct tty_struct * tty)1097a6afd9f3SGreg Kroah-Hartman static int mxser_tiocmget(struct tty_struct *tty)
1098a6afd9f3SGreg Kroah-Hartman {
1099a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
110030f6027fSJiri Slaby 	unsigned char control;
1101a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
110230f6027fSJiri Slaby 	u8 msr;
1103a6afd9f3SGreg Kroah-Hartman 
110418900ca6SPeter Hurley 	if (tty_io_error(tty))
1105a6afd9f3SGreg Kroah-Hartman 		return -EIO;
1106a6afd9f3SGreg Kroah-Hartman 
1107a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&info->slock, flags);
1108202acdaaSJiri Slaby 	control = info->MCR;
110930f6027fSJiri Slaby 	msr = mxser_check_modem_status(tty, info);
1110a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&info->slock, flags);
1111202acdaaSJiri Slaby 
1112a6afd9f3SGreg Kroah-Hartman 	return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1113a6afd9f3SGreg Kroah-Hartman 		    ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
111430f6027fSJiri Slaby 		    ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) |
111530f6027fSJiri Slaby 		    ((msr & UART_MSR_RI) ? TIOCM_RNG : 0) |
111630f6027fSJiri Slaby 		    ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0) |
111730f6027fSJiri Slaby 		    ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0);
1118a6afd9f3SGreg Kroah-Hartman }
1119a6afd9f3SGreg Kroah-Hartman 
mxser_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)1120a6afd9f3SGreg Kroah-Hartman static int mxser_tiocmset(struct tty_struct *tty,
1121a6afd9f3SGreg Kroah-Hartman 		unsigned int set, unsigned int clear)
1122a6afd9f3SGreg Kroah-Hartman {
1123a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
1124a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1125a6afd9f3SGreg Kroah-Hartman 
112618900ca6SPeter Hurley 	if (tty_io_error(tty))
1127a6afd9f3SGreg Kroah-Hartman 		return -EIO;
1128a6afd9f3SGreg Kroah-Hartman 
1129a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&info->slock, flags);
1130a6afd9f3SGreg Kroah-Hartman 
1131a6afd9f3SGreg Kroah-Hartman 	if (set & TIOCM_RTS)
1132a6afd9f3SGreg Kroah-Hartman 		info->MCR |= UART_MCR_RTS;
1133a6afd9f3SGreg Kroah-Hartman 	if (set & TIOCM_DTR)
1134a6afd9f3SGreg Kroah-Hartman 		info->MCR |= UART_MCR_DTR;
1135a6afd9f3SGreg Kroah-Hartman 
1136a6afd9f3SGreg Kroah-Hartman 	if (clear & TIOCM_RTS)
1137a6afd9f3SGreg Kroah-Hartman 		info->MCR &= ~UART_MCR_RTS;
1138a6afd9f3SGreg Kroah-Hartman 	if (clear & TIOCM_DTR)
1139a6afd9f3SGreg Kroah-Hartman 		info->MCR &= ~UART_MCR_DTR;
1140a6afd9f3SGreg Kroah-Hartman 
1141a6afd9f3SGreg Kroah-Hartman 	outb(info->MCR, info->ioaddr + UART_MCR);
1142a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&info->slock, flags);
1143a6afd9f3SGreg Kroah-Hartman 	return 0;
1144a6afd9f3SGreg Kroah-Hartman }
1145a6afd9f3SGreg Kroah-Hartman 
mxser_cflags_changed(struct mxser_port * info,unsigned long arg,struct async_icount * cprev)1146a6afd9f3SGreg Kroah-Hartman static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1147a6afd9f3SGreg Kroah-Hartman 		struct async_icount *cprev)
1148a6afd9f3SGreg Kroah-Hartman {
1149a6afd9f3SGreg Kroah-Hartman 	struct async_icount cnow;
1150a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1151a6afd9f3SGreg Kroah-Hartman 	int ret;
1152a6afd9f3SGreg Kroah-Hartman 
1153a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&info->slock, flags);
1154a6afd9f3SGreg Kroah-Hartman 	cnow = info->icount;	/* atomic copy */
1155a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&info->slock, flags);
1156a6afd9f3SGreg Kroah-Hartman 
1157a6afd9f3SGreg Kroah-Hartman 	ret =	((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1158a6afd9f3SGreg Kroah-Hartman 		((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1159a6afd9f3SGreg Kroah-Hartman 		((arg & TIOCM_CD)  && (cnow.dcd != cprev->dcd)) ||
1160a6afd9f3SGreg Kroah-Hartman 		((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1161a6afd9f3SGreg Kroah-Hartman 
1162a6afd9f3SGreg Kroah-Hartman 	*cprev = cnow;
1163a6afd9f3SGreg Kroah-Hartman 
1164a6afd9f3SGreg Kroah-Hartman 	return ret;
1165a6afd9f3SGreg Kroah-Hartman }
1166a6afd9f3SGreg Kroah-Hartman 
11679fae5f85SJiri Slaby /* We should likely switch to TIOCGRS485/TIOCSRS485. */
mxser_ioctl_op_mode(struct mxser_port * port,int index,bool set,int __user * u_opmode)11689fae5f85SJiri Slaby static int mxser_ioctl_op_mode(struct mxser_port *port, int index, bool set,
11699fae5f85SJiri Slaby 		int __user *u_opmode)
11709fae5f85SJiri Slaby {
11719fae5f85SJiri Slaby 	int opmode, p = index % 4;
11729fae5f85SJiri Slaby 	int shiftbit = p * 2;
1173238d117dSJiri Slaby 	u8 val;
11749fae5f85SJiri Slaby 
11759fae5f85SJiri Slaby 	if (port->board->must_hwid != MOXA_MUST_MU860_HWID)
11769fae5f85SJiri Slaby 		return -EFAULT;
11779fae5f85SJiri Slaby 
11789fae5f85SJiri Slaby 	if (set) {
11799fae5f85SJiri Slaby 		if (get_user(opmode, u_opmode))
11809fae5f85SJiri Slaby 			return -EFAULT;
11819fae5f85SJiri Slaby 
1182238d117dSJiri Slaby 		if (opmode & ~OP_MODE_MASK)
1183238d117dSJiri Slaby 			return -EINVAL;
11849fae5f85SJiri Slaby 
11859fae5f85SJiri Slaby 		spin_lock_irq(&port->slock);
11869fae5f85SJiri Slaby 		val = inb(port->opmode_ioaddr);
1187238d117dSJiri Slaby 		val &= ~(OP_MODE_MASK << shiftbit);
11889fae5f85SJiri Slaby 		val |= (opmode << shiftbit);
11899fae5f85SJiri Slaby 		outb(val, port->opmode_ioaddr);
11909fae5f85SJiri Slaby 		spin_unlock_irq(&port->slock);
1191238d117dSJiri Slaby 
1192238d117dSJiri Slaby 		return 0;
1193238d117dSJiri Slaby 	}
1194238d117dSJiri Slaby 
11959fae5f85SJiri Slaby 	spin_lock_irq(&port->slock);
11969fae5f85SJiri Slaby 	opmode = inb(port->opmode_ioaddr) >> shiftbit;
11979fae5f85SJiri Slaby 	spin_unlock_irq(&port->slock);
11989fae5f85SJiri Slaby 
1199238d117dSJiri Slaby 	return put_user(opmode & OP_MODE_MASK, u_opmode);
12009fae5f85SJiri Slaby }
12019fae5f85SJiri Slaby 
mxser_ioctl(struct tty_struct * tty,unsigned int cmd,unsigned long arg)1202a6afd9f3SGreg Kroah-Hartman static int mxser_ioctl(struct tty_struct *tty,
1203a6afd9f3SGreg Kroah-Hartman 		unsigned int cmd, unsigned long arg)
1204a6afd9f3SGreg Kroah-Hartman {
1205a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
1206a6afd9f3SGreg Kroah-Hartman 	struct async_icount cnow;
1207a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1208a6afd9f3SGreg Kroah-Hartman 	void __user *argp = (void __user *)arg;
1209a6afd9f3SGreg Kroah-Hartman 
12109fae5f85SJiri Slaby 	if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE)
12119fae5f85SJiri Slaby 		return mxser_ioctl_op_mode(info, tty->index,
12129fae5f85SJiri Slaby 				cmd == MOXA_SET_OP_MODE, argp);
1213a6afd9f3SGreg Kroah-Hartman 
12146da5b587SAl Viro 	if (cmd != TIOCMIWAIT && tty_io_error(tty))
1215a6afd9f3SGreg Kroah-Hartman 		return -EIO;
1216a6afd9f3SGreg Kroah-Hartman 
1217a6afd9f3SGreg Kroah-Hartman 	switch (cmd) {
1218a6afd9f3SGreg Kroah-Hartman 	case TIOCSERGETLSR:	/* Get line status register */
1219a6afd9f3SGreg Kroah-Hartman 		return  mxser_get_lsr_info(info, argp);
1220a6afd9f3SGreg Kroah-Hartman 		/*
1221a6afd9f3SGreg Kroah-Hartman 		 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1222a6afd9f3SGreg Kroah-Hartman 		 * - mask passed in arg for lines of interest
1223a6afd9f3SGreg Kroah-Hartman 		 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1224a6afd9f3SGreg Kroah-Hartman 		 * Caller should use TIOCGICOUNT to see which one it was
1225a6afd9f3SGreg Kroah-Hartman 		 */
1226a6afd9f3SGreg Kroah-Hartman 	case TIOCMIWAIT:
1227a6afd9f3SGreg Kroah-Hartman 		spin_lock_irqsave(&info->slock, flags);
1228a6afd9f3SGreg Kroah-Hartman 		cnow = info->icount;	/* note the counters on entry */
1229a6afd9f3SGreg Kroah-Hartman 		spin_unlock_irqrestore(&info->slock, flags);
1230a6afd9f3SGreg Kroah-Hartman 
1231a6afd9f3SGreg Kroah-Hartman 		return wait_event_interruptible(info->port.delta_msr_wait,
1232a6afd9f3SGreg Kroah-Hartman 				mxser_cflags_changed(info, arg, &cnow));
1233a6afd9f3SGreg Kroah-Hartman 	default:
1234a6afd9f3SGreg Kroah-Hartman 		return -ENOIOCTLCMD;
1235a6afd9f3SGreg Kroah-Hartman 	}
1236a6afd9f3SGreg Kroah-Hartman 	return 0;
1237a6afd9f3SGreg Kroah-Hartman }
1238a6afd9f3SGreg Kroah-Hartman 
1239a6afd9f3SGreg Kroah-Hartman 	/*
1240a6afd9f3SGreg Kroah-Hartman 	 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1241a6afd9f3SGreg Kroah-Hartman 	 * Return: write counters to the user passed counter struct
1242a6afd9f3SGreg Kroah-Hartman 	 * NB: both 1->0 and 0->1 transitions are counted except for
1243a6afd9f3SGreg Kroah-Hartman 	 *     RI where only 0->1 is counted.
1244a6afd9f3SGreg Kroah-Hartman 	 */
1245a6afd9f3SGreg Kroah-Hartman 
mxser_get_icount(struct tty_struct * tty,struct serial_icounter_struct * icount)1246a6afd9f3SGreg Kroah-Hartman static int mxser_get_icount(struct tty_struct *tty,
1247a6afd9f3SGreg Kroah-Hartman 		struct serial_icounter_struct *icount)
1248a6afd9f3SGreg Kroah-Hartman 
1249a6afd9f3SGreg Kroah-Hartman {
1250a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
1251a6afd9f3SGreg Kroah-Hartman 	struct async_icount cnow;
1252a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1253a6afd9f3SGreg Kroah-Hartman 
1254a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&info->slock, flags);
1255a6afd9f3SGreg Kroah-Hartman 	cnow = info->icount;
1256a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&info->slock, flags);
1257a6afd9f3SGreg Kroah-Hartman 
1258a6afd9f3SGreg Kroah-Hartman 	icount->frame = cnow.frame;
1259a6afd9f3SGreg Kroah-Hartman 	icount->brk = cnow.brk;
1260a6afd9f3SGreg Kroah-Hartman 	icount->overrun = cnow.overrun;
1261a6afd9f3SGreg Kroah-Hartman 	icount->buf_overrun = cnow.buf_overrun;
1262a6afd9f3SGreg Kroah-Hartman 	icount->parity = cnow.parity;
1263a6afd9f3SGreg Kroah-Hartman 	icount->rx = cnow.rx;
1264a6afd9f3SGreg Kroah-Hartman 	icount->tx = cnow.tx;
1265a6afd9f3SGreg Kroah-Hartman 	icount->cts = cnow.cts;
1266a6afd9f3SGreg Kroah-Hartman 	icount->dsr = cnow.dsr;
1267a6afd9f3SGreg Kroah-Hartman 	icount->rng = cnow.rng;
1268a6afd9f3SGreg Kroah-Hartman 	icount->dcd = cnow.dcd;
1269a6afd9f3SGreg Kroah-Hartman 	return 0;
1270a6afd9f3SGreg Kroah-Hartman }
1271a6afd9f3SGreg Kroah-Hartman 
1272c6693e6eSJiri Slaby /*
1273c6693e6eSJiri Slaby  * This routine is called by the upper-layer tty layer to signal that
1274c6693e6eSJiri Slaby  * incoming characters should be throttled.
1275c6693e6eSJiri Slaby  */
mxser_throttle(struct tty_struct * tty)1276c6693e6eSJiri Slaby static void mxser_throttle(struct tty_struct *tty)
1277a6afd9f3SGreg Kroah-Hartman {
1278a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
1279a6afd9f3SGreg Kroah-Hartman 
1280a6afd9f3SGreg Kroah-Hartman 	if (I_IXOFF(tty)) {
1281292955a7SJiri Slaby 		if (info->board->must_hwid) {
1282a6afd9f3SGreg Kroah-Hartman 			info->IER &= ~MOXA_MUST_RECV_ISR;
1283a6afd9f3SGreg Kroah-Hartman 			outb(info->IER, info->ioaddr + UART_IER);
1284a6afd9f3SGreg Kroah-Hartman 		} else {
1285a6afd9f3SGreg Kroah-Hartman 			info->x_char = STOP_CHAR(tty);
1286a6afd9f3SGreg Kroah-Hartman 			outb(0, info->ioaddr + UART_IER);
1287a6afd9f3SGreg Kroah-Hartman 			info->IER |= UART_IER_THRI;
1288a6afd9f3SGreg Kroah-Hartman 			outb(info->IER, info->ioaddr + UART_IER);
1289a6afd9f3SGreg Kroah-Hartman 		}
1290a6afd9f3SGreg Kroah-Hartman 	}
1291a6afd9f3SGreg Kroah-Hartman 
12929db276f8SPeter Hurley 	if (C_CRTSCTS(tty)) {
1293a6afd9f3SGreg Kroah-Hartman 		info->MCR &= ~UART_MCR_RTS;
1294a6afd9f3SGreg Kroah-Hartman 		outb(info->MCR, info->ioaddr + UART_MCR);
1295a6afd9f3SGreg Kroah-Hartman 	}
1296a6afd9f3SGreg Kroah-Hartman }
1297a6afd9f3SGreg Kroah-Hartman 
mxser_unthrottle(struct tty_struct * tty)1298a6afd9f3SGreg Kroah-Hartman static void mxser_unthrottle(struct tty_struct *tty)
1299a6afd9f3SGreg Kroah-Hartman {
1300a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
1301a6afd9f3SGreg Kroah-Hartman 
1302a6afd9f3SGreg Kroah-Hartman 	/* startrx */
1303a6afd9f3SGreg Kroah-Hartman 	if (I_IXOFF(tty)) {
1304a6afd9f3SGreg Kroah-Hartman 		if (info->x_char)
1305a6afd9f3SGreg Kroah-Hartman 			info->x_char = 0;
1306a6afd9f3SGreg Kroah-Hartman 		else {
1307292955a7SJiri Slaby 			if (info->board->must_hwid) {
1308a6afd9f3SGreg Kroah-Hartman 				info->IER |= MOXA_MUST_RECV_ISR;
1309a6afd9f3SGreg Kroah-Hartman 				outb(info->IER, info->ioaddr + UART_IER);
1310a6afd9f3SGreg Kroah-Hartman 			} else {
1311a6afd9f3SGreg Kroah-Hartman 				info->x_char = START_CHAR(tty);
1312a6afd9f3SGreg Kroah-Hartman 				outb(0, info->ioaddr + UART_IER);
1313a6afd9f3SGreg Kroah-Hartman 				info->IER |= UART_IER_THRI;
1314a6afd9f3SGreg Kroah-Hartman 				outb(info->IER, info->ioaddr + UART_IER);
1315a6afd9f3SGreg Kroah-Hartman 			}
1316a6afd9f3SGreg Kroah-Hartman 		}
1317a6afd9f3SGreg Kroah-Hartman 	}
1318a6afd9f3SGreg Kroah-Hartman 
13199db276f8SPeter Hurley 	if (C_CRTSCTS(tty)) {
1320a6afd9f3SGreg Kroah-Hartman 		info->MCR |= UART_MCR_RTS;
1321a6afd9f3SGreg Kroah-Hartman 		outb(info->MCR, info->ioaddr + UART_MCR);
1322a6afd9f3SGreg Kroah-Hartman 	}
1323a6afd9f3SGreg Kroah-Hartman }
1324a6afd9f3SGreg Kroah-Hartman 
1325a6afd9f3SGreg Kroah-Hartman /*
1326a6afd9f3SGreg Kroah-Hartman  * mxser_stop() and mxser_start()
1327a6afd9f3SGreg Kroah-Hartman  *
13286e94dbc7SJiri Slaby  * This routines are called before setting or resetting tty->flow.stopped.
1329a6afd9f3SGreg Kroah-Hartman  * They enable or disable transmitter interrupts, as necessary.
1330a6afd9f3SGreg Kroah-Hartman  */
mxser_stop(struct tty_struct * tty)1331a6afd9f3SGreg Kroah-Hartman static void mxser_stop(struct tty_struct *tty)
1332a6afd9f3SGreg Kroah-Hartman {
1333a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
1334a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1335a6afd9f3SGreg Kroah-Hartman 
1336a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&info->slock, flags);
1337740165f7SJiri Slaby 	if (info->IER & UART_IER_THRI)
1338740165f7SJiri Slaby 		__mxser_stop_tx(info);
1339a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&info->slock, flags);
1340a6afd9f3SGreg Kroah-Hartman }
1341a6afd9f3SGreg Kroah-Hartman 
mxser_start(struct tty_struct * tty)1342a6afd9f3SGreg Kroah-Hartman static void mxser_start(struct tty_struct *tty)
1343a6afd9f3SGreg Kroah-Hartman {
1344a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
1345a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1346a6afd9f3SGreg Kroah-Hartman 
1347a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&info->slock, flags);
134832330c83SJiri Slaby 	if (!kfifo_is_empty(&info->port.xmit_fifo))
1349740165f7SJiri Slaby 		__mxser_start_tx(info);
1350a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&info->slock, flags);
1351a6afd9f3SGreg Kroah-Hartman }
1352a6afd9f3SGreg Kroah-Hartman 
mxser_set_termios(struct tty_struct * tty,const struct ktermios * old_termios)1353a8c11c15SIlpo Järvinen static void mxser_set_termios(struct tty_struct *tty,
1354a8c11c15SIlpo Järvinen 			      const struct ktermios *old_termios)
1355a6afd9f3SGreg Kroah-Hartman {
1356a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
1357a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1358a6afd9f3SGreg Kroah-Hartman 
1359a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&info->slock, flags);
13603fdfa165SJiri Slaby 	mxser_change_speed(tty, old_termios);
1361a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&info->slock, flags);
1362a6afd9f3SGreg Kroah-Hartman 
13639db276f8SPeter Hurley 	if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
1364035173c9SIlpo Järvinen 		tty->hw_stopped = false;
1365a6afd9f3SGreg Kroah-Hartman 		mxser_start(tty);
1366a6afd9f3SGreg Kroah-Hartman 	}
1367a6afd9f3SGreg Kroah-Hartman 
1368a6afd9f3SGreg Kroah-Hartman 	/* Handle sw stopped */
13699db276f8SPeter Hurley 	if ((old_termios->c_iflag & IXON) && !I_IXON(tty)) {
13706e94dbc7SJiri Slaby 		tty->flow.stopped = 0;
1371a6afd9f3SGreg Kroah-Hartman 
1372292955a7SJiri Slaby 		if (info->board->must_hwid) {
1373a6afd9f3SGreg Kroah-Hartman 			spin_lock_irqsave(&info->slock, flags);
1374b441eb0fSJiri Slaby 			mxser_must_set_rx_sw_flow_control(info->ioaddr, false);
1375a6afd9f3SGreg Kroah-Hartman 			spin_unlock_irqrestore(&info->slock, flags);
1376a6afd9f3SGreg Kroah-Hartman 		}
1377a6afd9f3SGreg Kroah-Hartman 
1378a6afd9f3SGreg Kroah-Hartman 		mxser_start(tty);
1379a6afd9f3SGreg Kroah-Hartman 	}
1380a6afd9f3SGreg Kroah-Hartman }
1381a6afd9f3SGreg Kroah-Hartman 
mxser_tx_empty(struct mxser_port * info)1382239ef19eSJiri Slaby static bool mxser_tx_empty(struct mxser_port *info)
1383239ef19eSJiri Slaby {
1384239ef19eSJiri Slaby 	unsigned long flags;
1385239ef19eSJiri Slaby 	u8 lsr;
1386239ef19eSJiri Slaby 
1387239ef19eSJiri Slaby 	spin_lock_irqsave(&info->slock, flags);
1388239ef19eSJiri Slaby 	lsr = inb(info->ioaddr + UART_LSR);
1389239ef19eSJiri Slaby 	spin_unlock_irqrestore(&info->slock, flags);
1390239ef19eSJiri Slaby 
1391239ef19eSJiri Slaby 	return !(lsr & UART_LSR_TEMT);
1392239ef19eSJiri Slaby }
1393239ef19eSJiri Slaby 
1394a6afd9f3SGreg Kroah-Hartman /*
1395a6afd9f3SGreg Kroah-Hartman  * mxser_wait_until_sent() --- wait until the transmitter is empty
1396a6afd9f3SGreg Kroah-Hartman  */
mxser_wait_until_sent(struct tty_struct * tty,int timeout)1397a6afd9f3SGreg Kroah-Hartman static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1398a6afd9f3SGreg Kroah-Hartman {
1399a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
140049b798a6SJiri Slaby 	unsigned long expire, char_time;
1401a6afd9f3SGreg Kroah-Hartman 
1402a6afd9f3SGreg Kroah-Hartman 	if (info->type == PORT_UNKNOWN)
1403a6afd9f3SGreg Kroah-Hartman 		return;
1404a6afd9f3SGreg Kroah-Hartman 
1405a6afd9f3SGreg Kroah-Hartman 	if (info->xmit_fifo_size == 0)
1406a6afd9f3SGreg Kroah-Hartman 		return;		/* Just in case.... */
1407a6afd9f3SGreg Kroah-Hartman 
1408a6afd9f3SGreg Kroah-Hartman 	/*
1409a6afd9f3SGreg Kroah-Hartman 	 * Set the check interval to be 1/5 of the estimated time to
1410a6afd9f3SGreg Kroah-Hartman 	 * send a single character, and make it at least 1.  The check
1411a6afd9f3SGreg Kroah-Hartman 	 * interval should also be less than the timeout.
1412a6afd9f3SGreg Kroah-Hartman 	 *
1413a6afd9f3SGreg Kroah-Hartman 	 * Note: we have to use pretty tight timings here to satisfy
1414a6afd9f3SGreg Kroah-Hartman 	 * the NIST-PCTS.
1415a6afd9f3SGreg Kroah-Hartman 	 */
1416a6afd9f3SGreg Kroah-Hartman 	char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1417a6afd9f3SGreg Kroah-Hartman 	char_time = char_time / 5;
1418a6afd9f3SGreg Kroah-Hartman 	if (char_time == 0)
1419a6afd9f3SGreg Kroah-Hartman 		char_time = 1;
1420a6afd9f3SGreg Kroah-Hartman 	if (timeout && timeout < char_time)
1421a6afd9f3SGreg Kroah-Hartman 		char_time = timeout;
1422fe74bc61SJiri Slaby 
1423fe74bc61SJiri Slaby 	char_time = jiffies_to_msecs(char_time);
1424fe74bc61SJiri Slaby 
1425a6afd9f3SGreg Kroah-Hartman 	/*
1426a6afd9f3SGreg Kroah-Hartman 	 * If the transmitter hasn't cleared in twice the approximate
1427a6afd9f3SGreg Kroah-Hartman 	 * amount of time to send the entire FIFO, it probably won't
1428a6afd9f3SGreg Kroah-Hartman 	 * ever clear.  This assumes the UART isn't doing flow
1429a6afd9f3SGreg Kroah-Hartman 	 * control, which is currently the case.  Hence, if it ever
1430a6afd9f3SGreg Kroah-Hartman 	 * takes longer than info->timeout, this is probably due to a
1431a6afd9f3SGreg Kroah-Hartman 	 * UART bug of some kind.  So, we clamp the timeout parameter at
1432a6afd9f3SGreg Kroah-Hartman 	 * 2*info->timeout.
1433a6afd9f3SGreg Kroah-Hartman 	 */
1434a6afd9f3SGreg Kroah-Hartman 	if (!timeout || timeout > 2 * info->timeout)
1435a6afd9f3SGreg Kroah-Hartman 		timeout = 2 * info->timeout;
14368bab534bSJiri Slaby 
143749b798a6SJiri Slaby 	expire = jiffies + timeout;
143849b798a6SJiri Slaby 
1439239ef19eSJiri Slaby 	while (mxser_tx_empty(info)) {
1440fe74bc61SJiri Slaby 		msleep_interruptible(char_time);
1441a6afd9f3SGreg Kroah-Hartman 		if (signal_pending(current))
1442a6afd9f3SGreg Kroah-Hartman 			break;
144349b798a6SJiri Slaby 		if (time_after(jiffies, expire))
1444a6afd9f3SGreg Kroah-Hartman 			break;
1445a6afd9f3SGreg Kroah-Hartman 	}
1446a6afd9f3SGreg Kroah-Hartman }
1447a6afd9f3SGreg Kroah-Hartman 
1448a6afd9f3SGreg Kroah-Hartman /*
1449a6afd9f3SGreg Kroah-Hartman  * This routine is called by tty_hangup() when a hangup is signaled.
1450a6afd9f3SGreg Kroah-Hartman  */
mxser_hangup(struct tty_struct * tty)1451a6afd9f3SGreg Kroah-Hartman static void mxser_hangup(struct tty_struct *tty)
1452a6afd9f3SGreg Kroah-Hartman {
1453a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
1454a6afd9f3SGreg Kroah-Hartman 
1455a6afd9f3SGreg Kroah-Hartman 	mxser_flush_buffer(tty);
1456a6afd9f3SGreg Kroah-Hartman 	tty_port_hangup(&info->port);
1457a6afd9f3SGreg Kroah-Hartman }
1458a6afd9f3SGreg Kroah-Hartman 
1459a6afd9f3SGreg Kroah-Hartman /*
1460a6afd9f3SGreg Kroah-Hartman  * mxser_rs_break() --- routine which turns the break handling on or off
1461a6afd9f3SGreg Kroah-Hartman  */
mxser_rs_break(struct tty_struct * tty,int break_state)1462a6afd9f3SGreg Kroah-Hartman static int mxser_rs_break(struct tty_struct *tty, int break_state)
1463a6afd9f3SGreg Kroah-Hartman {
1464a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info = tty->driver_data;
1465a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
146659908433SJiri Slaby 	u8 lcr;
1467a6afd9f3SGreg Kroah-Hartman 
1468a6afd9f3SGreg Kroah-Hartman 	spin_lock_irqsave(&info->slock, flags);
146959908433SJiri Slaby 	lcr = inb(info->ioaddr + UART_LCR);
1470a6afd9f3SGreg Kroah-Hartman 	if (break_state == -1)
147159908433SJiri Slaby 		lcr |= UART_LCR_SBC;
1472a6afd9f3SGreg Kroah-Hartman 	else
147359908433SJiri Slaby 		lcr &= ~UART_LCR_SBC;
147459908433SJiri Slaby 	outb(lcr, info->ioaddr + UART_LCR);
1475a6afd9f3SGreg Kroah-Hartman 	spin_unlock_irqrestore(&info->slock, flags);
147659908433SJiri Slaby 
1477a6afd9f3SGreg Kroah-Hartman 	return 0;
1478a6afd9f3SGreg Kroah-Hartman }
1479a6afd9f3SGreg Kroah-Hartman 
mxser_receive_chars_new(struct mxser_port * port,u8 status)14809dd6f306SJiri Slaby static bool mxser_receive_chars_new(struct mxser_port *port, u8 status)
1481e5ce1bceSJiri Slaby {
1482e5ce1bceSJiri Slaby 	enum mxser_must_hwid hwid = port->board->must_hwid;
1483e5ce1bceSJiri Slaby 	u8 gdl;
1484e5ce1bceSJiri Slaby 
1485e5ce1bceSJiri Slaby 	if (hwid == MOXA_OTHER_UART)
1486e5ce1bceSJiri Slaby 		return false;
14877d5006d5SJiri Slaby 	if (status & (UART_LSR_BRK_ERROR_BITS | MOXA_MUST_LSR_RERR))
1488e5ce1bceSJiri Slaby 		return false;
1489e5ce1bceSJiri Slaby 
1490e5ce1bceSJiri Slaby 	gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
1491e5ce1bceSJiri Slaby 	if (hwid == MOXA_MUST_MU150_HWID)
1492e5ce1bceSJiri Slaby 		gdl &= MOXA_MUST_GDL_MASK;
1493e5ce1bceSJiri Slaby 
1494e5ce1bceSJiri Slaby 	while (gdl--) {
1495e5ce1bceSJiri Slaby 		u8 ch = inb(port->ioaddr + UART_RX);
1496eb68ac04SJiri Slaby 		if (!tty_insert_flip_char(&port->port, ch, 0))
1497eb68ac04SJiri Slaby 			port->icount.buf_overrun++;
1498e5ce1bceSJiri Slaby 	}
1499e5ce1bceSJiri Slaby 
1500e5ce1bceSJiri Slaby 	return true;
1501e5ce1bceSJiri Slaby }
1502e5ce1bceSJiri Slaby 
mxser_receive_chars_old(struct tty_struct * tty,struct mxser_port * port,u8 status)15030c419421SJiri Slaby static u8 mxser_receive_chars_old(struct tty_struct *tty,
150495b3ea4cSJiri Slaby 		                struct mxser_port *port, u8 status)
1505a6afd9f3SGreg Kroah-Hartman {
15060c419421SJiri Slaby 	enum mxser_must_hwid hwid = port->board->must_hwid;
1507a6afd9f3SGreg Kroah-Hartman 	int ignored = 0;
1508a6afd9f3SGreg Kroah-Hartman 	int max = 256;
15090c419421SJiri Slaby 	u8 ch;
1510a6afd9f3SGreg Kroah-Hartman 
1511a6afd9f3SGreg Kroah-Hartman 	do {
1512a6afd9f3SGreg Kroah-Hartman 		if (max-- < 0)
1513a6afd9f3SGreg Kroah-Hartman 			break;
1514a6afd9f3SGreg Kroah-Hartman 
1515a6afd9f3SGreg Kroah-Hartman 		ch = inb(port->ioaddr + UART_RX);
15160c419421SJiri Slaby 		if (hwid && (status & UART_LSR_OE))
1517d249e662SJiri Slaby 			outb(port->FCR | UART_FCR_CLEAR_RCVR,
1518aaa28e9fSJiri Slaby 					port->ioaddr + UART_FCR);
151915517806SJiri Slaby 		status &= port->read_status_mask;
152015517806SJiri Slaby 		if (status & port->ignore_status_mask) {
1521a6afd9f3SGreg Kroah-Hartman 			if (++ignored > 100)
1522a6afd9f3SGreg Kroah-Hartman 				break;
1523a6afd9f3SGreg Kroah-Hartman 		} else {
1524a6afd9f3SGreg Kroah-Hartman 			char flag = 0;
152570640052SJiri Slaby 			if (status & UART_LSR_BRK_ERROR_BITS) {
152615517806SJiri Slaby 				if (status & UART_LSR_BI) {
1527a6afd9f3SGreg Kroah-Hartman 					flag = TTY_BREAK;
1528a6afd9f3SGreg Kroah-Hartman 					port->icount.brk++;
1529a6afd9f3SGreg Kroah-Hartman 
1530a6afd9f3SGreg Kroah-Hartman 					if (port->port.flags & ASYNC_SAK)
1531a6afd9f3SGreg Kroah-Hartman 						do_SAK(tty);
153215517806SJiri Slaby 				} else if (status & UART_LSR_PE) {
1533a6afd9f3SGreg Kroah-Hartman 					flag = TTY_PARITY;
1534a6afd9f3SGreg Kroah-Hartman 					port->icount.parity++;
153515517806SJiri Slaby 				} else if (status & UART_LSR_FE) {
1536a6afd9f3SGreg Kroah-Hartman 					flag = TTY_FRAME;
1537a6afd9f3SGreg Kroah-Hartman 					port->icount.frame++;
153815517806SJiri Slaby 				} else if (status & UART_LSR_OE) {
1539a6afd9f3SGreg Kroah-Hartman 					flag = TTY_OVERRUN;
1540a6afd9f3SGreg Kroah-Hartman 					port->icount.overrun++;
15416de6e5c4SJiri Slaby 				}
1542a6afd9f3SGreg Kroah-Hartman 			}
1543eb68ac04SJiri Slaby 			if (!tty_insert_flip_char(&port->port, ch, flag)) {
1544eb68ac04SJiri Slaby 				port->icount.buf_overrun++;
1545a6afd9f3SGreg Kroah-Hartman 				break;
1546a6afd9f3SGreg Kroah-Hartman 			}
1547eb68ac04SJiri Slaby 		}
1548a6afd9f3SGreg Kroah-Hartman 
15490c419421SJiri Slaby 		if (hwid)
1550a6afd9f3SGreg Kroah-Hartman 			break;
1551a6afd9f3SGreg Kroah-Hartman 
155215517806SJiri Slaby 		status = inb(port->ioaddr + UART_LSR);
155315517806SJiri Slaby 	} while (status & UART_LSR_DR);
1554a6afd9f3SGreg Kroah-Hartman 
15550c419421SJiri Slaby 	return status;
15560c419421SJiri Slaby }
15570c419421SJiri Slaby 
mxser_receive_chars(struct tty_struct * tty,struct mxser_port * port,u8 status)15580c419421SJiri Slaby static u8 mxser_receive_chars(struct tty_struct *tty,
15590c419421SJiri Slaby 		struct mxser_port *port, u8 status)
15600c419421SJiri Slaby {
15619dd6f306SJiri Slaby 	if (!mxser_receive_chars_new(port, status))
156295b3ea4cSJiri Slaby 		status = mxser_receive_chars_old(tty, port, status);
15630c419421SJiri Slaby 
15642e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->port);
156515517806SJiri Slaby 
156615517806SJiri Slaby 	return status;
1567a6afd9f3SGreg Kroah-Hartman }
1568a6afd9f3SGreg Kroah-Hartman 
mxser_transmit_chars(struct tty_struct * tty,struct mxser_port * port)1569a6afd9f3SGreg Kroah-Hartman static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
1570a6afd9f3SGreg Kroah-Hartman {
15713b88dbffSJiri Slaby 	int count;
1572a6afd9f3SGreg Kroah-Hartman 
1573a6afd9f3SGreg Kroah-Hartman 	if (port->x_char) {
1574a6afd9f3SGreg Kroah-Hartman 		outb(port->x_char, port->ioaddr + UART_TX);
1575a6afd9f3SGreg Kroah-Hartman 		port->x_char = 0;
1576a6afd9f3SGreg Kroah-Hartman 		port->icount.tx++;
1577a6afd9f3SGreg Kroah-Hartman 		return;
1578a6afd9f3SGreg Kroah-Hartman 	}
1579a6afd9f3SGreg Kroah-Hartman 
158032330c83SJiri Slaby 	if (kfifo_is_empty(&port->port.xmit_fifo) || tty->flow.stopped ||
15815d1ea1adSJiri Slaby 			(tty->hw_stopped && !mxser_16550A_or_MUST(port))) {
1582740165f7SJiri Slaby 		__mxser_stop_tx(port);
1583a6afd9f3SGreg Kroah-Hartman 		return;
1584a6afd9f3SGreg Kroah-Hartman 	}
1585a6afd9f3SGreg Kroah-Hartman 
1586a6afd9f3SGreg Kroah-Hartman 	count = port->xmit_fifo_size;
1587a6afd9f3SGreg Kroah-Hartman 	do {
158832330c83SJiri Slaby 		unsigned char c;
158932330c83SJiri Slaby 
159032330c83SJiri Slaby 		if (!kfifo_get(&port->port.xmit_fifo, &c))
1591a6afd9f3SGreg Kroah-Hartman 			break;
159232330c83SJiri Slaby 
159332330c83SJiri Slaby 		outb(c, port->ioaddr + UART_TX);
159432330c83SJiri Slaby 		port->icount.tx++;
1595a6afd9f3SGreg Kroah-Hartman 	} while (--count > 0);
1596a6afd9f3SGreg Kroah-Hartman 
159732330c83SJiri Slaby 	if (kfifo_len(&port->port.xmit_fifo) < WAKEUP_CHARS)
1598a6afd9f3SGreg Kroah-Hartman 		tty_wakeup(tty);
1599a6afd9f3SGreg Kroah-Hartman 
160032330c83SJiri Slaby 	if (kfifo_is_empty(&port->port.xmit_fifo))
1601740165f7SJiri Slaby 		__mxser_stop_tx(port);
1602a6afd9f3SGreg Kroah-Hartman }
1603a6afd9f3SGreg Kroah-Hartman 
mxser_port_isr(struct mxser_port * port)16049e40ea1fSJiri Slaby static bool mxser_port_isr(struct mxser_port *port)
16059e40ea1fSJiri Slaby {
16069e40ea1fSJiri Slaby 	struct tty_struct *tty;
160730f6027fSJiri Slaby 	u8 iir, status;
16089e40ea1fSJiri Slaby 	bool error = false;
16099e40ea1fSJiri Slaby 
16109e40ea1fSJiri Slaby 	iir = inb(port->ioaddr + UART_IIR);
16119e40ea1fSJiri Slaby 	if (iir & UART_IIR_NO_INT)
16129e40ea1fSJiri Slaby 		return true;
16139e40ea1fSJiri Slaby 
16149e40ea1fSJiri Slaby 	iir &= MOXA_MUST_IIR_MASK;
16159e40ea1fSJiri Slaby 	tty = tty_port_tty_get(&port->port);
1616274ab58dSJiri Slaby 	if (!tty) {
16179e40ea1fSJiri Slaby 		status = inb(port->ioaddr + UART_LSR);
1618d249e662SJiri Slaby 		outb(port->FCR | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
1619aaa28e9fSJiri Slaby 				port->ioaddr + UART_FCR);
16209e40ea1fSJiri Slaby 		inb(port->ioaddr + UART_MSR);
16219e40ea1fSJiri Slaby 
16229e40ea1fSJiri Slaby 		error = true;
16239e40ea1fSJiri Slaby 		goto put_tty;
16249e40ea1fSJiri Slaby 	}
16259e40ea1fSJiri Slaby 
16269e40ea1fSJiri Slaby 	status = inb(port->ioaddr + UART_LSR);
16279e40ea1fSJiri Slaby 
16289e40ea1fSJiri Slaby 	if (port->board->must_hwid) {
16299e40ea1fSJiri Slaby 		if (iir == MOXA_MUST_IIR_GDA ||
16309e40ea1fSJiri Slaby 		    iir == MOXA_MUST_IIR_RDA ||
16319e40ea1fSJiri Slaby 		    iir == MOXA_MUST_IIR_RTO ||
16329e40ea1fSJiri Slaby 		    iir == MOXA_MUST_IIR_LSR)
16339e40ea1fSJiri Slaby 			status = mxser_receive_chars(tty, port, status);
16349e40ea1fSJiri Slaby 	} else {
16359e40ea1fSJiri Slaby 		status &= port->read_status_mask;
16369e40ea1fSJiri Slaby 		if (status & UART_LSR_DR)
16379e40ea1fSJiri Slaby 			status = mxser_receive_chars(tty, port, status);
16389e40ea1fSJiri Slaby 	}
16399e40ea1fSJiri Slaby 
164030f6027fSJiri Slaby 	mxser_check_modem_status(tty, port);
16419e40ea1fSJiri Slaby 
16429e40ea1fSJiri Slaby 	if (port->board->must_hwid) {
16439e40ea1fSJiri Slaby 		if (iir == 0x02 && (status & UART_LSR_THRE))
16449e40ea1fSJiri Slaby 			mxser_transmit_chars(tty, port);
16459e40ea1fSJiri Slaby 	} else {
16469e40ea1fSJiri Slaby 		if (status & UART_LSR_THRE)
16479e40ea1fSJiri Slaby 			mxser_transmit_chars(tty, port);
16489e40ea1fSJiri Slaby 	}
16499e40ea1fSJiri Slaby 
16509e40ea1fSJiri Slaby put_tty:
16519e40ea1fSJiri Slaby 	tty_kref_put(tty);
16529e40ea1fSJiri Slaby 
16539e40ea1fSJiri Slaby 	return error;
16549e40ea1fSJiri Slaby }
16559e40ea1fSJiri Slaby 
1656a6afd9f3SGreg Kroah-Hartman /*
1657a6afd9f3SGreg Kroah-Hartman  * This is the serial driver's generic interrupt routine
1658a6afd9f3SGreg Kroah-Hartman  */
mxser_interrupt(int irq,void * dev_id)1659a6afd9f3SGreg Kroah-Hartman static irqreturn_t mxser_interrupt(int irq, void *dev_id)
1660a6afd9f3SGreg Kroah-Hartman {
1661cef222cbSJiri Slaby 	struct mxser_board *brd = dev_id;
1662a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *port;
1663a6afd9f3SGreg Kroah-Hartman 	unsigned int int_cnt, pass_counter = 0;
1664c24c31ffSJiri Slaby 	unsigned int i, max = brd->nports;
1665a6afd9f3SGreg Kroah-Hartman 	int handled = IRQ_NONE;
16669cb5c9c3SJiri Slaby 	u8 irqbits, bits, mask = BIT(max) - 1;
1667a6afd9f3SGreg Kroah-Hartman 
1668a6afd9f3SGreg Kroah-Hartman 	while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
16699cb5c9c3SJiri Slaby 		irqbits = inb(brd->vector) & mask;
16709cb5c9c3SJiri Slaby 		if (irqbits == mask)
1671a6afd9f3SGreg Kroah-Hartman 			break;
1672a6afd9f3SGreg Kroah-Hartman 
1673a6afd9f3SGreg Kroah-Hartman 		handled = IRQ_HANDLED;
1674a6afd9f3SGreg Kroah-Hartman 		for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
16759cb5c9c3SJiri Slaby 			if (irqbits == mask)
1676a6afd9f3SGreg Kroah-Hartman 				break;
1677a6afd9f3SGreg Kroah-Hartman 			if (bits & irqbits)
1678a6afd9f3SGreg Kroah-Hartman 				continue;
1679a6afd9f3SGreg Kroah-Hartman 			port = &brd->ports[i];
1680a6afd9f3SGreg Kroah-Hartman 
1681a6afd9f3SGreg Kroah-Hartman 			int_cnt = 0;
1682a6afd9f3SGreg Kroah-Hartman 			spin_lock(&port->slock);
1683a6afd9f3SGreg Kroah-Hartman 			do {
16849e40ea1fSJiri Slaby 				if (mxser_port_isr(port))
1685a6afd9f3SGreg Kroah-Hartman 					break;
1686a6afd9f3SGreg Kroah-Hartman 			} while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
1687a6afd9f3SGreg Kroah-Hartman 			spin_unlock(&port->slock);
1688a6afd9f3SGreg Kroah-Hartman 		}
1689a6afd9f3SGreg Kroah-Hartman 	}
1690a6afd9f3SGreg Kroah-Hartman 
1691a6afd9f3SGreg Kroah-Hartman 	return handled;
1692a6afd9f3SGreg Kroah-Hartman }
1693a6afd9f3SGreg Kroah-Hartman 
1694a6afd9f3SGreg Kroah-Hartman static const struct tty_operations mxser_ops = {
1695a6afd9f3SGreg Kroah-Hartman 	.open = mxser_open,
1696a6afd9f3SGreg Kroah-Hartman 	.close = mxser_close,
1697a6afd9f3SGreg Kroah-Hartman 	.write = mxser_write,
1698a6afd9f3SGreg Kroah-Hartman 	.put_char = mxser_put_char,
1699a6afd9f3SGreg Kroah-Hartman 	.flush_chars = mxser_flush_chars,
1700a6afd9f3SGreg Kroah-Hartman 	.write_room = mxser_write_room,
1701a6afd9f3SGreg Kroah-Hartman 	.chars_in_buffer = mxser_chars_in_buffer,
1702a6afd9f3SGreg Kroah-Hartman 	.flush_buffer = mxser_flush_buffer,
1703a6afd9f3SGreg Kroah-Hartman 	.ioctl = mxser_ioctl,
1704a6afd9f3SGreg Kroah-Hartman 	.throttle = mxser_throttle,
1705a6afd9f3SGreg Kroah-Hartman 	.unthrottle = mxser_unthrottle,
1706a6afd9f3SGreg Kroah-Hartman 	.set_termios = mxser_set_termios,
1707a6afd9f3SGreg Kroah-Hartman 	.stop = mxser_stop,
1708a6afd9f3SGreg Kroah-Hartman 	.start = mxser_start,
1709a6afd9f3SGreg Kroah-Hartman 	.hangup = mxser_hangup,
1710a6afd9f3SGreg Kroah-Hartman 	.break_ctl = mxser_rs_break,
1711a6afd9f3SGreg Kroah-Hartman 	.wait_until_sent = mxser_wait_until_sent,
1712a6afd9f3SGreg Kroah-Hartman 	.tiocmget = mxser_tiocmget,
1713a6afd9f3SGreg Kroah-Hartman 	.tiocmset = mxser_tiocmset,
17146da5b587SAl Viro 	.set_serial = mxser_set_serial_info,
17156da5b587SAl Viro 	.get_serial = mxser_get_serial_info,
1716a6afd9f3SGreg Kroah-Hartman 	.get_icount = mxser_get_icount,
1717a6afd9f3SGreg Kroah-Hartman };
1718a6afd9f3SGreg Kroah-Hartman 
171904b757dfSAya Mahfouz static const struct tty_port_operations mxser_port_ops = {
1720a6afd9f3SGreg Kroah-Hartman 	.carrier_raised = mxser_carrier_raised,
1721a6afd9f3SGreg Kroah-Hartman 	.dtr_rts = mxser_dtr_rts,
1722a6afd9f3SGreg Kroah-Hartman 	.activate = mxser_activate,
1723a6afd9f3SGreg Kroah-Hartman 	.shutdown = mxser_shutdown_port,
1724a6afd9f3SGreg Kroah-Hartman };
1725a6afd9f3SGreg Kroah-Hartman 
1726a6afd9f3SGreg Kroah-Hartman /*
1727a6afd9f3SGreg Kroah-Hartman  * The MOXA Smartio/Industio serial driver boot-time initialization code!
1728a6afd9f3SGreg Kroah-Hartman  */
1729a6afd9f3SGreg Kroah-Hartman 
mxser_initbrd(struct mxser_board * brd,bool high_baud)1730c24c31ffSJiri Slaby static void mxser_initbrd(struct mxser_board *brd, bool high_baud)
1731a6afd9f3SGreg Kroah-Hartman {
1732a6afd9f3SGreg Kroah-Hartman 	struct mxser_port *info;
1733a6afd9f3SGreg Kroah-Hartman 	unsigned int i;
173457faa7d6SJiri Slaby 	bool is_mu860;
173557faa7d6SJiri Slaby 
173657faa7d6SJiri Slaby 	brd->must_hwid = mxser_must_get_hwid(brd->ports[0].ioaddr);
173757faa7d6SJiri Slaby 	is_mu860 = brd->must_hwid == MOXA_MUST_MU860_HWID;
173857faa7d6SJiri Slaby 
173957faa7d6SJiri Slaby 	for (i = 0; i < UART_INFO_NUM; i++) {
174057faa7d6SJiri Slaby 		if (Gpci_uart_info[i].type == brd->must_hwid) {
174157faa7d6SJiri Slaby 			brd->max_baud = Gpci_uart_info[i].max_baud;
174257faa7d6SJiri Slaby 
174357faa7d6SJiri Slaby 			/* exception....CP-102 */
1744c24c31ffSJiri Slaby 			if (high_baud)
174557faa7d6SJiri Slaby 				brd->max_baud = 921600;
174657faa7d6SJiri Slaby 			break;
174757faa7d6SJiri Slaby 		}
174857faa7d6SJiri Slaby 	}
174957faa7d6SJiri Slaby 
175057faa7d6SJiri Slaby 	if (is_mu860) {
175157faa7d6SJiri Slaby 		/* set to RS232 mode by default */
175257faa7d6SJiri Slaby 		outb(0, brd->vector + 4);
175357faa7d6SJiri Slaby 		outb(0, brd->vector + 0x0c);
175457faa7d6SJiri Slaby 	}
1755a6afd9f3SGreg Kroah-Hartman 
1756c24c31ffSJiri Slaby 	for (i = 0; i < brd->nports; i++) {
1757a6afd9f3SGreg Kroah-Hartman 		info = &brd->ports[i];
175857faa7d6SJiri Slaby 		if (is_mu860) {
175957faa7d6SJiri Slaby 			if (i < 4)
176057faa7d6SJiri Slaby 				info->opmode_ioaddr = brd->vector + 4;
176157faa7d6SJiri Slaby 			else
176257faa7d6SJiri Slaby 				info->opmode_ioaddr = brd->vector + 0x0c;
176357faa7d6SJiri Slaby 		}
1764a6afd9f3SGreg Kroah-Hartman 		tty_port_init(&info->port);
1765a6afd9f3SGreg Kroah-Hartman 		info->port.ops = &mxser_port_ops;
1766a6afd9f3SGreg Kroah-Hartman 		info->board = brd;
1767a6afd9f3SGreg Kroah-Hartman 
1768a6afd9f3SGreg Kroah-Hartman 		/* Enhance mode enabled here */
1769292955a7SJiri Slaby 		if (brd->must_hwid != MOXA_OTHER_UART)
1770edb7d27cSJiri Slaby 			mxser_must_set_enhance_mode(info->ioaddr, true);
1771a6afd9f3SGreg Kroah-Hartman 
177258a2ddb3SJiri Slaby 		info->type = PORT_16550A;
1773a6afd9f3SGreg Kroah-Hartman 
1774c3db20c3SJiri Slaby 		mxser_process_txrx_fifo(info);
1775a6afd9f3SGreg Kroah-Hartman 
1776a6afd9f3SGreg Kroah-Hartman 		info->port.close_delay = 5 * HZ / 10;
1777a6afd9f3SGreg Kroah-Hartman 		info->port.closing_wait = 30 * HZ;
1778a6afd9f3SGreg Kroah-Hartman 		spin_lock_init(&info->slock);
1779a6afd9f3SGreg Kroah-Hartman 
1780a6afd9f3SGreg Kroah-Hartman 		/* before set INT ISR, disable all int */
1781a6afd9f3SGreg Kroah-Hartman 		outb(inb(info->ioaddr + UART_IER) & 0xf0,
1782a6afd9f3SGreg Kroah-Hartman 			info->ioaddr + UART_IER);
1783a6afd9f3SGreg Kroah-Hartman 	}
1784a6afd9f3SGreg Kroah-Hartman }
1785a6afd9f3SGreg Kroah-Hartman 
mxser_probe(struct pci_dev * pdev,const struct pci_device_id * ent)17869671f099SBill Pemberton static int mxser_probe(struct pci_dev *pdev,
1787a6afd9f3SGreg Kroah-Hartman 		const struct pci_device_id *ent)
1788a6afd9f3SGreg Kroah-Hartman {
1789a6afd9f3SGreg Kroah-Hartman 	struct mxser_board *brd;
179013d4aba8SJiri Slaby 	unsigned int i, base;
1791a6afd9f3SGreg Kroah-Hartman 	unsigned long ioaddress;
1792c24c31ffSJiri Slaby 	unsigned short nports = MXSER_NPORTS(ent->driver_data);
17939e17df37SAlexey Khoroshilov 	struct device *tty_dev;
1794a6afd9f3SGreg Kroah-Hartman 	int retval = -EINVAL;
1795a6afd9f3SGreg Kroah-Hartman 
1796f8b6b327SJiri Slaby 	i = find_first_zero_bit(mxser_boards, MXSER_BOARDS);
1797a6afd9f3SGreg Kroah-Hartman 	if (i >= MXSER_BOARDS) {
1798a6afd9f3SGreg Kroah-Hartman 		dev_err(&pdev->dev, "too many boards found (maximum %d), board "
1799a6afd9f3SGreg Kroah-Hartman 				"not configured\n", MXSER_BOARDS);
1800a6afd9f3SGreg Kroah-Hartman 		goto err;
1801a6afd9f3SGreg Kroah-Hartman 	}
1802a6afd9f3SGreg Kroah-Hartman 
1803ad1c92ffSJiri Slaby 	brd = devm_kzalloc(&pdev->dev, struct_size(brd, ports, nports),
1804ad1c92ffSJiri Slaby 			GFP_KERNEL);
1805f8b6b327SJiri Slaby 	if (!brd)
1806f8b6b327SJiri Slaby 		goto err;
1807f8b6b327SJiri Slaby 
180813d4aba8SJiri Slaby 	brd->idx = i;
1809f8b6b327SJiri Slaby 	__set_bit(brd->idx, mxser_boards);
181013d4aba8SJiri Slaby 	base = i * MXSER_PORTS_PER_BOARD;
1811a6afd9f3SGreg Kroah-Hartman 
1812dcb04e21SJiri Slaby 	retval = pcim_enable_device(pdev);
1813a6afd9f3SGreg Kroah-Hartman 	if (retval) {
1814a6afd9f3SGreg Kroah-Hartman 		dev_err(&pdev->dev, "PCI enable failed\n");
1815f8b6b327SJiri Slaby 		goto err_zero;
1816a6afd9f3SGreg Kroah-Hartman 	}
1817a6afd9f3SGreg Kroah-Hartman 
1818a6afd9f3SGreg Kroah-Hartman 	/* io address */
1819a6afd9f3SGreg Kroah-Hartman 	ioaddress = pci_resource_start(pdev, 2);
1820a6afd9f3SGreg Kroah-Hartman 	retval = pci_request_region(pdev, 2, "mxser(IO)");
1821a6afd9f3SGreg Kroah-Hartman 	if (retval)
1822f8b6b327SJiri Slaby 		goto err_zero;
1823a6afd9f3SGreg Kroah-Hartman 
1824c24c31ffSJiri Slaby 	brd->nports = nports;
1825c24c31ffSJiri Slaby 	for (i = 0; i < nports; i++)
1826a6afd9f3SGreg Kroah-Hartman 		brd->ports[i].ioaddr = ioaddress + 8 * i;
1827a6afd9f3SGreg Kroah-Hartman 
1828a6afd9f3SGreg Kroah-Hartman 	/* vector */
1829a6afd9f3SGreg Kroah-Hartman 	ioaddress = pci_resource_start(pdev, 3);
1830a6afd9f3SGreg Kroah-Hartman 	retval = pci_request_region(pdev, 3, "mxser(vector)");
1831a6afd9f3SGreg Kroah-Hartman 	if (retval)
1832a6afd9f3SGreg Kroah-Hartman 		goto err_zero;
1833a6afd9f3SGreg Kroah-Hartman 	brd->vector = ioaddress;
1834a6afd9f3SGreg Kroah-Hartman 
1835a6afd9f3SGreg Kroah-Hartman 	/* irq */
1836a6afd9f3SGreg Kroah-Hartman 	brd->irq = pdev->irq;
1837a6afd9f3SGreg Kroah-Hartman 
1838c24c31ffSJiri Slaby 	mxser_initbrd(brd, ent->driver_data & MXSER_HIGHBAUD);
18397f0e79dcSJiri Slaby 
18407f0e79dcSJiri Slaby 	retval = devm_request_irq(&pdev->dev, brd->irq, mxser_interrupt,
18417f0e79dcSJiri Slaby 			IRQF_SHARED, "mxser", brd);
18427f0e79dcSJiri Slaby 	if (retval) {
18437f0e79dcSJiri Slaby 		dev_err(&pdev->dev, "request irq failed");
18447f0e79dcSJiri Slaby 		goto err_relbrd;
18457f0e79dcSJiri Slaby 	}
1846a6afd9f3SGreg Kroah-Hartman 
1847c24c31ffSJiri Slaby 	for (i = 0; i < nports; i++) {
18489e17df37SAlexey Khoroshilov 		tty_dev = tty_port_register_device(&brd->ports[i].port,
184913d4aba8SJiri Slaby 				mxvar_sdriver, base + i, &pdev->dev);
18509e17df37SAlexey Khoroshilov 		if (IS_ERR(tty_dev)) {
18519e17df37SAlexey Khoroshilov 			retval = PTR_ERR(tty_dev);
18521b581f17SAlexey Khoroshilov 			for (; i > 0; i--)
18539e17df37SAlexey Khoroshilov 				tty_unregister_device(mxvar_sdriver,
185413d4aba8SJiri Slaby 					base + i - 1);
18559e17df37SAlexey Khoroshilov 			goto err_relbrd;
18569e17df37SAlexey Khoroshilov 		}
18579e17df37SAlexey Khoroshilov 	}
1858a6afd9f3SGreg Kroah-Hartman 
1859a6afd9f3SGreg Kroah-Hartman 	pci_set_drvdata(pdev, brd);
1860a6afd9f3SGreg Kroah-Hartman 
1861a6afd9f3SGreg Kroah-Hartman 	return 0;
18629e17df37SAlexey Khoroshilov err_relbrd:
1863c24c31ffSJiri Slaby 	for (i = 0; i < nports; i++)
18649e17df37SAlexey Khoroshilov 		tty_port_destroy(&brd->ports[i].port);
1865a6afd9f3SGreg Kroah-Hartman err_zero:
1866f8b6b327SJiri Slaby 	__clear_bit(brd->idx, mxser_boards);
1867a6afd9f3SGreg Kroah-Hartman err:
1868a6afd9f3SGreg Kroah-Hartman 	return retval;
1869a6afd9f3SGreg Kroah-Hartman }
1870a6afd9f3SGreg Kroah-Hartman 
mxser_remove(struct pci_dev * pdev)1871ae8d8a14SBill Pemberton static void mxser_remove(struct pci_dev *pdev)
1872a6afd9f3SGreg Kroah-Hartman {
1873a6afd9f3SGreg Kroah-Hartman 	struct mxser_board *brd = pci_get_drvdata(pdev);
187413d4aba8SJiri Slaby 	unsigned int i, base = brd->idx * MXSER_PORTS_PER_BOARD;
1875a6afd9f3SGreg Kroah-Hartman 
1876c24c31ffSJiri Slaby 	for (i = 0; i < brd->nports; i++) {
187713d4aba8SJiri Slaby 		tty_unregister_device(mxvar_sdriver, base + i);
1878d450f085SJiri Slaby 		tty_port_destroy(&brd->ports[i].port);
1879d450f085SJiri Slaby 	}
1880d450f085SJiri Slaby 
1881f8b6b327SJiri Slaby 	__clear_bit(brd->idx, mxser_boards);
1882a6afd9f3SGreg Kroah-Hartman }
1883a6afd9f3SGreg Kroah-Hartman 
1884a6afd9f3SGreg Kroah-Hartman static struct pci_driver mxser_driver = {
1885a6afd9f3SGreg Kroah-Hartman 	.name = "mxser",
1886a6afd9f3SGreg Kroah-Hartman 	.id_table = mxser_pcibrds,
1887a6afd9f3SGreg Kroah-Hartman 	.probe = mxser_probe,
188891116cbaSBill Pemberton 	.remove = mxser_remove
1889a6afd9f3SGreg Kroah-Hartman };
1890a6afd9f3SGreg Kroah-Hartman 
mxser_module_init(void)1891a6afd9f3SGreg Kroah-Hartman static int __init mxser_module_init(void)
1892a6afd9f3SGreg Kroah-Hartman {
1893a6afd9f3SGreg Kroah-Hartman 	int retval;
1894a6afd9f3SGreg Kroah-Hartman 
189539b7b42bSJiri Slaby 	mxvar_sdriver = tty_alloc_driver(MXSER_PORTS, TTY_DRIVER_REAL_RAW |
189639b7b42bSJiri Slaby 			TTY_DRIVER_DYNAMIC_DEV);
189739b7b42bSJiri Slaby 	if (IS_ERR(mxvar_sdriver))
189839b7b42bSJiri Slaby 		return PTR_ERR(mxvar_sdriver);
1899a6afd9f3SGreg Kroah-Hartman 
1900a6afd9f3SGreg Kroah-Hartman 	/* Initialize the tty_driver structure */
1901a6afd9f3SGreg Kroah-Hartman 	mxvar_sdriver->name = "ttyMI";
1902a6afd9f3SGreg Kroah-Hartman 	mxvar_sdriver->major = ttymajor;
1903a6afd9f3SGreg Kroah-Hartman 	mxvar_sdriver->minor_start = 0;
1904a6afd9f3SGreg Kroah-Hartman 	mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
1905a6afd9f3SGreg Kroah-Hartman 	mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
1906a6afd9f3SGreg Kroah-Hartman 	mxvar_sdriver->init_termios = tty_std_termios;
1907a6afd9f3SGreg Kroah-Hartman 	mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
1908a6afd9f3SGreg Kroah-Hartman 	tty_set_operations(mxvar_sdriver, &mxser_ops);
1909a6afd9f3SGreg Kroah-Hartman 
1910a6afd9f3SGreg Kroah-Hartman 	retval = tty_register_driver(mxvar_sdriver);
1911a6afd9f3SGreg Kroah-Hartman 	if (retval) {
1912a6afd9f3SGreg Kroah-Hartman 		printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
1913a6afd9f3SGreg Kroah-Hartman 				"tty driver !\n");
1914a6afd9f3SGreg Kroah-Hartman 		goto err_put;
1915a6afd9f3SGreg Kroah-Hartman 	}
1916a6afd9f3SGreg Kroah-Hartman 
1917a6afd9f3SGreg Kroah-Hartman 	retval = pci_register_driver(&mxser_driver);
1918a6afd9f3SGreg Kroah-Hartman 	if (retval) {
1919a6afd9f3SGreg Kroah-Hartman 		printk(KERN_ERR "mxser: can't register pci driver\n");
1920a6afd9f3SGreg Kroah-Hartman 		goto err_unr;
1921a6afd9f3SGreg Kroah-Hartman 	}
1922a6afd9f3SGreg Kroah-Hartman 
1923a6afd9f3SGreg Kroah-Hartman 	return 0;
1924a6afd9f3SGreg Kroah-Hartman err_unr:
1925a6afd9f3SGreg Kroah-Hartman 	tty_unregister_driver(mxvar_sdriver);
1926a6afd9f3SGreg Kroah-Hartman err_put:
19279f90a4ddSJiri Slaby 	tty_driver_kref_put(mxvar_sdriver);
1928a6afd9f3SGreg Kroah-Hartman 	return retval;
1929a6afd9f3SGreg Kroah-Hartman }
1930a6afd9f3SGreg Kroah-Hartman 
mxser_module_exit(void)1931a6afd9f3SGreg Kroah-Hartman static void __exit mxser_module_exit(void)
1932a6afd9f3SGreg Kroah-Hartman {
1933a6afd9f3SGreg Kroah-Hartman 	pci_unregister_driver(&mxser_driver);
1934a6afd9f3SGreg Kroah-Hartman 	tty_unregister_driver(mxvar_sdriver);
19359f90a4ddSJiri Slaby 	tty_driver_kref_put(mxvar_sdriver);
1936a6afd9f3SGreg Kroah-Hartman }
1937a6afd9f3SGreg Kroah-Hartman 
1938a6afd9f3SGreg Kroah-Hartman module_init(mxser_module_init);
1939a6afd9f3SGreg Kroah-Hartman module_exit(mxser_module_exit);
1940