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