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