Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * mxser.c -- MOXA Smartio/Industio family multiport serial driver. |
| 4 | * |
Jiri Slaby | 80ff8a8 | 2008-02-07 00:16:51 -0800 | [diff] [blame] | 5 | * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com). |
| 6 | * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 8 | * This code is loosely based on the 1.8 moxa driver which is based on |
| 9 | * Linux serial driver, written by Linus Torvalds, Theodore T'so and |
| 10 | * others. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox |
Alan Cox | 8eb04cf | 2008-11-11 14:48:44 +0000 | [diff] [blame] | 13 | * <alan@lxorguk.ukuu.org.uk>. The original 1.8 code is available on |
| 14 | * www.moxa.com. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * - Fixed x86_64 cleanness |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/errno.h> |
| 20 | #include <linux/signal.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/timer.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/tty.h> |
| 25 | #include <linux/tty_flip.h> |
| 26 | #include <linux/serial.h> |
| 27 | #include <linux/serial_reg.h> |
| 28 | #include <linux/major.h> |
| 29 | #include <linux/string.h> |
| 30 | #include <linux/fcntl.h> |
| 31 | #include <linux/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/ioport.h> |
| 33 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/delay.h> |
| 35 | #include <linux/pci.h> |
Jiri Slaby | 1977f03 | 2007-10-18 23:40:25 -0700 | [diff] [blame] | 36 | #include <linux/bitops.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
Manuel Zerpies | 5a3c6b251 | 2011-06-16 14:07:22 +0200 | [diff] [blame] | 38 | #include <linux/ratelimit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <asm/io.h> |
| 41 | #include <asm/irq.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 42 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Jiri Slaby | 4463cc5 | 2021-06-18 08:14:10 +0200 | [diff] [blame] | 44 | /* |
| 45 | * Semi-public control interfaces |
| 46 | */ |
| 47 | |
| 48 | /* |
| 49 | * MOXA ioctls |
| 50 | */ |
| 51 | |
| 52 | #define MOXA 0x400 |
Jiri Slaby | 4463cc5 | 2021-06-18 08:14:10 +0200 | [diff] [blame] | 53 | #define MOXA_SET_OP_MODE (MOXA + 66) |
| 54 | #define MOXA_GET_OP_MODE (MOXA + 67) |
| 55 | |
| 56 | #define RS232_MODE 0 |
| 57 | #define RS485_2WIRE_MODE 1 |
| 58 | #define RS422_MODE 2 |
| 59 | #define RS485_4WIRE_MODE 3 |
| 60 | #define OP_MODE_MASK 3 |
| 61 | |
Jiri Slaby | 4463cc5 | 2021-06-18 08:14:10 +0200 | [diff] [blame] | 62 | /* --------------------------------------------------- */ |
| 63 | |
Jiri Slaby | 4463cc5 | 2021-06-18 08:14:10 +0200 | [diff] [blame] | 64 | /* |
| 65 | * Follow just what Moxa Must chip defines. |
| 66 | * |
Jiri Slaby | 464fbf6 | 2021-06-18 08:15:08 +0200 | [diff] [blame] | 67 | * When LCR register (offset 0x03) is written the following value, the Must chip |
| 68 | * will enter enhanced mode. And a write to EFR (offset 0x02) bit 6,7 will |
Jiri Slaby | 4463cc5 | 2021-06-18 08:14:10 +0200 | [diff] [blame] | 69 | * change bank. |
| 70 | */ |
Jiri Slaby | 464fbf6 | 2021-06-18 08:15:08 +0200 | [diff] [blame] | 71 | #define MOXA_MUST_ENTER_ENHANCED 0xBF |
Jiri Slaby | 4463cc5 | 2021-06-18 08:14:10 +0200 | [diff] [blame] | 72 | |
Jiri Slaby | 464fbf6 | 2021-06-18 08:15:08 +0200 | [diff] [blame] | 73 | /* when enhanced mode is enabled, access to general bank register */ |
Jiri Slaby | 4463cc5 | 2021-06-18 08:14:10 +0200 | [diff] [blame] | 74 | #define MOXA_MUST_GDL_REGISTER 0x07 |
| 75 | #define MOXA_MUST_GDL_MASK 0x7F |
| 76 | #define MOXA_MUST_GDL_HAS_BAD_DATA 0x80 |
| 77 | |
| 78 | #define MOXA_MUST_LSR_RERR 0x80 /* error in receive FIFO */ |
Jiri Slaby | 464fbf6 | 2021-06-18 08:15:08 +0200 | [diff] [blame] | 79 | /* enhanced register bank select and enhanced mode setting register */ |
| 80 | /* This works only when LCR register equals to 0xBF */ |
Jiri Slaby | 4463cc5 | 2021-06-18 08:14:10 +0200 | [diff] [blame] | 81 | #define MOXA_MUST_EFR_REGISTER 0x02 |
Jiri Slaby | 464fbf6 | 2021-06-18 08:15:08 +0200 | [diff] [blame] | 82 | #define MOXA_MUST_EFR_EFRB_ENABLE 0x10 /* enhanced mode enable */ |
| 83 | /* enhanced register bank set 0, 1, 2 */ |
Jiri Slaby | 4463cc5 | 2021-06-18 08:14:10 +0200 | [diff] [blame] | 84 | #define MOXA_MUST_EFR_BANK0 0x00 |
| 85 | #define MOXA_MUST_EFR_BANK1 0x40 |
| 86 | #define MOXA_MUST_EFR_BANK2 0x80 |
| 87 | #define MOXA_MUST_EFR_BANK3 0xC0 |
| 88 | #define MOXA_MUST_EFR_BANK_MASK 0xC0 |
| 89 | |
| 90 | /* set XON1 value register, when LCR=0xBF and change to bank0 */ |
| 91 | #define MOXA_MUST_XON1_REGISTER 0x04 |
| 92 | |
| 93 | /* set XON2 value register, when LCR=0xBF and change to bank0 */ |
| 94 | #define MOXA_MUST_XON2_REGISTER 0x05 |
| 95 | |
| 96 | /* set XOFF1 value register, when LCR=0xBF and change to bank0 */ |
| 97 | #define MOXA_MUST_XOFF1_REGISTER 0x06 |
| 98 | |
| 99 | /* set XOFF2 value register, when LCR=0xBF and change to bank0 */ |
| 100 | #define MOXA_MUST_XOFF2_REGISTER 0x07 |
| 101 | |
| 102 | #define MOXA_MUST_RBRTL_REGISTER 0x04 |
| 103 | #define MOXA_MUST_RBRTH_REGISTER 0x05 |
| 104 | #define MOXA_MUST_RBRTI_REGISTER 0x06 |
| 105 | #define MOXA_MUST_THRTL_REGISTER 0x07 |
| 106 | #define MOXA_MUST_ENUM_REGISTER 0x04 |
| 107 | #define MOXA_MUST_HWID_REGISTER 0x05 |
| 108 | #define MOXA_MUST_ECR_REGISTER 0x06 |
| 109 | #define MOXA_MUST_CSR_REGISTER 0x07 |
| 110 | |
| 111 | #define MOXA_MUST_FCR_GDA_MODE_ENABLE 0x20 /* good data mode enable */ |
| 112 | #define MOXA_MUST_FCR_GDA_ONLY_ENABLE 0x10 /* only good data put into RxFIFO */ |
| 113 | |
| 114 | #define MOXA_MUST_IER_ECTSI 0x80 /* enable CTS interrupt */ |
| 115 | #define MOXA_MUST_IER_ERTSI 0x40 /* enable RTS interrupt */ |
| 116 | #define MOXA_MUST_IER_XINT 0x20 /* enable Xon/Xoff interrupt */ |
| 117 | #define MOXA_MUST_IER_EGDAI 0x10 /* enable GDA interrupt */ |
| 118 | |
| 119 | #define MOXA_MUST_RECV_ISR (UART_IER_RDI | MOXA_MUST_IER_EGDAI) |
| 120 | |
| 121 | /* GDA interrupt pending */ |
| 122 | #define MOXA_MUST_IIR_GDA 0x1C |
| 123 | #define MOXA_MUST_IIR_RDA 0x04 |
| 124 | #define MOXA_MUST_IIR_RTO 0x0C |
| 125 | #define MOXA_MUST_IIR_LSR 0x06 |
| 126 | |
| 127 | /* received Xon/Xoff or specical interrupt pending */ |
| 128 | #define MOXA_MUST_IIR_XSC 0x10 |
| 129 | |
| 130 | /* RTS/CTS change state interrupt pending */ |
| 131 | #define MOXA_MUST_IIR_RTSCTS 0x20 |
| 132 | #define MOXA_MUST_IIR_MASK 0x3E |
| 133 | |
| 134 | #define MOXA_MUST_MCR_XON_FLAG 0x40 |
| 135 | #define MOXA_MUST_MCR_XON_ANY 0x80 |
| 136 | #define MOXA_MUST_MCR_TX_XON 0x08 |
| 137 | |
| 138 | #define MOXA_MUST_EFR_SF_MASK 0x0F /* software flow control on chip mask value */ |
| 139 | #define MOXA_MUST_EFR_SF_TX1 0x08 /* send Xon1/Xoff1 */ |
| 140 | #define MOXA_MUST_EFR_SF_TX2 0x04 /* send Xon2/Xoff2 */ |
| 141 | #define MOXA_MUST_EFR_SF_TX12 0x0C /* send Xon1,Xon2/Xoff1,Xoff2 */ |
| 142 | #define MOXA_MUST_EFR_SF_TX_NO 0x00 /* don't send Xon/Xoff */ |
| 143 | #define MOXA_MUST_EFR_SF_TX_MASK 0x0C /* Tx software flow control mask */ |
| 144 | #define MOXA_MUST_EFR_SF_RX_NO 0x00 /* don't receive Xon/Xoff */ |
| 145 | #define MOXA_MUST_EFR_SF_RX1 0x02 /* receive Xon1/Xoff1 */ |
| 146 | #define MOXA_MUST_EFR_SF_RX2 0x01 /* receive Xon2/Xoff2 */ |
| 147 | #define MOXA_MUST_EFR_SF_RX12 0x03 /* receive Xon1,Xon2/Xoff1,Xoff2 */ |
| 148 | #define MOXA_MUST_EFR_SF_RX_MASK 0x03 /* Rx software flow control mask */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | #define MXSERMAJOR 174 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | #define MXSER_BOARDS 4 /* Max. boards */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */ |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 154 | #define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD) |
| 155 | #define MXSER_ISR_PASS_LIMIT 100 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | #define WAKEUP_CHARS 256 |
| 158 | |
Jiri Slaby | a6970c3 | 2021-06-18 08:14:41 +0200 | [diff] [blame] | 159 | #define MXSER_BAUD_BASE 921600 |
Jiri Slaby | d811b26 | 2021-06-18 08:14:42 +0200 | [diff] [blame] | 160 | #define MXSER_CUSTOM_DIVISOR (MXSER_BAUD_BASE * 16) |
Jiri Slaby | a6970c3 | 2021-06-18 08:14:41 +0200 | [diff] [blame] | 161 | |
Jiri Slaby | e129def | 2008-07-22 11:20:34 +0100 | [diff] [blame] | 162 | #define PCI_DEVICE_ID_POS104UL 0x1044 |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 163 | #define PCI_DEVICE_ID_CB108 0x1080 |
Jiri Slaby | e129def | 2008-07-22 11:20:34 +0100 | [diff] [blame] | 164 | #define PCI_DEVICE_ID_CP102UF 0x1023 |
Jiri Slaby | 502f295 | 2009-09-10 12:20:07 +0200 | [diff] [blame] | 165 | #define PCI_DEVICE_ID_CP112UL 0x1120 |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 166 | #define PCI_DEVICE_ID_CB114 0x1142 |
Jiri Slaby | 80ff8a8 | 2008-02-07 00:16:51 -0800 | [diff] [blame] | 167 | #define PCI_DEVICE_ID_CP114UL 0x1143 |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 168 | #define PCI_DEVICE_ID_CB134I 0x1341 |
| 169 | #define PCI_DEVICE_ID_CP138U 0x1380 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 171 | #define MXSER_NPORTS(ddata) ((ddata) & 0xffU) |
| 172 | #define MXSER_HIGHBAUD 0x0100 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Jiri Slaby | e455836 | 2021-06-18 08:14:14 +0200 | [diff] [blame] | 174 | enum mxser_must_hwid { |
| 175 | MOXA_OTHER_UART = 0x00, |
| 176 | MOXA_MUST_MU150_HWID = 0x01, |
| 177 | MOXA_MUST_MU860_HWID = 0x02, |
| 178 | }; |
| 179 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 180 | static const struct { |
Jiri Slaby | dc33f64 | 2021-06-18 08:14:11 +0200 | [diff] [blame] | 181 | u8 type; |
| 182 | u8 fifo_size; |
| 183 | u8 rx_high_water; |
| 184 | u8 rx_low_water; |
| 185 | speed_t max_baud; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 186 | } Gpci_uart_info[] = { |
Jiri Slaby | dc33f64 | 2021-06-18 08:14:11 +0200 | [diff] [blame] | 187 | { MOXA_OTHER_UART, 16, 14, 1, 921600 }, |
| 188 | { MOXA_MUST_MU150_HWID, 64, 48, 16, 230400 }, |
| 189 | { MOXA_MUST_MU860_HWID, 128, 96, 32, 921600 } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | }; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 191 | #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 193 | |
| 194 | /* driver_data correspond to the lines in the structure above |
| 195 | see also ISA probe function before you change something */ |
Arvind Yadav | 3385ecf | 2017-07-23 15:31:01 +0530 | [diff] [blame] | 196 | static const struct pci_device_id mxser_pcibrds[] = { |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 197 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 8 }, |
| 198 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 }, |
Jiri Slaby | 1525490 | 2021-06-18 08:14:08 +0200 | [diff] [blame] | 199 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 2 }, |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 200 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 4 }, |
Jiri Slaby | 1525490 | 2021-06-18 08:14:08 +0200 | [diff] [blame] | 201 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 4 }, |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 202 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 2 | MXSER_HIGHBAUD }, |
| 203 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 4 }, |
| 204 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 8 }, |
| 205 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 2 }, |
| 206 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 4 }, |
| 207 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 4 }, |
| 208 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 8 }, /* RC7000 */ |
| 209 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 8 }, |
| 210 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 2 }, |
| 211 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 2 }, |
| 212 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 8 }, |
| 213 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 8 }, |
| 214 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 4 }, |
| 215 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 8 }, |
| 216 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 4 }, |
| 217 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 4 }, |
| 218 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 8 }, |
| 219 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 4 }, |
| 220 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 4 }, |
| 221 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 2 }, |
| 222 | { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP112UL), .driver_data = 2 }, |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 223 | { } |
| 224 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | MODULE_DEVICE_TABLE(pci, mxser_pcibrds); |
| 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | static int ttymajor = MXSERMAJOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
| 229 | /* Variables for insmod */ |
| 230 | |
| 231 | MODULE_AUTHOR("Casper Yang"); |
| 232 | MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver"); |
Rusty Russell | 8d3b33f | 2006-03-25 03:07:05 -0800 | [diff] [blame] | 233 | module_param(ttymajor, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | MODULE_LICENSE("GPL"); |
| 235 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 236 | struct mxser_board; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 238 | struct mxser_port { |
Alan Cox | 0ad9e7d | 2008-07-16 21:56:10 +0100 | [diff] [blame] | 239 | struct tty_port port; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 240 | struct mxser_board *board; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 241 | |
| 242 | unsigned long ioaddr; |
| 243 | unsigned long opmode_ioaddr; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 244 | |
Jiri Slaby | dc33f64 | 2021-06-18 08:14:11 +0200 | [diff] [blame] | 245 | u8 rx_high_water; |
| 246 | u8 rx_low_water; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | int type; /* UART type */ |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 248 | |
Jiri Slaby | a93963e | 2021-06-18 08:14:24 +0200 | [diff] [blame] | 249 | unsigned char x_char; /* xon/xoff character */ |
| 250 | u8 IER; /* Interrupt Enable Register */ |
| 251 | u8 MCR; /* Modem control register */ |
Jiri Slaby | d249e66 | 2021-09-22 09:59:38 +0200 | [diff] [blame] | 252 | u8 FCR; /* FIFO control register */ |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 253 | |
Jiri Slaby | 1923628 | 2021-09-22 09:59:34 +0200 | [diff] [blame] | 254 | bool ldisc_stop_rx; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 255 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 256 | struct async_icount icount; /* kernel counters for 4 input interrupts */ |
Jiri Slaby | 104583b | 2017-09-12 12:39:54 +0200 | [diff] [blame] | 257 | unsigned int timeout; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 258 | |
Jiri Slaby | a93963e | 2021-06-18 08:14:24 +0200 | [diff] [blame] | 259 | u8 read_status_mask; |
| 260 | u8 ignore_status_mask; |
Jiri Slaby | dc33f64 | 2021-06-18 08:14:11 +0200 | [diff] [blame] | 261 | u8 xmit_fifo_size; |
Jiri Slaby | 02e4314 | 2021-06-18 08:14:25 +0200 | [diff] [blame] | 262 | unsigned int xmit_head; |
| 263 | unsigned int xmit_tail; |
| 264 | unsigned int xmit_cnt; |
Peter Hurley | cd7b4b3 | 2016-01-10 14:51:38 -0800 | [diff] [blame] | 265 | int closing; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | spinlock_t slock; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | struct mxser_board { |
| 271 | unsigned int idx; |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 272 | unsigned short nports; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 273 | int irq; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 274 | unsigned long vector; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 275 | |
Jiri Slaby | e455836 | 2021-06-18 08:14:14 +0200 | [diff] [blame] | 276 | enum mxser_must_hwid must_hwid; |
Jiri Slaby | 928f946 | 2021-06-18 08:14:16 +0200 | [diff] [blame] | 277 | speed_t max_baud; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 278 | |
Jiri Slaby | ad1c92f | 2021-06-18 08:14:59 +0200 | [diff] [blame] | 279 | struct mxser_port ports[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | }; |
| 281 | |
Jiri Slaby | f8b6b32 | 2021-06-18 08:14:58 +0200 | [diff] [blame] | 282 | static DECLARE_BITMAP(mxser_boards, MXSER_BOARDS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | static struct tty_driver *mxvar_sdriver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
Jiri Slaby | edb7d27 | 2021-06-18 08:14:46 +0200 | [diff] [blame] | 285 | static u8 __mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set, |
| 286 | bool restore_LCR) |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 287 | { |
Jiri Slaby | edb7d27 | 2021-06-18 08:14:46 +0200 | [diff] [blame] | 288 | u8 oldlcr, efr; |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 289 | |
| 290 | oldlcr = inb(baseio + UART_LCR); |
Jiri Slaby | 464fbf6 | 2021-06-18 08:15:08 +0200 | [diff] [blame] | 291 | outb(MOXA_MUST_ENTER_ENHANCED, baseio + UART_LCR); |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 292 | |
| 293 | efr = inb(baseio + MOXA_MUST_EFR_REGISTER); |
Jiri Slaby | edb7d27 | 2021-06-18 08:14:46 +0200 | [diff] [blame] | 294 | efr &= ~clear; |
| 295 | efr |= set; |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 296 | |
| 297 | outb(efr, baseio + MOXA_MUST_EFR_REGISTER); |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 298 | |
Jiri Slaby | edb7d27 | 2021-06-18 08:14:46 +0200 | [diff] [blame] | 299 | if (restore_LCR) |
| 300 | outb(oldlcr, baseio + UART_LCR); |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 301 | |
Jiri Slaby | edb7d27 | 2021-06-18 08:14:46 +0200 | [diff] [blame] | 302 | return oldlcr; |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Jiri Slaby | b286484 | 2021-06-18 08:14:47 +0200 | [diff] [blame] | 305 | static u8 mxser_must_select_bank(unsigned long baseio, u8 bank) |
| 306 | { |
| 307 | return __mxser_must_set_EFR(baseio, MOXA_MUST_EFR_BANK_MASK, bank, |
| 308 | false); |
| 309 | } |
| 310 | |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 311 | static void mxser_set_must_xon1_value(unsigned long baseio, u8 value) |
| 312 | { |
Jiri Slaby | b286484 | 2021-06-18 08:14:47 +0200 | [diff] [blame] | 313 | u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK0); |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 314 | outb(value, baseio + MOXA_MUST_XON1_REGISTER); |
| 315 | outb(oldlcr, baseio + UART_LCR); |
| 316 | } |
| 317 | |
| 318 | static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value) |
| 319 | { |
Jiri Slaby | b286484 | 2021-06-18 08:14:47 +0200 | [diff] [blame] | 320 | u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK0); |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 321 | outb(value, baseio + MOXA_MUST_XOFF1_REGISTER); |
| 322 | outb(oldlcr, baseio + UART_LCR); |
| 323 | } |
| 324 | |
| 325 | static void mxser_set_must_fifo_value(struct mxser_port *info) |
| 326 | { |
Jiri Slaby | b286484 | 2021-06-18 08:14:47 +0200 | [diff] [blame] | 327 | u8 oldlcr = mxser_must_select_bank(info->ioaddr, MOXA_MUST_EFR_BANK1); |
Jiri Slaby | dc33f64 | 2021-06-18 08:14:11 +0200 | [diff] [blame] | 328 | outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER); |
| 329 | outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTI_REGISTER); |
| 330 | outb(info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER); |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 331 | outb(oldlcr, info->ioaddr + UART_LCR); |
| 332 | } |
| 333 | |
| 334 | static void mxser_set_must_enum_value(unsigned long baseio, u8 value) |
| 335 | { |
Jiri Slaby | b286484 | 2021-06-18 08:14:47 +0200 | [diff] [blame] | 336 | u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK2); |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 337 | outb(value, baseio + MOXA_MUST_ENUM_REGISTER); |
| 338 | outb(oldlcr, baseio + UART_LCR); |
| 339 | } |
| 340 | |
Jiri Slaby | b286484 | 2021-06-18 08:14:47 +0200 | [diff] [blame] | 341 | static u8 mxser_get_must_hardware_id(unsigned long baseio) |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 342 | { |
Jiri Slaby | b286484 | 2021-06-18 08:14:47 +0200 | [diff] [blame] | 343 | u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK2); |
| 344 | u8 id = inb(baseio + MOXA_MUST_HWID_REGISTER); |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 345 | outb(oldlcr, baseio + UART_LCR); |
Jiri Slaby | b286484 | 2021-06-18 08:14:47 +0200 | [diff] [blame] | 346 | |
| 347 | return id; |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Jiri Slaby | edb7d27 | 2021-06-18 08:14:46 +0200 | [diff] [blame] | 350 | static void mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set) |
| 351 | { |
| 352 | __mxser_must_set_EFR(baseio, clear, set, true); |
| 353 | } |
| 354 | |
| 355 | static void mxser_must_set_enhance_mode(unsigned long baseio, bool enable) |
| 356 | { |
| 357 | mxser_must_set_EFR(baseio, |
| 358 | enable ? 0 : MOXA_MUST_EFR_EFRB_ENABLE, |
| 359 | enable ? MOXA_MUST_EFR_EFRB_ENABLE : 0); |
| 360 | } |
| 361 | |
Jiri Slaby | b441eb0 | 2021-06-18 08:14:48 +0200 | [diff] [blame] | 362 | static void mxser_must_no_sw_flow_control(unsigned long baseio) |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 363 | { |
Jiri Slaby | b441eb0 | 2021-06-18 08:14:48 +0200 | [diff] [blame] | 364 | mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_MASK, 0); |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Jiri Slaby | b441eb0 | 2021-06-18 08:14:48 +0200 | [diff] [blame] | 367 | static void mxser_must_set_tx_sw_flow_control(unsigned long baseio, bool enable) |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 368 | { |
Jiri Slaby | b441eb0 | 2021-06-18 08:14:48 +0200 | [diff] [blame] | 369 | mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_TX_MASK, |
| 370 | enable ? MOXA_MUST_EFR_SF_TX1 : 0); |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Jiri Slaby | b441eb0 | 2021-06-18 08:14:48 +0200 | [diff] [blame] | 373 | static void mxser_must_set_rx_sw_flow_control(unsigned long baseio, bool enable) |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 374 | { |
Jiri Slaby | b441eb0 | 2021-06-18 08:14:48 +0200 | [diff] [blame] | 375 | mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_RX_MASK, |
| 376 | enable ? MOXA_MUST_EFR_SF_RX1 : 0); |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Jiri Slaby | e455836 | 2021-06-18 08:14:14 +0200 | [diff] [blame] | 379 | static enum mxser_must_hwid mxser_must_get_hwid(unsigned long io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | { |
| 381 | u8 oldmcr, hwid; |
| 382 | int i; |
| 383 | |
| 384 | outb(0, io + UART_LCR); |
Jiri Slaby | edb7d27 | 2021-06-18 08:14:46 +0200 | [diff] [blame] | 385 | mxser_must_set_enhance_mode(io, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | oldmcr = inb(io + UART_MCR); |
| 387 | outb(0, io + UART_MCR); |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 388 | mxser_set_must_xon1_value(io, 0x11); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | if ((hwid = inb(io + UART_MCR)) != 0) { |
| 390 | outb(oldmcr, io + UART_MCR); |
Jesper Juhl | 8ea2c2e | 2006-06-25 05:47:54 -0700 | [diff] [blame] | 391 | return MOXA_OTHER_UART; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Jiri Slaby | b286484 | 2021-06-18 08:14:47 +0200 | [diff] [blame] | 394 | hwid = mxser_get_must_hardware_id(io); |
Jiri Slaby | e455836 | 2021-06-18 08:14:14 +0200 | [diff] [blame] | 395 | for (i = 1; i < UART_INFO_NUM; i++) /* 0 = OTHER_UART */ |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 396 | if (hwid == Gpci_uart_info[i].type) |
Jiri Slaby | e455836 | 2021-06-18 08:14:14 +0200 | [diff] [blame] | 397 | return hwid; |
| 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | return MOXA_OTHER_UART; |
| 400 | } |
| 401 | |
Jiri Slaby | 5d1ea1a | 2021-06-18 08:15:12 +0200 | [diff] [blame] | 402 | static bool mxser_16550A_or_MUST(struct mxser_port *info) |
| 403 | { |
| 404 | return info->type == PORT_16550A || info->board->must_hwid; |
| 405 | } |
| 406 | |
Jiri Slaby | c3db20c | 2021-06-18 08:14:55 +0200 | [diff] [blame] | 407 | static void mxser_process_txrx_fifo(struct mxser_port *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | { |
Jiri Slaby | c3db20c | 2021-06-18 08:14:55 +0200 | [diff] [blame] | 409 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Jiri Slaby | c3db20c | 2021-06-18 08:14:55 +0200 | [diff] [blame] | 411 | if (info->type == PORT_16450 || info->type == PORT_8250) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | info->rx_high_water = 1; |
| 413 | info->rx_low_water = 1; |
| 414 | info->xmit_fifo_size = 1; |
Jiri Slaby | c3db20c | 2021-06-18 08:14:55 +0200 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | |
| 418 | for (i = 0; i < UART_INFO_NUM; i++) |
| 419 | if (info->board->must_hwid == Gpci_uart_info[i].type) { |
| 420 | info->rx_low_water = Gpci_uart_info[i].rx_low_water; |
| 421 | info->rx_high_water = Gpci_uart_info[i].rx_high_water; |
| 422 | info->xmit_fifo_size = Gpci_uart_info[i].fifo_size; |
| 423 | break; |
| 424 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Jiri Slaby | 740165f | 2021-06-18 08:15:05 +0200 | [diff] [blame] | 427 | static void __mxser_start_tx(struct mxser_port *info) |
| 428 | { |
| 429 | outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER); |
| 430 | info->IER |= UART_IER_THRI; |
| 431 | outb(info->IER, info->ioaddr + UART_IER); |
| 432 | } |
| 433 | |
| 434 | static void mxser_start_tx(struct mxser_port *info) |
| 435 | { |
| 436 | unsigned long flags; |
| 437 | |
| 438 | spin_lock_irqsave(&info->slock, flags); |
| 439 | __mxser_start_tx(info); |
| 440 | spin_unlock_irqrestore(&info->slock, flags); |
| 441 | } |
| 442 | |
| 443 | static void __mxser_stop_tx(struct mxser_port *info) |
| 444 | { |
| 445 | info->IER &= ~UART_IER_THRI; |
| 446 | outb(info->IER, info->ioaddr + UART_IER); |
| 447 | } |
| 448 | |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 449 | static int mxser_carrier_raised(struct tty_port *port) |
| 450 | { |
| 451 | struct mxser_port *mp = container_of(port, struct mxser_port, port); |
| 452 | return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0; |
| 453 | } |
| 454 | |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 455 | static void mxser_dtr_rts(struct tty_port *port, int on) |
Alan Cox | 5d951fb | 2009-01-02 13:45:19 +0000 | [diff] [blame] | 456 | { |
| 457 | struct mxser_port *mp = container_of(port, struct mxser_port, port); |
| 458 | unsigned long flags; |
Jiri Slaby | 007bbdc | 2021-06-18 08:15:02 +0200 | [diff] [blame] | 459 | u8 mcr; |
Alan Cox | 5d951fb | 2009-01-02 13:45:19 +0000 | [diff] [blame] | 460 | |
| 461 | spin_lock_irqsave(&mp->slock, flags); |
Jiri Slaby | 007bbdc | 2021-06-18 08:15:02 +0200 | [diff] [blame] | 462 | mcr = inb(mp->ioaddr + UART_MCR); |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 463 | if (on) |
Jiri Slaby | 007bbdc | 2021-06-18 08:15:02 +0200 | [diff] [blame] | 464 | mcr |= UART_MCR_DTR | UART_MCR_RTS; |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 465 | else |
Jiri Slaby | 007bbdc | 2021-06-18 08:15:02 +0200 | [diff] [blame] | 466 | mcr &= ~(UART_MCR_DTR | UART_MCR_RTS); |
| 467 | outb(mcr, mp->ioaddr + UART_MCR); |
Alan Cox | 5d951fb | 2009-01-02 13:45:19 +0000 | [diff] [blame] | 468 | spin_unlock_irqrestore(&mp->slock, flags); |
| 469 | } |
| 470 | |
Jiri Slaby | dc33f64 | 2021-06-18 08:14:11 +0200 | [diff] [blame] | 471 | static int mxser_set_baud(struct tty_struct *tty, speed_t newspd) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 472 | { |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 473 | struct mxser_port *info = tty->driver_data; |
Jiri Slaby | 104583b | 2017-09-12 12:39:54 +0200 | [diff] [blame] | 474 | unsigned int quot = 0, baud; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 475 | unsigned char cval; |
Jiri Slaby | 104583b | 2017-09-12 12:39:54 +0200 | [diff] [blame] | 476 | u64 timeout; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 477 | |
Jiri Slaby | 928f946 | 2021-06-18 08:14:16 +0200 | [diff] [blame] | 478 | if (newspd > info->board->max_baud) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 479 | return -1; |
| 480 | |
| 481 | if (newspd == 134) { |
Jiri Slaby | a6970c3 | 2021-06-18 08:14:41 +0200 | [diff] [blame] | 482 | quot = 2 * MXSER_BAUD_BASE / 269; |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 483 | tty_encode_baud_rate(tty, 134, 134); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 484 | } else if (newspd) { |
Jiri Slaby | a6970c3 | 2021-06-18 08:14:41 +0200 | [diff] [blame] | 485 | quot = MXSER_BAUD_BASE / newspd; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 486 | if (quot == 0) |
| 487 | quot = 1; |
Jiri Slaby | a6970c3 | 2021-06-18 08:14:41 +0200 | [diff] [blame] | 488 | baud = MXSER_BAUD_BASE / quot; |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 489 | tty_encode_baud_rate(tty, baud, baud); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 490 | } else { |
| 491 | quot = 0; |
| 492 | } |
| 493 | |
Jiri Slaby | 104583b | 2017-09-12 12:39:54 +0200 | [diff] [blame] | 494 | /* |
| 495 | * worst case (128 * 1000 * 10 * 18432) needs 35 bits, so divide in the |
| 496 | * u64 domain |
| 497 | */ |
| 498 | timeout = (u64)info->xmit_fifo_size * HZ * 10 * quot; |
Jiri Slaby | a6970c3 | 2021-06-18 08:14:41 +0200 | [diff] [blame] | 499 | do_div(timeout, MXSER_BAUD_BASE); |
Jiri Slaby | 104583b | 2017-09-12 12:39:54 +0200 | [diff] [blame] | 500 | info->timeout = timeout + HZ / 50; /* Add .02 seconds of slop */ |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 501 | |
| 502 | if (quot) { |
| 503 | info->MCR |= UART_MCR_DTR; |
| 504 | outb(info->MCR, info->ioaddr + UART_MCR); |
| 505 | } else { |
| 506 | info->MCR &= ~UART_MCR_DTR; |
| 507 | outb(info->MCR, info->ioaddr + UART_MCR); |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | cval = inb(info->ioaddr + UART_LCR); |
| 512 | |
| 513 | outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */ |
| 514 | |
| 515 | outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */ |
| 516 | outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */ |
| 517 | outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */ |
| 518 | |
| 519 | #ifdef BOTHER |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 520 | if (C_BAUD(tty) == BOTHER) { |
Jiri Slaby | a6970c3 | 2021-06-18 08:14:41 +0200 | [diff] [blame] | 521 | quot = MXSER_BAUD_BASE % newspd; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 522 | quot *= 8; |
| 523 | if (quot % newspd > newspd / 2) { |
| 524 | quot /= newspd; |
| 525 | quot++; |
| 526 | } else |
| 527 | quot /= newspd; |
| 528 | |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 529 | mxser_set_must_enum_value(info->ioaddr, quot); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 530 | } else |
| 531 | #endif |
Christoph Hellwig | 148ff86 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 532 | mxser_set_must_enum_value(info->ioaddr, 0); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
Jiri Slaby | be48666 | 2021-06-18 08:15:04 +0200 | [diff] [blame] | 537 | static void mxser_handle_cts(struct tty_struct *tty, struct mxser_port *info, |
| 538 | u8 msr) |
| 539 | { |
| 540 | bool cts = msr & UART_MSR_CTS; |
| 541 | |
| 542 | if (tty->hw_stopped) { |
| 543 | if (cts) { |
| 544 | tty->hw_stopped = 0; |
| 545 | |
Jiri Slaby | 5d1ea1a | 2021-06-18 08:15:12 +0200 | [diff] [blame] | 546 | if (!mxser_16550A_or_MUST(info)) |
Jiri Slaby | 740165f | 2021-06-18 08:15:05 +0200 | [diff] [blame] | 547 | __mxser_start_tx(info); |
Jiri Slaby | be48666 | 2021-06-18 08:15:04 +0200 | [diff] [blame] | 548 | tty_wakeup(tty); |
| 549 | } |
| 550 | return; |
| 551 | } else if (cts) |
| 552 | return; |
| 553 | |
| 554 | tty->hw_stopped = 1; |
Jiri Slaby | 5d1ea1a | 2021-06-18 08:15:12 +0200 | [diff] [blame] | 555 | if (!mxser_16550A_or_MUST(info)) |
Jiri Slaby | 740165f | 2021-06-18 08:15:05 +0200 | [diff] [blame] | 556 | __mxser_stop_tx(info); |
Jiri Slaby | be48666 | 2021-06-18 08:15:04 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 559 | /* |
| 560 | * This routine is called to set the UART divisor registers to match |
| 561 | * the specified baud rate for a serial port. |
| 562 | */ |
Jiri Slaby | 3fdfa16 | 2021-09-22 09:59:32 +0200 | [diff] [blame] | 563 | static void mxser_change_speed(struct tty_struct *tty, struct ktermios *old_termios) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 564 | { |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 565 | struct mxser_port *info = tty->driver_data; |
Jiri Slaby | d249e66 | 2021-09-22 09:59:38 +0200 | [diff] [blame] | 566 | unsigned cflag, cval; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 567 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 568 | cflag = tty->termios.c_cflag; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 569 | |
Jiri Slaby | 3fdfa16 | 2021-09-22 09:59:32 +0200 | [diff] [blame] | 570 | if (mxser_set_baud(tty, tty_get_baud_rate(tty))) { |
| 571 | /* Use previous rate on a failure */ |
| 572 | if (old_termios) { |
| 573 | speed_t baud = tty_termios_baud_rate(old_termios); |
| 574 | tty_encode_baud_rate(tty, baud, baud); |
| 575 | } |
| 576 | } |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 577 | |
| 578 | /* byte size and parity */ |
| 579 | switch (cflag & CSIZE) { |
Jiri Slaby | 2c21832 | 2021-06-18 08:14:43 +0200 | [diff] [blame] | 580 | default: |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 581 | case CS5: |
Jiri Slaby | 2c21832 | 2021-06-18 08:14:43 +0200 | [diff] [blame] | 582 | cval = UART_LCR_WLEN5; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 583 | break; |
| 584 | case CS6: |
Jiri Slaby | 2c21832 | 2021-06-18 08:14:43 +0200 | [diff] [blame] | 585 | cval = UART_LCR_WLEN6; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 586 | break; |
| 587 | case CS7: |
Jiri Slaby | 2c21832 | 2021-06-18 08:14:43 +0200 | [diff] [blame] | 588 | cval = UART_LCR_WLEN7; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 589 | break; |
| 590 | case CS8: |
Jiri Slaby | 2c21832 | 2021-06-18 08:14:43 +0200 | [diff] [blame] | 591 | cval = UART_LCR_WLEN8; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 592 | break; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 593 | } |
Jiri Slaby | 2c21832 | 2021-06-18 08:14:43 +0200 | [diff] [blame] | 594 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 595 | if (cflag & CSTOPB) |
Jiri Slaby | 2c21832 | 2021-06-18 08:14:43 +0200 | [diff] [blame] | 596 | cval |= UART_LCR_STOP; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 597 | if (cflag & PARENB) |
| 598 | cval |= UART_LCR_PARITY; |
| 599 | if (!(cflag & PARODD)) |
| 600 | cval |= UART_LCR_EPAR; |
| 601 | if (cflag & CMSPAR) |
| 602 | cval |= UART_LCR_SPAR; |
| 603 | |
Jiri Slaby | d249e66 | 2021-09-22 09:59:38 +0200 | [diff] [blame] | 604 | info->FCR = 0; |
Jiri Slaby | bf1434c | 2021-09-22 09:59:35 +0200 | [diff] [blame] | 605 | if (info->board->must_hwid) { |
Jiri Slaby | d249e66 | 2021-09-22 09:59:38 +0200 | [diff] [blame] | 606 | info->FCR |= UART_FCR_ENABLE_FIFO | |
Jiri Slaby | bf1434c | 2021-09-22 09:59:35 +0200 | [diff] [blame] | 607 | MOXA_MUST_FCR_GDA_MODE_ENABLE; |
| 608 | mxser_set_must_fifo_value(info); |
| 609 | } else if (info->type != PORT_8250 && info->type != PORT_16450) { |
Jiri Slaby | d249e66 | 2021-09-22 09:59:38 +0200 | [diff] [blame] | 610 | info->FCR |= UART_FCR_ENABLE_FIFO; |
Jiri Slaby | bf1434c | 2021-09-22 09:59:35 +0200 | [diff] [blame] | 611 | switch (info->rx_high_water) { |
| 612 | case 1: |
Jiri Slaby | d249e66 | 2021-09-22 09:59:38 +0200 | [diff] [blame] | 613 | info->FCR |= UART_FCR_TRIGGER_1; |
Jiri Slaby | bf1434c | 2021-09-22 09:59:35 +0200 | [diff] [blame] | 614 | break; |
| 615 | case 4: |
Jiri Slaby | d249e66 | 2021-09-22 09:59:38 +0200 | [diff] [blame] | 616 | info->FCR |= UART_FCR_TRIGGER_4; |
Jiri Slaby | bf1434c | 2021-09-22 09:59:35 +0200 | [diff] [blame] | 617 | break; |
| 618 | case 8: |
Jiri Slaby | d249e66 | 2021-09-22 09:59:38 +0200 | [diff] [blame] | 619 | info->FCR |= UART_FCR_TRIGGER_8; |
Jiri Slaby | bf1434c | 2021-09-22 09:59:35 +0200 | [diff] [blame] | 620 | break; |
| 621 | default: |
Jiri Slaby | d249e66 | 2021-09-22 09:59:38 +0200 | [diff] [blame] | 622 | info->FCR |= UART_FCR_TRIGGER_14; |
Jiri Slaby | bf1434c | 2021-09-22 09:59:35 +0200 | [diff] [blame] | 623 | break; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | /* CTS flow control flag and modem status interrupts */ |
| 628 | info->IER &= ~UART_IER_MSI; |
| 629 | info->MCR &= ~UART_MCR_AFE; |
Peter Hurley | 5604a98 | 2016-04-09 17:53:21 -0700 | [diff] [blame] | 630 | tty_port_set_cts_flow(&info->port, cflag & CRTSCTS); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 631 | if (cflag & CRTSCTS) { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 632 | info->IER |= UART_IER_MSI; |
Jiri Slaby | 5d1ea1a | 2021-06-18 08:15:12 +0200 | [diff] [blame] | 633 | if (mxser_16550A_or_MUST(info)) { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 634 | info->MCR |= UART_MCR_AFE; |
| 635 | } else { |
Jiri Slaby | be48666 | 2021-06-18 08:15:04 +0200 | [diff] [blame] | 636 | mxser_handle_cts(tty, info, |
| 637 | inb(info->ioaddr + UART_MSR)); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 638 | } |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 639 | } |
| 640 | outb(info->MCR, info->ioaddr + UART_MCR); |
Peter Hurley | 2d68655 | 2016-04-09 17:53:23 -0700 | [diff] [blame] | 641 | tty_port_set_check_carrier(&info->port, ~cflag & CLOCAL); |
| 642 | if (~cflag & CLOCAL) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 643 | info->IER |= UART_IER_MSI; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 644 | outb(info->IER, info->ioaddr + UART_IER); |
| 645 | |
| 646 | /* |
| 647 | * Set up parity check flag |
| 648 | */ |
| 649 | info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR; |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 650 | if (I_INPCK(tty)) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 651 | info->read_status_mask |= UART_LSR_FE | UART_LSR_PE; |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 652 | if (I_BRKINT(tty) || I_PARMRK(tty)) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 653 | info->read_status_mask |= UART_LSR_BI; |
| 654 | |
| 655 | info->ignore_status_mask = 0; |
| 656 | |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 657 | if (I_IGNBRK(tty)) { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 658 | info->ignore_status_mask |= UART_LSR_BI; |
| 659 | info->read_status_mask |= UART_LSR_BI; |
| 660 | /* |
| 661 | * If we're ignore parity and break indicators, ignore |
| 662 | * overruns too. (For real raw support). |
| 663 | */ |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 664 | if (I_IGNPAR(tty)) { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 665 | info->ignore_status_mask |= |
| 666 | UART_LSR_OE | |
| 667 | UART_LSR_PE | |
| 668 | UART_LSR_FE; |
| 669 | info->read_status_mask |= |
| 670 | UART_LSR_OE | |
| 671 | UART_LSR_PE | |
| 672 | UART_LSR_FE; |
| 673 | } |
| 674 | } |
Jiri Slaby | 292955a | 2021-06-18 08:14:13 +0200 | [diff] [blame] | 675 | if (info->board->must_hwid) { |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 676 | mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty)); |
| 677 | mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty)); |
Jiri Slaby | b441eb0 | 2021-06-18 08:14:48 +0200 | [diff] [blame] | 678 | mxser_must_set_rx_sw_flow_control(info->ioaddr, I_IXON(tty)); |
| 679 | mxser_must_set_tx_sw_flow_control(info->ioaddr, I_IXOFF(tty)); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | |
Jiri Slaby | d249e66 | 2021-09-22 09:59:38 +0200 | [diff] [blame] | 683 | outb(info->FCR, info->ioaddr + UART_FCR); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 684 | outb(cval, info->ioaddr + UART_LCR); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 685 | } |
| 686 | |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 687 | static void mxser_check_modem_status(struct tty_struct *tty, |
| 688 | struct mxser_port *port, int status) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 689 | { |
| 690 | /* update input line counters */ |
| 691 | if (status & UART_MSR_TERI) |
| 692 | port->icount.rng++; |
| 693 | if (status & UART_MSR_DDSR) |
| 694 | port->icount.dsr++; |
| 695 | if (status & UART_MSR_DDCD) |
| 696 | port->icount.dcd++; |
| 697 | if (status & UART_MSR_DCTS) |
| 698 | port->icount.cts++; |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 699 | wake_up_interruptible(&port->port.delta_msr_wait); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 700 | |
Peter Hurley | 2d68655 | 2016-04-09 17:53:23 -0700 | [diff] [blame] | 701 | if (tty_port_check_carrier(&port->port) && (status & UART_MSR_DDCD)) { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 702 | if (status & UART_MSR_DCD) |
Alan Cox | 0ad9e7d | 2008-07-16 21:56:10 +0100 | [diff] [blame] | 703 | wake_up_interruptible(&port->port.open_wait); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 704 | } |
| 705 | |
Jiri Slaby | be48666 | 2021-06-18 08:15:04 +0200 | [diff] [blame] | 706 | if (tty_port_cts_enabled(&port->port)) |
| 707 | mxser_handle_cts(tty, port, status); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 708 | } |
| 709 | |
Jiri Slaby | ee7e5e6 | 2021-09-22 09:59:36 +0200 | [diff] [blame] | 710 | static void mxser_disable_and_clear_FIFO(struct mxser_port *info) |
| 711 | { |
| 712 | u8 fcr = UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT; |
| 713 | |
| 714 | if (info->board->must_hwid) |
| 715 | fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE; |
| 716 | |
| 717 | outb(fcr, info->ioaddr + UART_FCR); |
| 718 | } |
| 719 | |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 720 | static int mxser_activate(struct tty_port *port, struct tty_struct *tty) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 721 | { |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 722 | struct mxser_port *info = container_of(port, struct mxser_port, port); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 723 | unsigned long page; |
| 724 | unsigned long flags; |
| 725 | |
| 726 | page = __get_free_page(GFP_KERNEL); |
| 727 | if (!page) |
| 728 | return -ENOMEM; |
| 729 | |
| 730 | spin_lock_irqsave(&info->slock, flags); |
| 731 | |
Jiri Slaby | 987a4cf | 2021-06-18 08:15:00 +0200 | [diff] [blame] | 732 | if (!info->type) { |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 733 | set_bit(TTY_IO_ERROR, &tty->flags); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 734 | free_page(page); |
| 735 | spin_unlock_irqrestore(&info->slock, flags); |
| 736 | return 0; |
| 737 | } |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 738 | info->port.xmit_buf = (unsigned char *) page; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 739 | |
| 740 | /* |
| 741 | * Clear the FIFO buffers and disable them |
| 742 | * (they will be reenabled in mxser_change_speed()) |
| 743 | */ |
Jiri Slaby | ee7e5e6 | 2021-09-22 09:59:36 +0200 | [diff] [blame] | 744 | mxser_disable_and_clear_FIFO(info); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 745 | |
| 746 | /* |
| 747 | * At this point there's no way the LSR could still be 0xFF; |
| 748 | * if it is, then bail out, because there's likely no UART |
| 749 | * here. |
| 750 | */ |
| 751 | if (inb(info->ioaddr + UART_LSR) == 0xff) { |
| 752 | spin_unlock_irqrestore(&info->slock, flags); |
| 753 | if (capable(CAP_SYS_ADMIN)) { |
Jiri Slaby | f43a510d | 2009-12-09 12:34:15 -0800 | [diff] [blame] | 754 | set_bit(TTY_IO_ERROR, &tty->flags); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 755 | return 0; |
| 756 | } else |
| 757 | return -ENODEV; |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * Clear the interrupt registers. |
| 762 | */ |
| 763 | (void) inb(info->ioaddr + UART_LSR); |
| 764 | (void) inb(info->ioaddr + UART_RX); |
| 765 | (void) inb(info->ioaddr + UART_IIR); |
| 766 | (void) inb(info->ioaddr + UART_MSR); |
| 767 | |
| 768 | /* |
| 769 | * Now, initialize the UART |
| 770 | */ |
| 771 | outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */ |
| 772 | info->MCR = UART_MCR_DTR | UART_MCR_RTS; |
| 773 | outb(info->MCR, info->ioaddr + UART_MCR); |
| 774 | |
| 775 | /* |
| 776 | * Finally, enable interrupts |
| 777 | */ |
| 778 | info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; |
| 779 | |
Jiri Slaby | 292955a | 2021-06-18 08:14:13 +0200 | [diff] [blame] | 780 | if (info->board->must_hwid) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 781 | info->IER |= MOXA_MUST_IER_EGDAI; |
| 782 | outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */ |
| 783 | |
| 784 | /* |
| 785 | * And clear the interrupt registers again for luck. |
| 786 | */ |
| 787 | (void) inb(info->ioaddr + UART_LSR); |
| 788 | (void) inb(info->ioaddr + UART_RX); |
| 789 | (void) inb(info->ioaddr + UART_IIR); |
| 790 | (void) inb(info->ioaddr + UART_MSR); |
| 791 | |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 792 | clear_bit(TTY_IO_ERROR, &tty->flags); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 793 | info->xmit_cnt = info->xmit_head = info->xmit_tail = 0; |
| 794 | |
| 795 | /* |
| 796 | * and set the speed of the serial port |
| 797 | */ |
Jiri Slaby | 3fdfa16 | 2021-09-22 09:59:32 +0200 | [diff] [blame] | 798 | mxser_change_speed(tty, NULL); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 799 | spin_unlock_irqrestore(&info->slock, flags); |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | /* |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 805 | * This routine will shutdown a serial port |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 806 | */ |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 807 | static void mxser_shutdown_port(struct tty_port *port) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 808 | { |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 809 | struct mxser_port *info = container_of(port, struct mxser_port, port); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 810 | unsigned long flags; |
| 811 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 812 | spin_lock_irqsave(&info->slock, flags); |
| 813 | |
| 814 | /* |
| 815 | * clear delta_msr_wait queue to avoid mem leaks: we may free the irq |
| 816 | * here so the queue might never be waken up |
| 817 | */ |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 818 | wake_up_interruptible(&info->port.delta_msr_wait); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 819 | |
| 820 | /* |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 821 | * Free the xmit buffer, if necessary |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 822 | */ |
Alan Cox | 0ad9e7d | 2008-07-16 21:56:10 +0100 | [diff] [blame] | 823 | if (info->port.xmit_buf) { |
| 824 | free_page((unsigned long) info->port.xmit_buf); |
| 825 | info->port.xmit_buf = NULL; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | info->IER = 0; |
| 829 | outb(0x00, info->ioaddr + UART_IER); |
| 830 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 831 | /* clear Rx/Tx FIFO's */ |
Jiri Slaby | ee7e5e6 | 2021-09-22 09:59:36 +0200 | [diff] [blame] | 832 | mxser_disable_and_clear_FIFO(info); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 833 | |
| 834 | /* read data port to reset things */ |
| 835 | (void) inb(info->ioaddr + UART_RX); |
| 836 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 837 | |
Jiri Slaby | 292955a | 2021-06-18 08:14:13 +0200 | [diff] [blame] | 838 | if (info->board->must_hwid) |
Jiri Slaby | b441eb0 | 2021-06-18 08:14:48 +0200 | [diff] [blame] | 839 | mxser_must_no_sw_flow_control(info->ioaddr); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 840 | |
| 841 | spin_unlock_irqrestore(&info->slock, flags); |
| 842 | } |
| 843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | /* |
| 845 | * This routine is called whenever a serial port is opened. It |
| 846 | * enables interrupts for a serial port, linking in its async structure into |
| 847 | * the IRQ chain. It also performs the serial-specific |
| 848 | * initialization for the tty structure. |
| 849 | */ |
| 850 | static int mxser_open(struct tty_struct *tty, struct file *filp) |
| 851 | { |
Jiri Slaby | 42ad25f | 2021-06-18 08:14:56 +0200 | [diff] [blame] | 852 | struct tty_port *tport = tty->port; |
| 853 | struct mxser_port *port = container_of(tport, struct mxser_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
Jiri Slaby | 42ad25f | 2021-06-18 08:14:56 +0200 | [diff] [blame] | 855 | tty->driver_data = port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
Jiri Slaby | 42ad25f | 2021-06-18 08:14:56 +0200 | [diff] [blame] | 857 | return tty_port_open(tport, tty, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Alan Cox | 978e595 | 2008-04-30 00:53:59 -0700 | [diff] [blame] | 860 | static void mxser_flush_buffer(struct tty_struct *tty) |
| 861 | { |
| 862 | struct mxser_port *info = tty->driver_data; |
Alan Cox | 978e595 | 2008-04-30 00:53:59 -0700 | [diff] [blame] | 863 | unsigned long flags; |
| 864 | |
| 865 | |
| 866 | spin_lock_irqsave(&info->slock, flags); |
| 867 | info->xmit_cnt = info->xmit_head = info->xmit_tail = 0; |
| 868 | |
Jiri Slaby | d249e66 | 2021-09-22 09:59:38 +0200 | [diff] [blame] | 869 | outb(info->FCR | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT, |
Alan Cox | 978e595 | 2008-04-30 00:53:59 -0700 | [diff] [blame] | 870 | info->ioaddr + UART_FCR); |
Alan Cox | 978e595 | 2008-04-30 00:53:59 -0700 | [diff] [blame] | 871 | |
| 872 | spin_unlock_irqrestore(&info->slock, flags); |
| 873 | |
| 874 | tty_wakeup(tty); |
| 875 | } |
| 876 | |
| 877 | |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 878 | static void mxser_close_port(struct tty_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | { |
Alan Cox | 1e2b025 | 2009-09-19 13:13:21 -0700 | [diff] [blame] | 880 | struct mxser_port *info = container_of(port, struct mxser_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | unsigned long timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | * At this point we stop accepting input. To do this, we |
| 884 | * disable the receive line status interrupts, and tell the |
| 885 | * interrupt driver to stop checking the data ready bit in the |
| 886 | * line status register. |
| 887 | */ |
| 888 | info->IER &= ~UART_IER_RLSI; |
Jiri Slaby | 292955a | 2021-06-18 08:14:13 +0200 | [diff] [blame] | 889 | if (info->board->must_hwid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | info->IER &= ~MOXA_MUST_RECV_ISR; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 891 | |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 892 | outb(info->IER, info->ioaddr + UART_IER); |
| 893 | /* |
| 894 | * Before we drop DTR, make sure the UART transmitter |
| 895 | * has completely drained; this is especially |
| 896 | * important if there is a transmit FIFO! |
| 897 | */ |
| 898 | timeout = jiffies + HZ; |
| 899 | while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) { |
| 900 | schedule_timeout_interruptible(5); |
| 901 | if (time_after(jiffies, timeout)) |
| 902 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | } |
Alan Cox | 1e2b025 | 2009-09-19 13:13:21 -0700 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /* |
| 907 | * This routine is called when the serial port gets closed. First, we |
| 908 | * wait for the last remaining data to be sent. Then, we unlink its |
| 909 | * async structure from the interrupt chain if necessary, and we free |
| 910 | * that IRQ if nothing is left in the chain. |
| 911 | */ |
| 912 | static void mxser_close(struct tty_struct *tty, struct file *filp) |
| 913 | { |
| 914 | struct mxser_port *info = tty->driver_data; |
| 915 | struct tty_port *port = &info->port; |
| 916 | |
Jiri Slaby | 389fc82 | 2021-06-18 08:14:38 +0200 | [diff] [blame] | 917 | if (info == NULL) |
Alan Cox | 1e2b025 | 2009-09-19 13:13:21 -0700 | [diff] [blame] | 918 | return; |
| 919 | if (tty_port_close_start(port, tty, filp) == 0) |
| 920 | return; |
Peter Hurley | cd7b4b3 | 2016-01-10 14:51:38 -0800 | [diff] [blame] | 921 | info->closing = 1; |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 922 | mutex_lock(&port->mutex); |
| 923 | mxser_close_port(port); |
Alan Cox | 1e2b025 | 2009-09-19 13:13:21 -0700 | [diff] [blame] | 924 | mxser_flush_buffer(tty); |
Peter Hurley | d41861c | 2016-04-09 17:53:25 -0700 | [diff] [blame] | 925 | if (tty_port_initialized(port) && C_HUPCL(tty)) |
| 926 | tty_port_lower_dtr_rts(port); |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 927 | mxser_shutdown_port(port); |
Peter Hurley | d41861c | 2016-04-09 17:53:25 -0700 | [diff] [blame] | 928 | tty_port_set_initialized(port, 0); |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 929 | mutex_unlock(&port->mutex); |
Peter Hurley | cd7b4b3 | 2016-01-10 14:51:38 -0800 | [diff] [blame] | 930 | info->closing = 0; |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 931 | /* Right now the tty_port set is done outside of the close_end helper |
| 932 | as we don't yet have everyone using refcounts */ |
| 933 | tty_port_close_end(port, tty); |
| 934 | tty_port_tty_set(port, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 938 | { |
| 939 | int c, total = 0; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 940 | struct mxser_port *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | unsigned long flags; |
| 942 | |
Alan Cox | 0ad9e7d | 2008-07-16 21:56:10 +0100 | [diff] [blame] | 943 | if (!info->port.xmit_buf) |
Jesper Juhl | 8ea2c2e | 2006-06-25 05:47:54 -0700 | [diff] [blame] | 944 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
| 946 | while (1) { |
Jesper Juhl | 8ea2c2e | 2006-06-25 05:47:54 -0700 | [diff] [blame] | 947 | c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1, |
| 948 | SERIAL_XMIT_SIZE - info->xmit_head)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | if (c <= 0) |
| 950 | break; |
| 951 | |
Alan Cox | 0ad9e7d | 2008-07-16 21:56:10 +0100 | [diff] [blame] | 952 | memcpy(info->port.xmit_buf + info->xmit_head, buf, c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | spin_lock_irqsave(&info->slock, flags); |
Jesper Juhl | 8ea2c2e | 2006-06-25 05:47:54 -0700 | [diff] [blame] | 954 | info->xmit_head = (info->xmit_head + c) & |
| 955 | (SERIAL_XMIT_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | info->xmit_cnt += c; |
| 957 | spin_unlock_irqrestore(&info->slock, flags); |
| 958 | |
| 959 | buf += c; |
| 960 | count -= c; |
| 961 | total += c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Jiri Slaby | 5d1ea1a | 2021-06-18 08:15:12 +0200 | [diff] [blame] | 964 | if (info->xmit_cnt && !tty->flow.stopped) |
| 965 | if (!tty->hw_stopped || mxser_16550A_or_MUST(info)) |
Jiri Slaby | 740165f | 2021-06-18 08:15:05 +0200 | [diff] [blame] | 966 | mxser_start_tx(info); |
Jiri Slaby | 5d1ea1a | 2021-06-18 08:15:12 +0200 | [diff] [blame] | 967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | return total; |
| 969 | } |
| 970 | |
Alan Cox | 0be2ead | 2008-04-30 00:54:03 -0700 | [diff] [blame] | 971 | static int mxser_put_char(struct tty_struct *tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 973 | struct mxser_port *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | unsigned long flags; |
| 975 | |
Alan Cox | 0ad9e7d | 2008-07-16 21:56:10 +0100 | [diff] [blame] | 976 | if (!info->port.xmit_buf) |
Alan Cox | 0be2ead | 2008-04-30 00:54:03 -0700 | [diff] [blame] | 977 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | |
| 979 | if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) |
Alan Cox | 0be2ead | 2008-04-30 00:54:03 -0700 | [diff] [blame] | 980 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
| 982 | spin_lock_irqsave(&info->slock, flags); |
Alan Cox | 0ad9e7d | 2008-07-16 21:56:10 +0100 | [diff] [blame] | 983 | info->port.xmit_buf[info->xmit_head++] = ch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | info->xmit_head &= SERIAL_XMIT_SIZE - 1; |
| 985 | info->xmit_cnt++; |
| 986 | spin_unlock_irqrestore(&info->slock, flags); |
Jiri Slaby | 8aff64e | 2021-06-18 08:15:03 +0200 | [diff] [blame] | 987 | |
Alan Cox | 0be2ead | 2008-04-30 00:54:03 -0700 | [diff] [blame] | 988 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | |
| 992 | static void mxser_flush_chars(struct tty_struct *tty) |
| 993 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 994 | struct mxser_port *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
Jiri Slaby | 265ceff | 2021-06-18 08:15:06 +0200 | [diff] [blame] | 996 | if (!info->xmit_cnt || tty->flow.stopped || !info->port.xmit_buf || |
Jiri Slaby | 5d1ea1a | 2021-06-18 08:15:12 +0200 | [diff] [blame] | 997 | (tty->hw_stopped && !mxser_16550A_or_MUST(info))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | return; |
| 999 | |
Jiri Slaby | 740165f | 2021-06-18 08:15:05 +0200 | [diff] [blame] | 1000 | mxser_start_tx(info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Jiri Slaby | 03b3b1a | 2021-05-05 11:19:15 +0200 | [diff] [blame] | 1003 | static unsigned int mxser_write_room(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1005 | struct mxser_port *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | int ret; |
| 1007 | |
| 1008 | ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1; |
Jiri Slaby | ace7dd9 | 2008-07-25 01:48:22 -0700 | [diff] [blame] | 1009 | return ret < 0 ? 0 : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Jiri Slaby | fff4ef1 | 2021-05-05 11:19:19 +0200 | [diff] [blame] | 1012 | static unsigned int mxser_chars_in_buffer(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1014 | struct mxser_port *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | return info->xmit_cnt; |
| 1016 | } |
| 1017 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1018 | /* |
| 1019 | * ------------------------------------------------------------ |
| 1020 | * friends of mxser_ioctl() |
| 1021 | * ------------------------------------------------------------ |
| 1022 | */ |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 1023 | static int mxser_get_serial_info(struct tty_struct *tty, |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1024 | struct serial_struct *ss) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | { |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 1026 | struct mxser_port *info = tty->driver_data; |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1027 | struct tty_port *port = &info->port; |
Johan Hovold | be6cf58 | 2021-04-07 12:23:31 +0200 | [diff] [blame] | 1028 | unsigned int closing_wait, close_delay; |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1029 | |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1030 | mutex_lock(&port->mutex); |
Johan Hovold | be6cf58 | 2021-04-07 12:23:31 +0200 | [diff] [blame] | 1031 | |
| 1032 | close_delay = jiffies_to_msecs(info->port.close_delay) / 10; |
| 1033 | closing_wait = info->port.closing_wait; |
| 1034 | if (closing_wait != ASYNC_CLOSING_WAIT_NONE) |
| 1035 | closing_wait = jiffies_to_msecs(closing_wait) / 10; |
| 1036 | |
Dan Carpenter | 2285c49 | 2021-08-25 10:24:35 +0300 | [diff] [blame] | 1037 | ss->type = info->type; |
| 1038 | ss->line = tty->index; |
| 1039 | ss->port = info->ioaddr; |
| 1040 | ss->irq = info->board->irq; |
| 1041 | ss->flags = info->port.flags; |
| 1042 | ss->baud_base = MXSER_BAUD_BASE; |
Johan Hovold | be6cf58 | 2021-04-07 12:23:31 +0200 | [diff] [blame] | 1043 | ss->close_delay = close_delay; |
| 1044 | ss->closing_wait = closing_wait; |
Jiri Slaby | d811b26 | 2021-06-18 08:14:42 +0200 | [diff] [blame] | 1045 | ss->custom_divisor = MXSER_CUSTOM_DIVISOR, |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1046 | mutex_unlock(&port->mutex); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1047 | return 0; |
| 1048 | } |
| 1049 | |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 1050 | static int mxser_set_serial_info(struct tty_struct *tty, |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1051 | struct serial_struct *ss) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1052 | { |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 1053 | struct mxser_port *info = tty->driver_data; |
Alan Cox | 07f86c0 | 2009-11-30 13:17:41 +0000 | [diff] [blame] | 1054 | struct tty_port *port = &info->port; |
Jiri Slaby | 80ff8a8 | 2008-02-07 00:16:51 -0800 | [diff] [blame] | 1055 | speed_t baud; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1056 | unsigned long sl_flags; |
Jiri Slaby | 06cc52e | 2021-06-18 08:15:11 +0200 | [diff] [blame] | 1057 | unsigned int old_speed, close_delay, closing_wait; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1058 | int retval = 0; |
| 1059 | |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1060 | if (tty_io_error(tty)) |
| 1061 | return -EIO; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1062 | |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1063 | mutex_lock(&port->mutex); |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1064 | |
| 1065 | if (ss->irq != info->board->irq || |
| 1066 | ss->port != info->ioaddr) { |
| 1067 | mutex_unlock(&port->mutex); |
Jiri Slaby | 80ff8a8 | 2008-02-07 00:16:51 -0800 | [diff] [blame] | 1068 | return -EINVAL; |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1069 | } |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1070 | |
Jiri Slaby | 06cc52e | 2021-06-18 08:15:11 +0200 | [diff] [blame] | 1071 | old_speed = port->flags & ASYNC_SPD_MASK; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1072 | |
Johan Hovold | be6cf58 | 2021-04-07 12:23:31 +0200 | [diff] [blame] | 1073 | close_delay = msecs_to_jiffies(ss->close_delay * 10); |
| 1074 | closing_wait = ss->closing_wait; |
| 1075 | if (closing_wait != ASYNC_CLOSING_WAIT_NONE) |
| 1076 | closing_wait = msecs_to_jiffies(closing_wait * 10); |
| 1077 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1078 | if (!capable(CAP_SYS_ADMIN)) { |
Jiri Slaby | a6970c3 | 2021-06-18 08:14:41 +0200 | [diff] [blame] | 1079 | if ((ss->baud_base != MXSER_BAUD_BASE) || |
Jiri Slaby | 1b3086b | 2021-06-18 08:15:10 +0200 | [diff] [blame] | 1080 | (close_delay != port->close_delay) || |
| 1081 | (closing_wait != port->closing_wait) || |
| 1082 | ((ss->flags & ~ASYNC_USR_MASK) != (port->flags & ~ASYNC_USR_MASK))) { |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1083 | mutex_unlock(&port->mutex); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1084 | return -EPERM; |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1085 | } |
Jiri Slaby | 1b3086b | 2021-06-18 08:15:10 +0200 | [diff] [blame] | 1086 | port->flags = (port->flags & ~ASYNC_USR_MASK) | |
| 1087 | (ss->flags & ASYNC_USR_MASK); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1088 | } else { |
| 1089 | /* |
| 1090 | * OK, past this point, all the error checking has been done. |
| 1091 | * At this point, we start making changes..... |
| 1092 | */ |
Alan Cox | 07f86c0 | 2009-11-30 13:17:41 +0000 | [diff] [blame] | 1093 | port->flags = ((port->flags & ~ASYNC_FLAGS) | |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1094 | (ss->flags & ASYNC_FLAGS)); |
Johan Hovold | be6cf58 | 2021-04-07 12:23:31 +0200 | [diff] [blame] | 1095 | port->close_delay = close_delay; |
| 1096 | port->closing_wait = closing_wait; |
Alan Cox | 07f86c0 | 2009-11-30 13:17:41 +0000 | [diff] [blame] | 1097 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST && |
Jiri Slaby | a6970c3 | 2021-06-18 08:14:41 +0200 | [diff] [blame] | 1098 | (ss->baud_base != MXSER_BAUD_BASE || |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1099 | ss->custom_divisor != |
Jiri Slaby | d811b26 | 2021-06-18 08:14:42 +0200 | [diff] [blame] | 1100 | MXSER_CUSTOM_DIVISOR)) { |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1101 | if (ss->custom_divisor == 0) { |
| 1102 | mutex_unlock(&port->mutex); |
Alan Cox | 07f86c0 | 2009-11-30 13:17:41 +0000 | [diff] [blame] | 1103 | return -EINVAL; |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1104 | } |
| 1105 | baud = ss->baud_base / ss->custom_divisor; |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 1106 | tty_encode_baud_rate(tty, baud, baud); |
Jiri Slaby | 80ff8a8 | 2008-02-07 00:16:51 -0800 | [diff] [blame] | 1107 | } |
Johan Hovold | b91cfb2 | 2021-04-07 12:23:32 +0200 | [diff] [blame] | 1108 | |
| 1109 | info->type = ss->type; |
| 1110 | |
Jiri Slaby | c3db20c | 2021-06-18 08:14:55 +0200 | [diff] [blame] | 1111 | mxser_process_txrx_fifo(info); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1112 | } |
| 1113 | |
Peter Hurley | d41861c | 2016-04-09 17:53:25 -0700 | [diff] [blame] | 1114 | if (tty_port_initialized(port)) { |
Jiri Slaby | 06cc52e | 2021-06-18 08:15:11 +0200 | [diff] [blame] | 1115 | if (old_speed != (port->flags & ASYNC_SPD_MASK)) { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1116 | spin_lock_irqsave(&info->slock, sl_flags); |
Jiri Slaby | 3fdfa16 | 2021-09-22 09:59:32 +0200 | [diff] [blame] | 1117 | mxser_change_speed(tty, NULL); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1118 | spin_unlock_irqrestore(&info->slock, sl_flags); |
| 1119 | } |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 1120 | } else { |
Alan Cox | 07f86c0 | 2009-11-30 13:17:41 +0000 | [diff] [blame] | 1121 | retval = mxser_activate(port, tty); |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 1122 | if (retval == 0) |
Peter Hurley | d41861c | 2016-04-09 17:53:25 -0700 | [diff] [blame] | 1123 | tty_port_set_initialized(port, 1); |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 1124 | } |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1125 | mutex_unlock(&port->mutex); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1126 | return retval; |
| 1127 | } |
| 1128 | |
| 1129 | /* |
| 1130 | * mxser_get_lsr_info - get line status register info |
| 1131 | * |
| 1132 | * Purpose: Let user call ioctl() to get info when the UART physically |
| 1133 | * is emptied. On bus types like RS485, the transmitter must |
| 1134 | * release the bus after transmitting. This must be done when |
| 1135 | * the transmit shift register is empty, not be done when the |
| 1136 | * transmit holding register is empty. This functionality |
| 1137 | * allows an RS485 driver to be written in user space. |
| 1138 | */ |
| 1139 | static int mxser_get_lsr_info(struct mxser_port *info, |
| 1140 | unsigned int __user *value) |
| 1141 | { |
| 1142 | unsigned char status; |
| 1143 | unsigned int result; |
| 1144 | unsigned long flags; |
| 1145 | |
| 1146 | spin_lock_irqsave(&info->slock, flags); |
| 1147 | status = inb(info->ioaddr + UART_LSR); |
| 1148 | spin_unlock_irqrestore(&info->slock, flags); |
| 1149 | result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0); |
| 1150 | return put_user(result, value); |
| 1151 | } |
| 1152 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 1153 | static int mxser_tiocmget(struct tty_struct *tty) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1154 | { |
| 1155 | struct mxser_port *info = tty->driver_data; |
| 1156 | unsigned char control, status; |
| 1157 | unsigned long flags; |
| 1158 | |
Peter Hurley | 18900ca | 2016-04-09 17:06:48 -0700 | [diff] [blame] | 1159 | if (tty_io_error(tty)) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1160 | return -EIO; |
| 1161 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1162 | spin_lock_irqsave(&info->slock, flags); |
Jiri Slaby | 202acda | 2021-06-18 08:15:09 +0200 | [diff] [blame] | 1163 | control = info->MCR; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1164 | status = inb(info->ioaddr + UART_MSR); |
| 1165 | if (status & UART_MSR_ANY_DELTA) |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 1166 | mxser_check_modem_status(tty, info, status); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1167 | spin_unlock_irqrestore(&info->slock, flags); |
Jiri Slaby | 202acda | 2021-06-18 08:15:09 +0200 | [diff] [blame] | 1168 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1169 | return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) | |
| 1170 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) | |
| 1171 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) | |
| 1172 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0) | |
| 1173 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) | |
| 1174 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0); |
| 1175 | } |
| 1176 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 1177 | static int mxser_tiocmset(struct tty_struct *tty, |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1178 | unsigned int set, unsigned int clear) |
| 1179 | { |
| 1180 | struct mxser_port *info = tty->driver_data; |
| 1181 | unsigned long flags; |
| 1182 | |
Peter Hurley | 18900ca | 2016-04-09 17:06:48 -0700 | [diff] [blame] | 1183 | if (tty_io_error(tty)) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1184 | return -EIO; |
| 1185 | |
| 1186 | spin_lock_irqsave(&info->slock, flags); |
| 1187 | |
| 1188 | if (set & TIOCM_RTS) |
| 1189 | info->MCR |= UART_MCR_RTS; |
| 1190 | if (set & TIOCM_DTR) |
| 1191 | info->MCR |= UART_MCR_DTR; |
| 1192 | |
| 1193 | if (clear & TIOCM_RTS) |
| 1194 | info->MCR &= ~UART_MCR_RTS; |
| 1195 | if (clear & TIOCM_DTR) |
| 1196 | info->MCR &= ~UART_MCR_DTR; |
| 1197 | |
| 1198 | outb(info->MCR, info->ioaddr + UART_MCR); |
| 1199 | spin_unlock_irqrestore(&info->slock, flags); |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1203 | static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg, |
| 1204 | struct async_icount *cprev) |
| 1205 | { |
| 1206 | struct async_icount cnow; |
| 1207 | unsigned long flags; |
| 1208 | int ret; |
| 1209 | |
| 1210 | spin_lock_irqsave(&info->slock, flags); |
| 1211 | cnow = info->icount; /* atomic copy */ |
| 1212 | spin_unlock_irqrestore(&info->slock, flags); |
| 1213 | |
| 1214 | ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) || |
| 1215 | ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) || |
| 1216 | ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) || |
| 1217 | ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts)); |
| 1218 | |
| 1219 | *cprev = cnow; |
| 1220 | |
| 1221 | return ret; |
| 1222 | } |
| 1223 | |
Jiri Slaby | 9fae5f8 | 2021-06-18 08:14:44 +0200 | [diff] [blame] | 1224 | /* We should likely switch to TIOCGRS485/TIOCSRS485. */ |
| 1225 | static int mxser_ioctl_op_mode(struct mxser_port *port, int index, bool set, |
| 1226 | int __user *u_opmode) |
| 1227 | { |
Jiri Slaby | 9fae5f8 | 2021-06-18 08:14:44 +0200 | [diff] [blame] | 1228 | int opmode, p = index % 4; |
| 1229 | int shiftbit = p * 2; |
Jiri Slaby | 238d117 | 2021-06-18 08:14:45 +0200 | [diff] [blame] | 1230 | u8 val; |
Jiri Slaby | 9fae5f8 | 2021-06-18 08:14:44 +0200 | [diff] [blame] | 1231 | |
| 1232 | if (port->board->must_hwid != MOXA_MUST_MU860_HWID) |
| 1233 | return -EFAULT; |
| 1234 | |
| 1235 | if (set) { |
| 1236 | if (get_user(opmode, u_opmode)) |
| 1237 | return -EFAULT; |
| 1238 | |
Jiri Slaby | 238d117 | 2021-06-18 08:14:45 +0200 | [diff] [blame] | 1239 | if (opmode & ~OP_MODE_MASK) |
| 1240 | return -EINVAL; |
Jiri Slaby | 9fae5f8 | 2021-06-18 08:14:44 +0200 | [diff] [blame] | 1241 | |
| 1242 | spin_lock_irq(&port->slock); |
| 1243 | val = inb(port->opmode_ioaddr); |
Jiri Slaby | 238d117 | 2021-06-18 08:14:45 +0200 | [diff] [blame] | 1244 | val &= ~(OP_MODE_MASK << shiftbit); |
Jiri Slaby | 9fae5f8 | 2021-06-18 08:14:44 +0200 | [diff] [blame] | 1245 | val |= (opmode << shiftbit); |
| 1246 | outb(val, port->opmode_ioaddr); |
| 1247 | spin_unlock_irq(&port->slock); |
Jiri Slaby | 9fae5f8 | 2021-06-18 08:14:44 +0200 | [diff] [blame] | 1248 | |
Jiri Slaby | 238d117 | 2021-06-18 08:14:45 +0200 | [diff] [blame] | 1249 | return 0; |
Jiri Slaby | 9fae5f8 | 2021-06-18 08:14:44 +0200 | [diff] [blame] | 1250 | } |
| 1251 | |
Jiri Slaby | 238d117 | 2021-06-18 08:14:45 +0200 | [diff] [blame] | 1252 | spin_lock_irq(&port->slock); |
| 1253 | opmode = inb(port->opmode_ioaddr) >> shiftbit; |
| 1254 | spin_unlock_irq(&port->slock); |
| 1255 | |
| 1256 | return put_user(opmode & OP_MODE_MASK, u_opmode); |
Jiri Slaby | 9fae5f8 | 2021-06-18 08:14:44 +0200 | [diff] [blame] | 1257 | } |
| 1258 | |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 1259 | static int mxser_ioctl(struct tty_struct *tty, |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1260 | unsigned int cmd, unsigned long arg) |
| 1261 | { |
| 1262 | struct mxser_port *info = tty->driver_data; |
| 1263 | struct async_icount cnow; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | unsigned long flags; |
| 1265 | void __user *argp = (void __user *)arg; |
| 1266 | |
Jiri Slaby | 9fae5f8 | 2021-06-18 08:14:44 +0200 | [diff] [blame] | 1267 | if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) |
| 1268 | return mxser_ioctl_op_mode(info, tty->index, |
| 1269 | cmd == MOXA_SET_OP_MODE, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1271 | if (cmd != TIOCMIWAIT && tty_io_error(tty)) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1272 | return -EIO; |
| 1273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | case TIOCSERGETLSR: /* Get line status register */ |
Alan Cox | 9d6d162 | 2008-04-30 00:53:20 -0700 | [diff] [blame] | 1276 | return mxser_get_lsr_info(info, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | /* |
| 1278 | * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change |
| 1279 | * - mask passed in arg for lines of interest |
| 1280 | * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) |
| 1281 | * Caller should use TIOCGICOUNT to see which one it was |
| 1282 | */ |
Jiri Slaby | fc83815 | 2007-04-23 14:41:04 -0700 | [diff] [blame] | 1283 | case TIOCMIWAIT: |
| 1284 | spin_lock_irqsave(&info->slock, flags); |
| 1285 | cnow = info->icount; /* note the counters on entry */ |
| 1286 | spin_unlock_irqrestore(&info->slock, flags); |
| 1287 | |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1288 | return wait_event_interruptible(info->port.delta_msr_wait, |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1289 | mxser_cflags_changed(info, arg, &cnow)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | default: |
| 1291 | return -ENOIOCTLCMD; |
| 1292 | } |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
Alan Cox | 0587102 | 2010-09-16 18:21:52 +0100 | [diff] [blame] | 1296 | /* |
| 1297 | * Get counter of input serial line interrupts (DCD,RI,DSR,CTS) |
| 1298 | * Return: write counters to the user passed counter struct |
| 1299 | * NB: both 1->0 and 0->1 transitions are counted except for |
| 1300 | * RI where only 0->1 is counted. |
| 1301 | */ |
| 1302 | |
| 1303 | static int mxser_get_icount(struct tty_struct *tty, |
| 1304 | struct serial_icounter_struct *icount) |
| 1305 | |
| 1306 | { |
| 1307 | struct mxser_port *info = tty->driver_data; |
| 1308 | struct async_icount cnow; |
| 1309 | unsigned long flags; |
| 1310 | |
| 1311 | spin_lock_irqsave(&info->slock, flags); |
| 1312 | cnow = info->icount; |
| 1313 | spin_unlock_irqrestore(&info->slock, flags); |
| 1314 | |
| 1315 | icount->frame = cnow.frame; |
| 1316 | icount->brk = cnow.brk; |
| 1317 | icount->overrun = cnow.overrun; |
| 1318 | icount->buf_overrun = cnow.buf_overrun; |
| 1319 | icount->parity = cnow.parity; |
| 1320 | icount->rx = cnow.rx; |
| 1321 | icount->tx = cnow.tx; |
| 1322 | icount->cts = cnow.cts; |
| 1323 | icount->dsr = cnow.dsr; |
| 1324 | icount->rng = cnow.rng; |
| 1325 | icount->dcd = cnow.dcd; |
| 1326 | return 0; |
| 1327 | } |
| 1328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | static void mxser_stoprx(struct tty_struct *tty) |
| 1330 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1331 | struct mxser_port *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | |
Jiri Slaby | 1923628 | 2021-09-22 09:59:34 +0200 | [diff] [blame] | 1333 | info->ldisc_stop_rx = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | if (I_IXOFF(tty)) { |
Jiri Slaby | 292955a | 2021-06-18 08:14:13 +0200 | [diff] [blame] | 1335 | if (info->board->must_hwid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | info->IER &= ~MOXA_MUST_RECV_ISR; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1337 | outb(info->IER, info->ioaddr + UART_IER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | info->x_char = STOP_CHAR(tty); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1340 | outb(0, info->ioaddr + UART_IER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | info->IER |= UART_IER_THRI; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1342 | outb(info->IER, info->ioaddr + UART_IER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | |
Peter Hurley | 9db276f | 2016-01-10 20:36:15 -0800 | [diff] [blame] | 1346 | if (C_CRTSCTS(tty)) { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1347 | info->MCR &= ~UART_MCR_RTS; |
| 1348 | outb(info->MCR, info->ioaddr + UART_MCR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | /* |
| 1353 | * This routine is called by the upper-layer tty layer to signal that |
| 1354 | * incoming characters should be throttled. |
| 1355 | */ |
| 1356 | static void mxser_throttle(struct tty_struct *tty) |
| 1357 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | mxser_stoprx(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | static void mxser_unthrottle(struct tty_struct *tty) |
| 1362 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1363 | struct mxser_port *info = tty->driver_data; |
Jesper Juhl | 8ea2c2e | 2006-06-25 05:47:54 -0700 | [diff] [blame] | 1364 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1365 | /* startrx */ |
Jiri Slaby | 1923628 | 2021-09-22 09:59:34 +0200 | [diff] [blame] | 1366 | info->ldisc_stop_rx = false; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1367 | if (I_IXOFF(tty)) { |
| 1368 | if (info->x_char) |
| 1369 | info->x_char = 0; |
| 1370 | else { |
Jiri Slaby | 292955a | 2021-06-18 08:14:13 +0200 | [diff] [blame] | 1371 | if (info->board->must_hwid) { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1372 | info->IER |= MOXA_MUST_RECV_ISR; |
| 1373 | outb(info->IER, info->ioaddr + UART_IER); |
| 1374 | } else { |
| 1375 | info->x_char = START_CHAR(tty); |
| 1376 | outb(0, info->ioaddr + UART_IER); |
| 1377 | info->IER |= UART_IER_THRI; |
| 1378 | outb(info->IER, info->ioaddr + UART_IER); |
| 1379 | } |
| 1380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
Peter Hurley | 9db276f | 2016-01-10 20:36:15 -0800 | [diff] [blame] | 1383 | if (C_CRTSCTS(tty)) { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1384 | info->MCR |= UART_MCR_RTS; |
| 1385 | outb(info->MCR, info->ioaddr + UART_MCR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | /* |
| 1390 | * mxser_stop() and mxser_start() |
| 1391 | * |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 1392 | * This routines are called before setting or resetting tty->flow.stopped. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | * They enable or disable transmitter interrupts, as necessary. |
| 1394 | */ |
| 1395 | static void mxser_stop(struct tty_struct *tty) |
| 1396 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1397 | struct mxser_port *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | unsigned long flags; |
| 1399 | |
| 1400 | spin_lock_irqsave(&info->slock, flags); |
Jiri Slaby | 740165f | 2021-06-18 08:15:05 +0200 | [diff] [blame] | 1401 | if (info->IER & UART_IER_THRI) |
| 1402 | __mxser_stop_tx(info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | spin_unlock_irqrestore(&info->slock, flags); |
| 1404 | } |
| 1405 | |
| 1406 | static void mxser_start(struct tty_struct *tty) |
| 1407 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1408 | struct mxser_port *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | unsigned long flags; |
| 1410 | |
| 1411 | spin_lock_irqsave(&info->slock, flags); |
Jiri Slaby | 740165f | 2021-06-18 08:15:05 +0200 | [diff] [blame] | 1412 | if (info->xmit_cnt && info->port.xmit_buf) |
| 1413 | __mxser_start_tx(info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | spin_unlock_irqrestore(&info->slock, flags); |
| 1415 | } |
| 1416 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1417 | static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios) |
| 1418 | { |
| 1419 | struct mxser_port *info = tty->driver_data; |
| 1420 | unsigned long flags; |
| 1421 | |
| 1422 | spin_lock_irqsave(&info->slock, flags); |
Jiri Slaby | 3fdfa16 | 2021-09-22 09:59:32 +0200 | [diff] [blame] | 1423 | mxser_change_speed(tty, old_termios); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1424 | spin_unlock_irqrestore(&info->slock, flags); |
| 1425 | |
Peter Hurley | 9db276f | 2016-01-10 20:36:15 -0800 | [diff] [blame] | 1426 | if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1427 | tty->hw_stopped = 0; |
| 1428 | mxser_start(tty); |
| 1429 | } |
| 1430 | |
| 1431 | /* Handle sw stopped */ |
Peter Hurley | 9db276f | 2016-01-10 20:36:15 -0800 | [diff] [blame] | 1432 | if ((old_termios->c_iflag & IXON) && !I_IXON(tty)) { |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 1433 | tty->flow.stopped = 0; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1434 | |
Jiri Slaby | 292955a | 2021-06-18 08:14:13 +0200 | [diff] [blame] | 1435 | if (info->board->must_hwid) { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1436 | spin_lock_irqsave(&info->slock, flags); |
Jiri Slaby | b441eb0 | 2021-06-18 08:14:48 +0200 | [diff] [blame] | 1437 | mxser_must_set_rx_sw_flow_control(info->ioaddr, false); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1438 | spin_unlock_irqrestore(&info->slock, flags); |
| 1439 | } |
| 1440 | |
| 1441 | mxser_start(tty); |
| 1442 | } |
| 1443 | } |
| 1444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | /* |
| 1446 | * mxser_wait_until_sent() --- wait until the transmitter is empty |
| 1447 | */ |
| 1448 | static void mxser_wait_until_sent(struct tty_struct *tty, int timeout) |
| 1449 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1450 | struct mxser_port *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | unsigned long orig_jiffies, char_time; |
Alan Cox | 07f86c0 | 2009-11-30 13:17:41 +0000 | [diff] [blame] | 1452 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | int lsr; |
| 1454 | |
| 1455 | if (info->type == PORT_UNKNOWN) |
| 1456 | return; |
| 1457 | |
| 1458 | if (info->xmit_fifo_size == 0) |
| 1459 | return; /* Just in case.... */ |
| 1460 | |
| 1461 | orig_jiffies = jiffies; |
| 1462 | /* |
| 1463 | * Set the check interval to be 1/5 of the estimated time to |
| 1464 | * send a single character, and make it at least 1. The check |
| 1465 | * interval should also be less than the timeout. |
| 1466 | * |
| 1467 | * Note: we have to use pretty tight timings here to satisfy |
| 1468 | * the NIST-PCTS. |
| 1469 | */ |
| 1470 | char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size; |
| 1471 | char_time = char_time / 5; |
| 1472 | if (char_time == 0) |
| 1473 | char_time = 1; |
| 1474 | if (timeout && timeout < char_time) |
| 1475 | char_time = timeout; |
| 1476 | /* |
| 1477 | * If the transmitter hasn't cleared in twice the approximate |
| 1478 | * amount of time to send the entire FIFO, it probably won't |
| 1479 | * ever clear. This assumes the UART isn't doing flow |
| 1480 | * control, which is currently the case. Hence, if it ever |
| 1481 | * takes longer than info->timeout, this is probably due to a |
| 1482 | * UART bug of some kind. So, we clamp the timeout parameter at |
| 1483 | * 2*info->timeout. |
| 1484 | */ |
| 1485 | if (!timeout || timeout > 2 * info->timeout) |
| 1486 | timeout = 2 * info->timeout; |
Jiri Slaby | 8bab534 | 2011-07-14 14:35:15 +0200 | [diff] [blame] | 1487 | |
Alan Cox | 07f86c0 | 2009-11-30 13:17:41 +0000 | [diff] [blame] | 1488 | spin_lock_irqsave(&info->slock, flags); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1489 | while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) { |
Alan Cox | 07f86c0 | 2009-11-30 13:17:41 +0000 | [diff] [blame] | 1490 | spin_unlock_irqrestore(&info->slock, flags); |
Nishanth Aravamudan | da4cd8d | 2005-09-10 00:27:30 -0700 | [diff] [blame] | 1491 | schedule_timeout_interruptible(char_time); |
Alan Cox | 07f86c0 | 2009-11-30 13:17:41 +0000 | [diff] [blame] | 1492 | spin_lock_irqsave(&info->slock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | if (signal_pending(current)) |
| 1494 | break; |
| 1495 | if (timeout && time_after(jiffies, orig_jiffies + timeout)) |
| 1496 | break; |
| 1497 | } |
Alan Cox | 07f86c0 | 2009-11-30 13:17:41 +0000 | [diff] [blame] | 1498 | spin_unlock_irqrestore(&info->slock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | set_current_state(TASK_RUNNING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | } |
| 1501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | /* |
| 1503 | * This routine is called by tty_hangup() when a hangup is signaled. |
| 1504 | */ |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1505 | static void mxser_hangup(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1507 | struct mxser_port *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | |
| 1509 | mxser_flush_buffer(tty); |
Alan Cox | 3b6826b | 2009-01-02 13:45:58 +0000 | [diff] [blame] | 1510 | tty_port_hangup(&info->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | /* |
| 1514 | * mxser_rs_break() --- routine which turns the break handling on or off |
| 1515 | */ |
Alan Cox | 9e98966c | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 1516 | static int mxser_rs_break(struct tty_struct *tty, int break_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1518 | struct mxser_port *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | unsigned long flags; |
Jiri Slaby | 5990843 | 2021-06-18 08:15:01 +0200 | [diff] [blame] | 1520 | u8 lcr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | |
| 1522 | spin_lock_irqsave(&info->slock, flags); |
Jiri Slaby | 5990843 | 2021-06-18 08:15:01 +0200 | [diff] [blame] | 1523 | lcr = inb(info->ioaddr + UART_LCR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | if (break_state == -1) |
Jiri Slaby | 5990843 | 2021-06-18 08:15:01 +0200 | [diff] [blame] | 1525 | lcr |= UART_LCR_SBC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | else |
Jiri Slaby | 5990843 | 2021-06-18 08:15:01 +0200 | [diff] [blame] | 1527 | lcr &= ~UART_LCR_SBC; |
| 1528 | outb(lcr, info->ioaddr + UART_LCR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | spin_unlock_irqrestore(&info->slock, flags); |
Jiri Slaby | 5990843 | 2021-06-18 08:15:01 +0200 | [diff] [blame] | 1530 | |
Alan Cox | 9e98966c | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 1531 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
Jiri Slaby | e5ce1bc | 2021-06-18 08:14:21 +0200 | [diff] [blame] | 1534 | static bool mxser_receive_chars_new(struct tty_struct *tty, |
Jiri Slaby | 95b3ea4 | 2021-06-18 08:14:37 +0200 | [diff] [blame] | 1535 | struct mxser_port *port, u8 status) |
Jiri Slaby | e5ce1bc | 2021-06-18 08:14:21 +0200 | [diff] [blame] | 1536 | { |
| 1537 | enum mxser_must_hwid hwid = port->board->must_hwid; |
| 1538 | u8 gdl; |
| 1539 | |
| 1540 | if (hwid == MOXA_OTHER_UART) |
| 1541 | return false; |
Jiri Slaby | 7d5006d | 2021-09-22 09:59:33 +0200 | [diff] [blame] | 1542 | if (status & (UART_LSR_BRK_ERROR_BITS | MOXA_MUST_LSR_RERR)) |
Jiri Slaby | e5ce1bc | 2021-06-18 08:14:21 +0200 | [diff] [blame] | 1543 | return false; |
| 1544 | |
| 1545 | gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER); |
| 1546 | if (hwid == MOXA_MUST_MU150_HWID) |
| 1547 | gdl &= MOXA_MUST_GDL_MASK; |
| 1548 | |
| 1549 | if (gdl >= tty->receive_room && !port->ldisc_stop_rx) |
| 1550 | mxser_stoprx(tty); |
| 1551 | |
| 1552 | while (gdl--) { |
| 1553 | u8 ch = inb(port->ioaddr + UART_RX); |
| 1554 | tty_insert_flip_char(&port->port, ch, 0); |
Jiri Slaby | e5ce1bc | 2021-06-18 08:14:21 +0200 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | return true; |
| 1558 | } |
| 1559 | |
Jiri Slaby | 0c41942 | 2021-06-18 08:14:22 +0200 | [diff] [blame] | 1560 | static u8 mxser_receive_chars_old(struct tty_struct *tty, |
Jiri Slaby | 95b3ea4 | 2021-06-18 08:14:37 +0200 | [diff] [blame] | 1561 | struct mxser_port *port, u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | { |
Jiri Slaby | 0c41942 | 2021-06-18 08:14:22 +0200 | [diff] [blame] | 1563 | enum mxser_must_hwid hwid = port->board->must_hwid; |
| 1564 | int recv_room = tty->receive_room; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | int ignored = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | int max = 256; |
Jiri Slaby | 95b3ea4 | 2021-06-18 08:14:37 +0200 | [diff] [blame] | 1567 | int cnt = 0; |
Jiri Slaby | 0c41942 | 2021-06-18 08:14:22 +0200 | [diff] [blame] | 1568 | u8 ch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | |
| 1570 | do { |
| 1571 | if (max-- < 0) |
| 1572 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1574 | ch = inb(port->ioaddr + UART_RX); |
Jiri Slaby | 0c41942 | 2021-06-18 08:14:22 +0200 | [diff] [blame] | 1575 | if (hwid && (status & UART_LSR_OE)) |
Jiri Slaby | d249e66 | 2021-09-22 09:59:38 +0200 | [diff] [blame] | 1576 | outb(port->FCR | UART_FCR_CLEAR_RCVR, |
Jiri Slaby | aaa28e9 | 2021-06-18 08:15:07 +0200 | [diff] [blame] | 1577 | port->ioaddr + UART_FCR); |
Jiri Slaby | 1551780 | 2021-06-18 08:14:18 +0200 | [diff] [blame] | 1578 | status &= port->read_status_mask; |
| 1579 | if (status & port->ignore_status_mask) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | if (++ignored > 100) |
| 1581 | break; |
| 1582 | } else { |
Denis Vlasenko | 3399ba5 | 2005-06-06 13:35:55 -0700 | [diff] [blame] | 1583 | char flag = 0; |
Jiri Slaby | 7064005 | 2021-06-18 08:14:26 +0200 | [diff] [blame] | 1584 | if (status & UART_LSR_BRK_ERROR_BITS) { |
Jiri Slaby | 1551780 | 2021-06-18 08:14:18 +0200 | [diff] [blame] | 1585 | if (status & UART_LSR_BI) { |
Denis Vlasenko | 3399ba5 | 2005-06-06 13:35:55 -0700 | [diff] [blame] | 1586 | flag = TTY_BREAK; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1587 | port->icount.brk++; |
| 1588 | |
Alan Cox | 0ad9e7d | 2008-07-16 21:56:10 +0100 | [diff] [blame] | 1589 | if (port->port.flags & ASYNC_SAK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | do_SAK(tty); |
Jiri Slaby | 1551780 | 2021-06-18 08:14:18 +0200 | [diff] [blame] | 1591 | } else if (status & UART_LSR_PE) { |
Denis Vlasenko | 3399ba5 | 2005-06-06 13:35:55 -0700 | [diff] [blame] | 1592 | flag = TTY_PARITY; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1593 | port->icount.parity++; |
Jiri Slaby | 1551780 | 2021-06-18 08:14:18 +0200 | [diff] [blame] | 1594 | } else if (status & UART_LSR_FE) { |
Denis Vlasenko | 3399ba5 | 2005-06-06 13:35:55 -0700 | [diff] [blame] | 1595 | flag = TTY_FRAME; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1596 | port->icount.frame++; |
Jiri Slaby | 1551780 | 2021-06-18 08:14:18 +0200 | [diff] [blame] | 1597 | } else if (status & UART_LSR_OE) { |
Denis Vlasenko | 3399ba5 | 2005-06-06 13:35:55 -0700 | [diff] [blame] | 1598 | flag = TTY_OVERRUN; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1599 | port->icount.overrun++; |
Jiri Slaby | 6de6e5c | 2021-06-18 08:14:23 +0200 | [diff] [blame] | 1600 | } |
Denis Vlasenko | 3399ba5 | 2005-06-06 13:35:55 -0700 | [diff] [blame] | 1601 | } |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 1602 | tty_insert_flip_char(&port->port, ch, flag); |
Jiri Slaby | 95b3ea4 | 2021-06-18 08:14:37 +0200 | [diff] [blame] | 1603 | cnt++; |
| 1604 | if (cnt >= recv_room) { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1605 | if (!port->ldisc_stop_rx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | mxser_stoprx(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | break; |
| 1608 | } |
| 1609 | |
| 1610 | } |
| 1611 | |
Jiri Slaby | 0c41942 | 2021-06-18 08:14:22 +0200 | [diff] [blame] | 1612 | if (hwid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | |
Jiri Slaby | 1551780 | 2021-06-18 08:14:18 +0200 | [diff] [blame] | 1615 | status = inb(port->ioaddr + UART_LSR); |
| 1616 | } while (status & UART_LSR_DR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | |
Jiri Slaby | 0c41942 | 2021-06-18 08:14:22 +0200 | [diff] [blame] | 1618 | return status; |
| 1619 | } |
| 1620 | |
| 1621 | static u8 mxser_receive_chars(struct tty_struct *tty, |
| 1622 | struct mxser_port *port, u8 status) |
| 1623 | { |
Jiri Slaby | 0c41942 | 2021-06-18 08:14:22 +0200 | [diff] [blame] | 1624 | if (tty->receive_room == 0 && !port->ldisc_stop_rx) |
| 1625 | mxser_stoprx(tty); |
| 1626 | |
Jiri Slaby | 95b3ea4 | 2021-06-18 08:14:37 +0200 | [diff] [blame] | 1627 | if (!mxser_receive_chars_new(tty, port, status)) |
| 1628 | status = mxser_receive_chars_old(tty, port, status); |
Jiri Slaby | 0c41942 | 2021-06-18 08:14:22 +0200 | [diff] [blame] | 1629 | |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 1630 | tty_flip_buffer_push(&port->port); |
Jiri Slaby | 1551780 | 2021-06-18 08:14:18 +0200 | [diff] [blame] | 1631 | |
| 1632 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 1635 | static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | { |
| 1637 | int count, cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1639 | if (port->x_char) { |
| 1640 | outb(port->x_char, port->ioaddr + UART_TX); |
| 1641 | port->x_char = 0; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1642 | port->icount.tx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | return; |
| 1644 | } |
| 1645 | |
Alan Cox | 0ad9e7d | 2008-07-16 21:56:10 +0100 | [diff] [blame] | 1646 | if (port->port.xmit_buf == NULL) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1647 | return; |
| 1648 | |
Jiri Slaby | 265ceff | 2021-06-18 08:15:06 +0200 | [diff] [blame] | 1649 | if (!port->xmit_cnt || tty->flow.stopped || |
Jiri Slaby | 5d1ea1a | 2021-06-18 08:15:12 +0200 | [diff] [blame] | 1650 | (tty->hw_stopped && !mxser_16550A_or_MUST(port))) { |
Jiri Slaby | 740165f | 2021-06-18 08:15:05 +0200 | [diff] [blame] | 1651 | __mxser_stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | return; |
| 1653 | } |
| 1654 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1655 | cnt = port->xmit_cnt; |
| 1656 | count = port->xmit_fifo_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | do { |
Alan Cox | 0ad9e7d | 2008-07-16 21:56:10 +0100 | [diff] [blame] | 1658 | outb(port->port.xmit_buf[port->xmit_tail++], |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1659 | port->ioaddr + UART_TX); |
| 1660 | port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1); |
Jiri Slaby | 265ceff | 2021-06-18 08:15:06 +0200 | [diff] [blame] | 1661 | if (!--port->xmit_cnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | break; |
| 1663 | } while (--count > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1665 | port->icount.tx += (cnt - port->xmit_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | |
Kulikov Vasiliy | 464eb8f | 2010-07-23 20:34:53 +0400 | [diff] [blame] | 1667 | if (port->xmit_cnt < WAKEUP_CHARS) |
Alan Cox | 216ba02 | 2008-10-13 10:40:19 +0100 | [diff] [blame] | 1668 | tty_wakeup(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | |
Jiri Slaby | 265ceff | 2021-06-18 08:15:06 +0200 | [diff] [blame] | 1670 | if (!port->xmit_cnt) |
Jiri Slaby | 740165f | 2021-06-18 08:15:05 +0200 | [diff] [blame] | 1671 | __mxser_stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
Jiri Slaby | 9e40ea1 | 2021-06-18 08:14:19 +0200 | [diff] [blame] | 1674 | static bool mxser_port_isr(struct mxser_port *port) |
| 1675 | { |
| 1676 | struct tty_struct *tty; |
| 1677 | u8 iir, msr, status; |
| 1678 | bool error = false; |
| 1679 | |
| 1680 | iir = inb(port->ioaddr + UART_IIR); |
| 1681 | if (iir & UART_IIR_NO_INT) |
| 1682 | return true; |
| 1683 | |
| 1684 | iir &= MOXA_MUST_IIR_MASK; |
| 1685 | tty = tty_port_tty_get(&port->port); |
| 1686 | if (!tty || port->closing || !tty_port_initialized(&port->port)) { |
| 1687 | status = inb(port->ioaddr + UART_LSR); |
Jiri Slaby | d249e66 | 2021-09-22 09:59:38 +0200 | [diff] [blame] | 1688 | outb(port->FCR | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT, |
Jiri Slaby | aaa28e9 | 2021-06-18 08:15:07 +0200 | [diff] [blame] | 1689 | port->ioaddr + UART_FCR); |
Jiri Slaby | 9e40ea1 | 2021-06-18 08:14:19 +0200 | [diff] [blame] | 1690 | inb(port->ioaddr + UART_MSR); |
| 1691 | |
| 1692 | error = true; |
| 1693 | goto put_tty; |
| 1694 | } |
| 1695 | |
| 1696 | status = inb(port->ioaddr + UART_LSR); |
| 1697 | |
Jiri Slaby | 9e40ea1 | 2021-06-18 08:14:19 +0200 | [diff] [blame] | 1698 | if (port->board->must_hwid) { |
| 1699 | if (iir == MOXA_MUST_IIR_GDA || |
| 1700 | iir == MOXA_MUST_IIR_RDA || |
| 1701 | iir == MOXA_MUST_IIR_RTO || |
| 1702 | iir == MOXA_MUST_IIR_LSR) |
| 1703 | status = mxser_receive_chars(tty, port, status); |
| 1704 | } else { |
| 1705 | status &= port->read_status_mask; |
| 1706 | if (status & UART_LSR_DR) |
| 1707 | status = mxser_receive_chars(tty, port, status); |
| 1708 | } |
| 1709 | |
| 1710 | msr = inb(port->ioaddr + UART_MSR); |
| 1711 | if (msr & UART_MSR_ANY_DELTA) |
| 1712 | mxser_check_modem_status(tty, port, msr); |
| 1713 | |
| 1714 | if (port->board->must_hwid) { |
| 1715 | if (iir == 0x02 && (status & UART_LSR_THRE)) |
| 1716 | mxser_transmit_chars(tty, port); |
| 1717 | } else { |
| 1718 | if (status & UART_LSR_THRE) |
| 1719 | mxser_transmit_chars(tty, port); |
| 1720 | } |
| 1721 | |
| 1722 | put_tty: |
| 1723 | tty_kref_put(tty); |
| 1724 | |
| 1725 | return error; |
| 1726 | } |
| 1727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | /* |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1729 | * This is the serial driver's generic interrupt routine |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | */ |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1731 | static irqreturn_t mxser_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | { |
Jiri Slaby | cef222c | 2021-06-18 08:14:17 +0200 | [diff] [blame] | 1733 | struct mxser_board *brd = dev_id; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1734 | struct mxser_port *port; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1735 | unsigned int int_cnt, pass_counter = 0; |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 1736 | unsigned int i, max = brd->nports; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1737 | int handled = IRQ_NONE; |
Jiri Slaby | 9cb5c9c | 2021-06-18 08:14:20 +0200 | [diff] [blame] | 1738 | u8 irqbits, bits, mask = BIT(max) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1740 | while (pass_counter++ < MXSER_ISR_PASS_LIMIT) { |
Jiri Slaby | 9cb5c9c | 2021-06-18 08:14:20 +0200 | [diff] [blame] | 1741 | irqbits = inb(brd->vector) & mask; |
| 1742 | if (irqbits == mask) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1743 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1745 | handled = IRQ_HANDLED; |
| 1746 | for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) { |
Jiri Slaby | 9cb5c9c | 2021-06-18 08:14:20 +0200 | [diff] [blame] | 1747 | if (irqbits == mask) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1748 | break; |
| 1749 | if (bits & irqbits) |
| 1750 | continue; |
| 1751 | port = &brd->ports[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1753 | int_cnt = 0; |
| 1754 | spin_lock(&port->slock); |
| 1755 | do { |
Jiri Slaby | 9e40ea1 | 2021-06-18 08:14:19 +0200 | [diff] [blame] | 1756 | if (mxser_port_isr(port)) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1757 | break; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1758 | } while (int_cnt++ < MXSER_ISR_PASS_LIMIT); |
| 1759 | spin_unlock(&port->slock); |
| 1760 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1763 | return handled; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1766 | static const struct tty_operations mxser_ops = { |
| 1767 | .open = mxser_open, |
| 1768 | .close = mxser_close, |
| 1769 | .write = mxser_write, |
| 1770 | .put_char = mxser_put_char, |
| 1771 | .flush_chars = mxser_flush_chars, |
| 1772 | .write_room = mxser_write_room, |
| 1773 | .chars_in_buffer = mxser_chars_in_buffer, |
| 1774 | .flush_buffer = mxser_flush_buffer, |
| 1775 | .ioctl = mxser_ioctl, |
| 1776 | .throttle = mxser_throttle, |
| 1777 | .unthrottle = mxser_unthrottle, |
| 1778 | .set_termios = mxser_set_termios, |
| 1779 | .stop = mxser_stop, |
| 1780 | .start = mxser_start, |
| 1781 | .hangup = mxser_hangup, |
| 1782 | .break_ctl = mxser_rs_break, |
| 1783 | .wait_until_sent = mxser_wait_until_sent, |
| 1784 | .tiocmget = mxser_tiocmget, |
| 1785 | .tiocmset = mxser_tiocmset, |
Al Viro | 6da5b58 | 2018-09-11 22:22:06 -0400 | [diff] [blame] | 1786 | .set_serial = mxser_set_serial_info, |
| 1787 | .get_serial = mxser_get_serial_info, |
Alan Cox | 0587102 | 2010-09-16 18:21:52 +0100 | [diff] [blame] | 1788 | .get_icount = mxser_get_icount, |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1789 | }; |
| 1790 | |
Aya Mahfouz | 04b757d | 2015-12-15 01:53:36 +0200 | [diff] [blame] | 1791 | static const struct tty_port_operations mxser_port_ops = { |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 1792 | .carrier_raised = mxser_carrier_raised, |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 1793 | .dtr_rts = mxser_dtr_rts, |
Alan Cox | 6769140 | 2009-11-30 13:17:35 +0000 | [diff] [blame] | 1794 | .activate = mxser_activate, |
| 1795 | .shutdown = mxser_shutdown_port, |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 1796 | }; |
| 1797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | /* |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1799 | * The MOXA Smartio/Industio serial driver boot-time initialization code! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | */ |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1801 | |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 1802 | static void mxser_initbrd(struct mxser_board *brd, bool high_baud) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1804 | struct mxser_port *info; |
| 1805 | unsigned int i; |
Jiri Slaby | 57faa7d | 2021-06-18 08:14:49 +0200 | [diff] [blame] | 1806 | bool is_mu860; |
| 1807 | |
| 1808 | brd->must_hwid = mxser_must_get_hwid(brd->ports[0].ioaddr); |
| 1809 | is_mu860 = brd->must_hwid == MOXA_MUST_MU860_HWID; |
| 1810 | |
| 1811 | for (i = 0; i < UART_INFO_NUM; i++) { |
| 1812 | if (Gpci_uart_info[i].type == brd->must_hwid) { |
| 1813 | brd->max_baud = Gpci_uart_info[i].max_baud; |
| 1814 | |
| 1815 | /* exception....CP-102 */ |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 1816 | if (high_baud) |
Jiri Slaby | 57faa7d | 2021-06-18 08:14:49 +0200 | [diff] [blame] | 1817 | brd->max_baud = 921600; |
| 1818 | break; |
| 1819 | } |
| 1820 | } |
| 1821 | |
| 1822 | if (is_mu860) { |
| 1823 | /* set to RS232 mode by default */ |
| 1824 | outb(0, brd->vector + 4); |
| 1825 | outb(0, brd->vector + 0x0c); |
| 1826 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 1828 | for (i = 0; i < brd->nports; i++) { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1829 | info = &brd->ports[i]; |
Jiri Slaby | 57faa7d | 2021-06-18 08:14:49 +0200 | [diff] [blame] | 1830 | if (is_mu860) { |
| 1831 | if (i < 4) |
| 1832 | info->opmode_ioaddr = brd->vector + 4; |
| 1833 | else |
| 1834 | info->opmode_ioaddr = brd->vector + 0x0c; |
| 1835 | } |
Alan Cox | 44b7d1b | 2008-07-16 21:57:18 +0100 | [diff] [blame] | 1836 | tty_port_init(&info->port); |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 1837 | info->port.ops = &mxser_port_ops; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1838 | info->board = brd; |
Jiri Slaby | 1923628 | 2021-09-22 09:59:34 +0200 | [diff] [blame] | 1839 | info->ldisc_stop_rx = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1841 | /* Enhance mode enabled here */ |
Jiri Slaby | 292955a | 2021-06-18 08:14:13 +0200 | [diff] [blame] | 1842 | if (brd->must_hwid != MOXA_OTHER_UART) |
Jiri Slaby | edb7d27 | 2021-06-18 08:14:46 +0200 | [diff] [blame] | 1843 | mxser_must_set_enhance_mode(info->ioaddr, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | |
Jiri Slaby | 58a2ddb | 2021-06-18 08:14:15 +0200 | [diff] [blame] | 1845 | info->type = PORT_16550A; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1846 | |
Jiri Slaby | c3db20c | 2021-06-18 08:14:55 +0200 | [diff] [blame] | 1847 | mxser_process_txrx_fifo(info); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1848 | |
Alan Cox | 44b7d1b | 2008-07-16 21:57:18 +0100 | [diff] [blame] | 1849 | info->port.close_delay = 5 * HZ / 10; |
| 1850 | info->port.closing_wait = 30 * HZ; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1851 | spin_lock_init(&info->slock); |
| 1852 | |
| 1853 | /* before set INT ISR, disable all int */ |
| 1854 | outb(inb(info->ioaddr + UART_IER) & 0xf0, |
| 1855 | info->ioaddr + UART_IER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 1859 | static int mxser_probe(struct pci_dev *pdev, |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1860 | const struct pci_device_id *ent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1862 | struct mxser_board *brd; |
Jiri Slaby | 13d4aba | 2021-06-18 08:14:57 +0200 | [diff] [blame] | 1863 | unsigned int i, base; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1864 | unsigned long ioaddress; |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 1865 | unsigned short nports = MXSER_NPORTS(ent->driver_data); |
Alexey Khoroshilov | 9e17df3 | 2013-02-17 02:24:37 +0400 | [diff] [blame] | 1866 | struct device *tty_dev; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1867 | int retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | |
Jiri Slaby | f8b6b32 | 2021-06-18 08:14:58 +0200 | [diff] [blame] | 1869 | i = find_first_zero_bit(mxser_boards, MXSER_BOARDS); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1870 | if (i >= MXSER_BOARDS) { |
Jiri Slaby | 83766bc | 2008-07-25 01:48:21 -0700 | [diff] [blame] | 1871 | dev_err(&pdev->dev, "too many boards found (maximum %d), board " |
| 1872 | "not configured\n", MXSER_BOARDS); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1873 | goto err; |
| 1874 | } |
| 1875 | |
Jiri Slaby | ad1c92f | 2021-06-18 08:14:59 +0200 | [diff] [blame] | 1876 | brd = devm_kzalloc(&pdev->dev, struct_size(brd, ports, nports), |
| 1877 | GFP_KERNEL); |
Jiri Slaby | f8b6b32 | 2021-06-18 08:14:58 +0200 | [diff] [blame] | 1878 | if (!brd) |
| 1879 | goto err; |
| 1880 | |
Jiri Slaby | 13d4aba | 2021-06-18 08:14:57 +0200 | [diff] [blame] | 1881 | brd->idx = i; |
Jiri Slaby | f8b6b32 | 2021-06-18 08:14:58 +0200 | [diff] [blame] | 1882 | __set_bit(brd->idx, mxser_boards); |
Jiri Slaby | 13d4aba | 2021-06-18 08:14:57 +0200 | [diff] [blame] | 1883 | base = i * MXSER_PORTS_PER_BOARD; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1884 | |
Jiri Slaby | dcb04e2 | 2021-06-18 08:14:51 +0200 | [diff] [blame] | 1885 | retval = pcim_enable_device(pdev); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1886 | if (retval) { |
Jiri Slaby | 83766bc | 2008-07-25 01:48:21 -0700 | [diff] [blame] | 1887 | dev_err(&pdev->dev, "PCI enable failed\n"); |
Jiri Slaby | f8b6b32 | 2021-06-18 08:14:58 +0200 | [diff] [blame] | 1888 | goto err_zero; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | /* io address */ |
| 1892 | ioaddress = pci_resource_start(pdev, 2); |
| 1893 | retval = pci_request_region(pdev, 2, "mxser(IO)"); |
| 1894 | if (retval) |
Jiri Slaby | f8b6b32 | 2021-06-18 08:14:58 +0200 | [diff] [blame] | 1895 | goto err_zero; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1896 | |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 1897 | brd->nports = nports; |
| 1898 | for (i = 0; i < nports; i++) |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1899 | brd->ports[i].ioaddr = ioaddress + 8 * i; |
| 1900 | |
| 1901 | /* vector */ |
| 1902 | ioaddress = pci_resource_start(pdev, 3); |
| 1903 | retval = pci_request_region(pdev, 3, "mxser(vector)"); |
| 1904 | if (retval) |
Jiri Slaby | df48051 | 2010-09-03 10:31:37 +0200 | [diff] [blame] | 1905 | goto err_zero; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1906 | brd->vector = ioaddress; |
| 1907 | |
| 1908 | /* irq */ |
| 1909 | brd->irq = pdev->irq; |
| 1910 | |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 1911 | mxser_initbrd(brd, ent->driver_data & MXSER_HIGHBAUD); |
Jiri Slaby | 7f0e79d | 2021-06-18 08:14:52 +0200 | [diff] [blame] | 1912 | |
| 1913 | retval = devm_request_irq(&pdev->dev, brd->irq, mxser_interrupt, |
| 1914 | IRQF_SHARED, "mxser", brd); |
| 1915 | if (retval) { |
| 1916 | dev_err(&pdev->dev, "request irq failed"); |
| 1917 | goto err_relbrd; |
| 1918 | } |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1919 | |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 1920 | for (i = 0; i < nports; i++) { |
Alexey Khoroshilov | 9e17df3 | 2013-02-17 02:24:37 +0400 | [diff] [blame] | 1921 | tty_dev = tty_port_register_device(&brd->ports[i].port, |
Jiri Slaby | 13d4aba | 2021-06-18 08:14:57 +0200 | [diff] [blame] | 1922 | mxvar_sdriver, base + i, &pdev->dev); |
Alexey Khoroshilov | 9e17df3 | 2013-02-17 02:24:37 +0400 | [diff] [blame] | 1923 | if (IS_ERR(tty_dev)) { |
| 1924 | retval = PTR_ERR(tty_dev); |
Alexey Khoroshilov | 1b581f1 | 2013-04-07 23:46:47 +0400 | [diff] [blame] | 1925 | for (; i > 0; i--) |
Alexey Khoroshilov | 9e17df3 | 2013-02-17 02:24:37 +0400 | [diff] [blame] | 1926 | tty_unregister_device(mxvar_sdriver, |
Jiri Slaby | 13d4aba | 2021-06-18 08:14:57 +0200 | [diff] [blame] | 1927 | base + i - 1); |
Alexey Khoroshilov | 9e17df3 | 2013-02-17 02:24:37 +0400 | [diff] [blame] | 1928 | goto err_relbrd; |
| 1929 | } |
| 1930 | } |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1931 | |
| 1932 | pci_set_drvdata(pdev, brd); |
| 1933 | |
| 1934 | return 0; |
Alexey Khoroshilov | 9e17df3 | 2013-02-17 02:24:37 +0400 | [diff] [blame] | 1935 | err_relbrd: |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 1936 | for (i = 0; i < nports; i++) |
Alexey Khoroshilov | 9e17df3 | 2013-02-17 02:24:37 +0400 | [diff] [blame] | 1937 | tty_port_destroy(&brd->ports[i].port); |
Jiri Slaby | df48051 | 2010-09-03 10:31:37 +0200 | [diff] [blame] | 1938 | err_zero: |
Jiri Slaby | f8b6b32 | 2021-06-18 08:14:58 +0200 | [diff] [blame] | 1939 | __clear_bit(brd->idx, mxser_boards); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1940 | err: |
| 1941 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | } |
| 1943 | |
Bill Pemberton | ae8d8a1 | 2012-11-19 13:26:18 -0500 | [diff] [blame] | 1944 | static void mxser_remove(struct pci_dev *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1946 | struct mxser_board *brd = pci_get_drvdata(pdev); |
Jiri Slaby | 13d4aba | 2021-06-18 08:14:57 +0200 | [diff] [blame] | 1947 | unsigned int i, base = brd->idx * MXSER_PORTS_PER_BOARD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | |
Jiri Slaby | c24c31f | 2021-06-18 08:14:54 +0200 | [diff] [blame] | 1949 | for (i = 0; i < brd->nports; i++) { |
Jiri Slaby | 13d4aba | 2021-06-18 08:14:57 +0200 | [diff] [blame] | 1950 | tty_unregister_device(mxvar_sdriver, base + i); |
Jiri Slaby | d450f08 | 2021-06-18 08:14:50 +0200 | [diff] [blame] | 1951 | tty_port_destroy(&brd->ports[i].port); |
| 1952 | } |
| 1953 | |
Jiri Slaby | f8b6b32 | 2021-06-18 08:14:58 +0200 | [diff] [blame] | 1954 | __clear_bit(brd->idx, mxser_boards); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | } |
| 1956 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1957 | static struct pci_driver mxser_driver = { |
| 1958 | .name = "mxser", |
| 1959 | .id_table = mxser_pcibrds, |
| 1960 | .probe = mxser_probe, |
Bill Pemberton | 91116cb | 2012-11-19 13:21:06 -0500 | [diff] [blame] | 1961 | .remove = mxser_remove |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1962 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1964 | static int __init mxser_module_init(void) |
| 1965 | { |
Jiri Slaby | 1df0092 | 2008-07-25 01:48:22 -0700 | [diff] [blame] | 1966 | int retval; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1967 | |
Jiri Slaby | 39b7b42 | 2021-07-23 09:43:13 +0200 | [diff] [blame] | 1968 | mxvar_sdriver = tty_alloc_driver(MXSER_PORTS, TTY_DRIVER_REAL_RAW | |
| 1969 | TTY_DRIVER_DYNAMIC_DEV); |
| 1970 | if (IS_ERR(mxvar_sdriver)) |
| 1971 | return PTR_ERR(mxvar_sdriver); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1972 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1973 | /* Initialize the tty_driver structure */ |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1974 | mxvar_sdriver->name = "ttyMI"; |
| 1975 | mxvar_sdriver->major = ttymajor; |
| 1976 | mxvar_sdriver->minor_start = 0; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1977 | mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL; |
| 1978 | mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL; |
| 1979 | mxvar_sdriver->init_termios = tty_std_termios; |
| 1980 | mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1981 | tty_set_operations(mxvar_sdriver, &mxser_ops); |
| 1982 | |
| 1983 | retval = tty_register_driver(mxvar_sdriver); |
| 1984 | if (retval) { |
| 1985 | printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family " |
| 1986 | "tty driver !\n"); |
| 1987 | goto err_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | } |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1989 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1990 | retval = pci_register_driver(&mxser_driver); |
| 1991 | if (retval) { |
Jiri Slaby | 83766bc | 2008-07-25 01:48:21 -0700 | [diff] [blame] | 1992 | printk(KERN_ERR "mxser: can't register pci driver\n"); |
Jiri Slaby | 2913436 | 2021-06-18 08:14:07 +0200 | [diff] [blame] | 1993 | goto err_unr; |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1994 | } |
| 1995 | |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 1996 | return 0; |
| 1997 | err_unr: |
| 1998 | tty_unregister_driver(mxvar_sdriver); |
| 1999 | err_put: |
Jiri Slaby | 9f90a4d | 2021-07-23 09:43:16 +0200 | [diff] [blame] | 2000 | tty_driver_kref_put(mxvar_sdriver); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 2001 | return retval; |
| 2002 | } |
| 2003 | |
| 2004 | static void __exit mxser_module_exit(void) |
| 2005 | { |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 2006 | pci_unregister_driver(&mxser_driver); |
Jiri Slaby | 1c45607 | 2008-02-07 00:16:46 -0800 | [diff] [blame] | 2007 | tty_unregister_driver(mxvar_sdriver); |
Jiri Slaby | 9f90a4d | 2021-07-23 09:43:16 +0200 | [diff] [blame] | 2008 | tty_driver_kref_put(mxvar_sdriver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | module_init(mxser_module_init); |
| 2012 | module_exit(mxser_module_exit); |