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