blob: 308cb6014002d157b078f6eb6a65ac18d2fd1b0d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
3 *
Jiri Slaby80ff8a82008-02-07 00:16:51 -08004 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
5 * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Jiri Slaby1c456072008-02-07 00:16:46 -08007 * This code is loosely based on the 1.8 moxa driver which is based on
8 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
9 * others.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070014 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
17 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
18 * - Fixed x86_64 cleanness
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/signal.h>
24#include <linux/sched.h>
25#include <linux/timer.h>
26#include <linux/interrupt.h>
27#include <linux/tty.h>
28#include <linux/tty_flip.h>
29#include <linux/serial.h>
30#include <linux/serial_reg.h>
31#include <linux/major.h>
32#include <linux/string.h>
33#include <linux/fcntl.h>
34#include <linux/ptrace.h>
35#include <linux/gfp.h>
36#include <linux/ioport.h>
37#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/delay.h>
39#include <linux/pci.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070040#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/system.h>
43#include <asm/io.h>
44#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
46
47#include "mxser.h"
48
Jiri Slabye129def2008-07-22 11:20:34 +010049#define MXSER_VERSION "2.0.4" /* 1.12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define MXSERMAJOR 174
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define MXSER_BOARDS 4 /* Max. boards */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slaby1c456072008-02-07 00:16:46 -080054#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
55#define MXSER_ISR_PASS_LIMIT 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jiri Slaby1c456072008-02-07 00:16:46 -080057/*CheckIsMoxaMust return value*/
58#define MOXA_OTHER_UART 0x00
59#define MOXA_MUST_MU150_HWID 0x01
60#define MOXA_MUST_MU860_HWID 0x02
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define WAKEUP_CHARS 256
63
64#define UART_MCR_AFE 0x20
65#define UART_LSR_SPECIAL 0x1E
66
Jiri Slabye129def2008-07-22 11:20:34 +010067#define PCI_DEVICE_ID_POS104UL 0x1044
Jiri Slaby1c456072008-02-07 00:16:46 -080068#define PCI_DEVICE_ID_CB108 0x1080
Jiri Slabye129def2008-07-22 11:20:34 +010069#define PCI_DEVICE_ID_CP102UF 0x1023
Jiri Slaby1c456072008-02-07 00:16:46 -080070#define PCI_DEVICE_ID_CB114 0x1142
Jiri Slaby80ff8a82008-02-07 00:16:51 -080071#define PCI_DEVICE_ID_CP114UL 0x1143
Jiri Slaby1c456072008-02-07 00:16:46 -080072#define PCI_DEVICE_ID_CB134I 0x1341
73#define PCI_DEVICE_ID_CP138U 0x1380
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76#define C168_ASIC_ID 1
77#define C104_ASIC_ID 2
78#define C102_ASIC_ID 0xB
79#define CI132_ASIC_ID 4
80#define CI134_ASIC_ID 3
81#define CI104J_ASIC_ID 5
82
Jiri Slaby1c456072008-02-07 00:16:46 -080083#define MXSER_HIGHBAUD 1
84#define MXSER_HAS2 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070086/* This is only for PCI */
Jiri Slaby1c456072008-02-07 00:16:46 -080087static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 int type;
89 int tx_fifo;
90 int rx_fifo;
91 int xmit_fifo_size;
92 int rx_high_water;
93 int rx_trigger;
94 int rx_low_water;
95 long max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -080096} Gpci_uart_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
98 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
99 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
100};
Jiri Slaby1c456072008-02-07 00:16:46 -0800101#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Jiri Slaby1c456072008-02-07 00:16:46 -0800103struct mxser_cardinfo {
104 char *name;
105 unsigned int nports;
106 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
Jiri Slaby1c456072008-02-07 00:16:46 -0800109static const struct mxser_cardinfo mxser_cards[] = {
110/* 0*/ { "C168 series", 8, },
111 { "C104 series", 4, },
112 { "CI-104J series", 4, },
113 { "C168H/PCI series", 8, },
114 { "C104H/PCI series", 4, },
115/* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
116 { "CI-132 series", 4, MXSER_HAS2 },
117 { "CI-134 series", 4, },
118 { "CP-132 series", 2, },
119 { "CP-114 series", 4, },
120/*10*/ { "CT-114 series", 4, },
121 { "CP-102 series", 2, MXSER_HIGHBAUD },
122 { "CP-104U series", 4, },
123 { "CP-168U series", 8, },
124 { "CP-132U series", 2, },
125/*15*/ { "CP-134U series", 4, },
126 { "CP-104JU series", 4, },
127 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
128 { "CP-118U series", 8, },
129 { "CP-102UL series", 2, },
130/*20*/ { "CP-102U series", 2, },
131 { "CP-118EL series", 8, },
132 { "CP-168EL series", 8, },
133 { "CP-104EL series", 4, },
134 { "CB-108 series", 8, },
135/*25*/ { "CB-114 series", 4, },
136 { "CB-134I series", 4, },
137 { "CP-138U series", 8, },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800138 { "POS-104UL series", 4, },
Jiri Slabye129def2008-07-22 11:20:34 +0100139 { "CP-114UL series", 4, },
140/*30*/ { "CP-102UF series", 2, }
Jiri Slaby1c456072008-02-07 00:16:46 -0800141};
142
143/* driver_data correspond to the lines in the structure above
144 see also ISA probe function before you change something */
145static struct pci_device_id mxser_pcibrds[] = {
146 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
147 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
148 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
149 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
150 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
Jiri Slabye129def2008-07-22 11:20:34 +0100170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 30 },
Jiri Slaby1c456072008-02-07 00:16:46 -0800171 { }
172};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
174
Jiri Slaby1df00922008-07-25 01:48:22 -0700175static unsigned long ioaddr[MXSER_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static int ttymajor = MXSERMAJOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/* Variables for insmod */
179
180MODULE_AUTHOR("Casper Yang");
181MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
Jiri Slaby1df00922008-07-25 01:48:22 -0700182module_param_array(ioaddr, ulong, NULL, 0);
183MODULE_PARM_DESC(ioaddr, "ISA io addresses to look for a moxa board");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800184module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185MODULE_LICENSE("GPL");
186
187struct mxser_log {
188 int tick;
189 unsigned long rxcnt[MXSER_PORTS];
190 unsigned long txcnt[MXSER_PORTS];
191};
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193struct mxser_mon {
194 unsigned long rxcnt;
195 unsigned long txcnt;
196 unsigned long up_rxcnt;
197 unsigned long up_txcnt;
198 int modem_status;
199 unsigned char hold_reason;
200};
201
202struct mxser_mon_ext {
203 unsigned long rx_cnt[32];
204 unsigned long tx_cnt[32];
205 unsigned long up_rxcnt[32];
206 unsigned long up_txcnt[32];
207 int modem_status[32];
208
209 long baudrate[32];
210 int databits[32];
211 int stopbits[32];
212 int parity[32];
213 int flowctrl[32];
214 int fifo[32];
215 int iftype[32];
216};
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700217
Jiri Slaby1c456072008-02-07 00:16:46 -0800218struct mxser_board;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Jiri Slaby1c456072008-02-07 00:16:46 -0800220struct mxser_port {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100221 struct tty_port port;
Jiri Slaby1c456072008-02-07 00:16:46 -0800222 struct mxser_board *board;
Jiri Slaby1c456072008-02-07 00:16:46 -0800223
224 unsigned long ioaddr;
225 unsigned long opmode_ioaddr;
226 int max_baud;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int rx_high_water;
229 int rx_trigger; /* Rx fifo trigger level */
230 int rx_low_water;
231 int baud_base; /* max. speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 int type; /* UART type */
Jiri Slaby1c456072008-02-07 00:16:46 -0800233
234 int x_char; /* xon/xoff character */
235 int IER; /* Interrupt Enable Register */
236 int MCR; /* Modem control register */
237
238 unsigned char stop_rx;
239 unsigned char ldisc_stop_rx;
240
241 int custom_divisor;
Jiri Slaby1c456072008-02-07 00:16:46 -0800242 unsigned char err_shadow;
Jiri Slaby1c456072008-02-07 00:16:46 -0800243
Jiri Slaby1c456072008-02-07 00:16:46 -0800244 struct async_icount icount; /* kernel counters for 4 input interrupts */
245 int timeout;
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int read_status_mask;
248 int ignore_status_mask;
249 int xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 int xmit_head;
251 int xmit_tail;
252 int xmit_cnt;
Jiri Slaby1c456072008-02-07 00:16:46 -0800253
Alan Cox606d0992006-12-08 02:38:45 -0800254 struct ktermios normal_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -0800255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 struct mxser_mon mon_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 spinlock_t slock;
Jiri Slaby1c456072008-02-07 00:16:46 -0800259 wait_queue_head_t delta_msr_wait;
260};
261
262struct mxser_board {
263 unsigned int idx;
264 int irq;
265 const struct mxser_cardinfo *info;
266 unsigned long vector;
267 unsigned long vector_mask;
268
269 int chip_flag;
270 int uart_type;
271
272 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273};
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275struct mxser_mstatus {
276 tcflag_t cflag;
277 int cts;
278 int dsr;
279 int ri;
280 int dcd;
281};
282
Jiri Slaby1c456072008-02-07 00:16:46 -0800283static struct mxser_board mxser_boards[MXSER_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static struct tty_driver *mxvar_sdriver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static struct mxser_log mxvar_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static int mxser_set_baud_method[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Christoph Hellwig148ff862008-04-30 00:54:29 -0700288static void mxser_enable_must_enchance_mode(unsigned long baseio)
289{
290 u8 oldlcr;
291 u8 efr;
292
293 oldlcr = inb(baseio + UART_LCR);
294 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
295
296 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
297 efr |= MOXA_MUST_EFR_EFRB_ENABLE;
298
299 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
300 outb(oldlcr, baseio + UART_LCR);
301}
302
303static void mxser_disable_must_enchance_mode(unsigned long baseio)
304{
305 u8 oldlcr;
306 u8 efr;
307
308 oldlcr = inb(baseio + UART_LCR);
309 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
310
311 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
312 efr &= ~MOXA_MUST_EFR_EFRB_ENABLE;
313
314 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
315 outb(oldlcr, baseio + UART_LCR);
316}
317
318static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
319{
320 u8 oldlcr;
321 u8 efr;
322
323 oldlcr = inb(baseio + UART_LCR);
324 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
325
326 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
327 efr &= ~MOXA_MUST_EFR_BANK_MASK;
328 efr |= MOXA_MUST_EFR_BANK0;
329
330 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
331 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
332 outb(oldlcr, baseio + UART_LCR);
333}
334
335static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
336{
337 u8 oldlcr;
338 u8 efr;
339
340 oldlcr = inb(baseio + UART_LCR);
341 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
342
343 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
344 efr &= ~MOXA_MUST_EFR_BANK_MASK;
345 efr |= MOXA_MUST_EFR_BANK0;
346
347 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
348 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
349 outb(oldlcr, baseio + UART_LCR);
350}
351
352static void mxser_set_must_fifo_value(struct mxser_port *info)
353{
354 u8 oldlcr;
355 u8 efr;
356
357 oldlcr = inb(info->ioaddr + UART_LCR);
358 outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
359
360 efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
361 efr &= ~MOXA_MUST_EFR_BANK_MASK;
362 efr |= MOXA_MUST_EFR_BANK1;
363
364 outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
365 outb((u8)info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
366 outb((u8)info->rx_trigger, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
367 outb((u8)info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
368 outb(oldlcr, info->ioaddr + UART_LCR);
369}
370
371static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
372{
373 u8 oldlcr;
374 u8 efr;
375
376 oldlcr = inb(baseio + UART_LCR);
377 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
378
379 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
380 efr &= ~MOXA_MUST_EFR_BANK_MASK;
381 efr |= MOXA_MUST_EFR_BANK2;
382
383 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
384 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
385 outb(oldlcr, baseio + UART_LCR);
386}
387
388static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
389{
390 u8 oldlcr;
391 u8 efr;
392
393 oldlcr = inb(baseio + UART_LCR);
394 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
395
396 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
397 efr &= ~MOXA_MUST_EFR_BANK_MASK;
398 efr |= MOXA_MUST_EFR_BANK2;
399
400 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
401 *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
402 outb(oldlcr, baseio + UART_LCR);
403}
404
405static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
406{
407 u8 oldlcr;
408 u8 efr;
409
410 oldlcr = inb(baseio + UART_LCR);
411 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
412
413 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
414 efr &= ~MOXA_MUST_EFR_SF_MASK;
415
416 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
417 outb(oldlcr, baseio + UART_LCR);
418}
419
420static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
421{
422 u8 oldlcr;
423 u8 efr;
424
425 oldlcr = inb(baseio + UART_LCR);
426 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
427
428 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
429 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
430 efr |= MOXA_MUST_EFR_SF_TX1;
431
432 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
433 outb(oldlcr, baseio + UART_LCR);
434}
435
436static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
437{
438 u8 oldlcr;
439 u8 efr;
440
441 oldlcr = inb(baseio + UART_LCR);
442 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
443
444 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
445 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
446
447 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
448 outb(oldlcr, baseio + UART_LCR);
449}
450
451static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
452{
453 u8 oldlcr;
454 u8 efr;
455
456 oldlcr = inb(baseio + UART_LCR);
457 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
458
459 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
460 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
461 efr |= MOXA_MUST_EFR_SF_RX1;
462
463 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
464 outb(oldlcr, baseio + UART_LCR);
465}
466
467static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
468{
469 u8 oldlcr;
470 u8 efr;
471
472 oldlcr = inb(baseio + UART_LCR);
473 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
474
475 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
476 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
477
478 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
479 outb(oldlcr, baseio + UART_LCR);
480}
481
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700482#ifdef CONFIG_PCI
Jiri Slaby1c456072008-02-07 00:16:46 -0800483static int __devinit CheckIsMoxaMust(unsigned long io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 u8 oldmcr, hwid;
486 int i;
487
488 outb(0, io + UART_LCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700489 mxser_disable_must_enchance_mode(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 oldmcr = inb(io + UART_MCR);
491 outb(0, io + UART_MCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700492 mxser_set_must_xon1_value(io, 0x11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if ((hwid = inb(io + UART_MCR)) != 0) {
494 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700495 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497
Christoph Hellwig148ff862008-04-30 00:54:29 -0700498 mxser_get_must_hardware_id(io, &hwid);
Jiri Slaby1c456072008-02-07 00:16:46 -0800499 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
500 if (hwid == Gpci_uart_info[i].type)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700501 return (int)hwid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 return MOXA_OTHER_UART;
504}
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700505#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Jiri Slaby1c456072008-02-07 00:16:46 -0800507static void process_txrx_fifo(struct mxser_port *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 int i;
510
511 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
512 info->rx_trigger = 1;
513 info->rx_high_water = 1;
514 info->rx_low_water = 1;
515 info->xmit_fifo_size = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800516 } else
517 for (i = 0; i < UART_INFO_NUM; i++)
518 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
520 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
521 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
522 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
523 break;
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Jiri Slaby1c456072008-02-07 00:16:46 -0800527static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Jiri Slaby72800df2008-07-25 01:48:20 -0700529 static unsigned char mxser_msr[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 unsigned char status = 0;
531
532 status = inb(baseaddr + UART_MSR);
533
534 mxser_msr[port] &= 0x0F;
535 mxser_msr[port] |= status;
536 status = mxser_msr[port];
537 if (mode)
538 mxser_msr[port] = 0;
539
540 return status;
541}
542
Jiri Slaby1c456072008-02-07 00:16:46 -0800543static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
544 struct mxser_port *port)
545{
546 DECLARE_WAITQUEUE(wait, current);
547 int retval;
548 int do_clocal = 0;
549 unsigned long flags;
550
551 /*
552 * If non-blocking mode is set, or the port is not enabled,
553 * then make the check up front and then exit.
554 */
555 if ((filp->f_flags & O_NONBLOCK) ||
556 test_bit(TTY_IO_ERROR, &tty->flags)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100557 port->port.flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby1c456072008-02-07 00:16:46 -0800558 return 0;
559 }
560
561 if (tty->termios->c_cflag & CLOCAL)
562 do_clocal = 1;
563
564 /*
565 * Block waiting for the carrier detect and the line to become
566 * free (i.e., not in use by the callout). While we are in
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100567 * this loop, port->port.count is dropped by one, so that
Jiri Slaby1c456072008-02-07 00:16:46 -0800568 * mxser_close() knows when to free things. We restore it upon
569 * exit, either normal or abnormal.
570 */
571 retval = 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100572 add_wait_queue(&port->port.open_wait, &wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800573
574 spin_lock_irqsave(&port->slock, flags);
575 if (!tty_hung_up_p(filp))
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100576 port->port.count--;
Jiri Slaby1c456072008-02-07 00:16:46 -0800577 spin_unlock_irqrestore(&port->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100578 port->port.blocked_open++;
Jiri Slaby1c456072008-02-07 00:16:46 -0800579 while (1) {
580 spin_lock_irqsave(&port->slock, flags);
581 outb(inb(port->ioaddr + UART_MCR) |
582 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
583 spin_unlock_irqrestore(&port->slock, flags);
584 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100585 if (tty_hung_up_p(filp) || !(port->port.flags & ASYNC_INITIALIZED)) {
586 if (port->port.flags & ASYNC_HUP_NOTIFY)
Jiri Slaby1c456072008-02-07 00:16:46 -0800587 retval = -EAGAIN;
588 else
589 retval = -ERESTARTSYS;
590 break;
591 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100592 if (!(port->port.flags & ASYNC_CLOSING) &&
Jiri Slaby1c456072008-02-07 00:16:46 -0800593 (do_clocal ||
594 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
595 break;
596 if (signal_pending(current)) {
597 retval = -ERESTARTSYS;
598 break;
599 }
600 schedule();
601 }
602 set_current_state(TASK_RUNNING);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100603 remove_wait_queue(&port->port.open_wait, &wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800604 if (!tty_hung_up_p(filp))
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100605 port->port.count++;
606 port->port.blocked_open--;
Jiri Slaby1c456072008-02-07 00:16:46 -0800607 if (retval)
608 return retval;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100609 port->port.flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby1c456072008-02-07 00:16:46 -0800610 return 0;
611}
612
Alan Cox216ba022008-10-13 10:40:19 +0100613static int mxser_set_baud(struct tty_struct *tty, long newspd)
Jiri Slaby1c456072008-02-07 00:16:46 -0800614{
Alan Cox216ba022008-10-13 10:40:19 +0100615 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800616 int quot = 0, baud;
617 unsigned char cval;
618
Alan Cox216ba022008-10-13 10:40:19 +0100619 if (!tty->termios)
Jiri Slaby1c456072008-02-07 00:16:46 -0800620 return -1;
621
Alan Cox216ba022008-10-13 10:40:19 +0100622 if (!info->ioaddr)
Jiri Slaby1c456072008-02-07 00:16:46 -0800623 return -1;
624
625 if (newspd > info->max_baud)
626 return -1;
627
628 if (newspd == 134) {
629 quot = 2 * info->baud_base / 269;
Alan Cox216ba022008-10-13 10:40:19 +0100630 tty_encode_baud_rate(tty, 134, 134);
Jiri Slaby1c456072008-02-07 00:16:46 -0800631 } else if (newspd) {
632 quot = info->baud_base / newspd;
633 if (quot == 0)
634 quot = 1;
635 baud = info->baud_base/quot;
Alan Cox216ba022008-10-13 10:40:19 +0100636 tty_encode_baud_rate(tty, baud, baud);
Jiri Slaby1c456072008-02-07 00:16:46 -0800637 } else {
638 quot = 0;
639 }
640
641 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
642 info->timeout += HZ / 50; /* Add .02 seconds of slop */
643
644 if (quot) {
645 info->MCR |= UART_MCR_DTR;
646 outb(info->MCR, info->ioaddr + UART_MCR);
647 } else {
648 info->MCR &= ~UART_MCR_DTR;
649 outb(info->MCR, info->ioaddr + UART_MCR);
650 return 0;
651 }
652
653 cval = inb(info->ioaddr + UART_LCR);
654
655 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
656
657 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
658 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
659 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
660
661#ifdef BOTHER
Alan Cox216ba022008-10-13 10:40:19 +0100662 if (C_BAUD(tty) == BOTHER) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800663 quot = info->baud_base % newspd;
664 quot *= 8;
665 if (quot % newspd > newspd / 2) {
666 quot /= newspd;
667 quot++;
668 } else
669 quot /= newspd;
670
Christoph Hellwig148ff862008-04-30 00:54:29 -0700671 mxser_set_must_enum_value(info->ioaddr, quot);
Jiri Slaby1c456072008-02-07 00:16:46 -0800672 } else
673#endif
Christoph Hellwig148ff862008-04-30 00:54:29 -0700674 mxser_set_must_enum_value(info->ioaddr, 0);
Jiri Slaby1c456072008-02-07 00:16:46 -0800675
676 return 0;
677}
678
679/*
680 * This routine is called to set the UART divisor registers to match
681 * the specified baud rate for a serial port.
682 */
Alan Cox216ba022008-10-13 10:40:19 +0100683static int mxser_change_speed(struct tty_struct *tty,
684 struct ktermios *old_termios)
Jiri Slaby1c456072008-02-07 00:16:46 -0800685{
Alan Cox216ba022008-10-13 10:40:19 +0100686 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800687 unsigned cflag, cval, fcr;
688 int ret = 0;
689 unsigned char status;
690
Alan Cox216ba022008-10-13 10:40:19 +0100691 if (!tty->termios)
Jiri Slaby1c456072008-02-07 00:16:46 -0800692 return ret;
Alan Cox216ba022008-10-13 10:40:19 +0100693 cflag = tty->termios->c_cflag;
694 if (!info->ioaddr)
Jiri Slaby1c456072008-02-07 00:16:46 -0800695 return ret;
696
Alan Cox216ba022008-10-13 10:40:19 +0100697 if (mxser_set_baud_method[tty->index] == 0)
698 mxser_set_baud(tty, tty_get_baud_rate(tty));
Jiri Slaby1c456072008-02-07 00:16:46 -0800699
700 /* byte size and parity */
701 switch (cflag & CSIZE) {
702 case CS5:
703 cval = 0x00;
704 break;
705 case CS6:
706 cval = 0x01;
707 break;
708 case CS7:
709 cval = 0x02;
710 break;
711 case CS8:
712 cval = 0x03;
713 break;
714 default:
715 cval = 0x00;
716 break; /* too keep GCC shut... */
717 }
718 if (cflag & CSTOPB)
719 cval |= 0x04;
720 if (cflag & PARENB)
721 cval |= UART_LCR_PARITY;
722 if (!(cflag & PARODD))
723 cval |= UART_LCR_EPAR;
724 if (cflag & CMSPAR)
725 cval |= UART_LCR_SPAR;
726
727 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
728 if (info->board->chip_flag) {
729 fcr = UART_FCR_ENABLE_FIFO;
730 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700731 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800732 } else
733 fcr = 0;
734 } else {
735 fcr = UART_FCR_ENABLE_FIFO;
736 if (info->board->chip_flag) {
737 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700738 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800739 } else {
740 switch (info->rx_trigger) {
741 case 1:
742 fcr |= UART_FCR_TRIGGER_1;
743 break;
744 case 4:
745 fcr |= UART_FCR_TRIGGER_4;
746 break;
747 case 8:
748 fcr |= UART_FCR_TRIGGER_8;
749 break;
750 default:
751 fcr |= UART_FCR_TRIGGER_14;
752 break;
753 }
754 }
755 }
756
757 /* CTS flow control flag and modem status interrupts */
758 info->IER &= ~UART_IER_MSI;
759 info->MCR &= ~UART_MCR_AFE;
760 if (cflag & CRTSCTS) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100761 info->port.flags |= ASYNC_CTS_FLOW;
Jiri Slaby1c456072008-02-07 00:16:46 -0800762 info->IER |= UART_IER_MSI;
763 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
764 info->MCR |= UART_MCR_AFE;
765 } else {
766 status = inb(info->ioaddr + UART_MSR);
Alan Cox216ba022008-10-13 10:40:19 +0100767 if (tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800768 if (status & UART_MSR_CTS) {
Alan Cox216ba022008-10-13 10:40:19 +0100769 tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800770 if (info->type != PORT_16550A &&
771 !info->board->chip_flag) {
772 outb(info->IER & ~UART_IER_THRI,
773 info->ioaddr +
774 UART_IER);
775 info->IER |= UART_IER_THRI;
776 outb(info->IER, info->ioaddr +
777 UART_IER);
778 }
Alan Cox216ba022008-10-13 10:40:19 +0100779 tty_wakeup(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800780 }
781 } else {
782 if (!(status & UART_MSR_CTS)) {
Alan Cox216ba022008-10-13 10:40:19 +0100783 tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800784 if ((info->type != PORT_16550A) &&
785 (!info->board->chip_flag)) {
786 info->IER &= ~UART_IER_THRI;
787 outb(info->IER, info->ioaddr +
788 UART_IER);
789 }
790 }
791 }
792 }
793 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100794 info->port.flags &= ~ASYNC_CTS_FLOW;
Jiri Slaby1c456072008-02-07 00:16:46 -0800795 }
796 outb(info->MCR, info->ioaddr + UART_MCR);
797 if (cflag & CLOCAL) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100798 info->port.flags &= ~ASYNC_CHECK_CD;
Jiri Slaby1c456072008-02-07 00:16:46 -0800799 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100800 info->port.flags |= ASYNC_CHECK_CD;
Jiri Slaby1c456072008-02-07 00:16:46 -0800801 info->IER |= UART_IER_MSI;
802 }
803 outb(info->IER, info->ioaddr + UART_IER);
804
805 /*
806 * Set up parity check flag
807 */
808 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
Alan Cox216ba022008-10-13 10:40:19 +0100809 if (I_INPCK(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800810 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Alan Cox216ba022008-10-13 10:40:19 +0100811 if (I_BRKINT(tty) || I_PARMRK(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800812 info->read_status_mask |= UART_LSR_BI;
813
814 info->ignore_status_mask = 0;
815
Alan Cox216ba022008-10-13 10:40:19 +0100816 if (I_IGNBRK(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800817 info->ignore_status_mask |= UART_LSR_BI;
818 info->read_status_mask |= UART_LSR_BI;
819 /*
820 * If we're ignore parity and break indicators, ignore
821 * overruns too. (For real raw support).
822 */
Alan Cox216ba022008-10-13 10:40:19 +0100823 if (I_IGNPAR(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800824 info->ignore_status_mask |=
825 UART_LSR_OE |
826 UART_LSR_PE |
827 UART_LSR_FE;
828 info->read_status_mask |=
829 UART_LSR_OE |
830 UART_LSR_PE |
831 UART_LSR_FE;
832 }
833 }
834 if (info->board->chip_flag) {
Alan Cox216ba022008-10-13 10:40:19 +0100835 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
836 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
837 if (I_IXON(tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700838 mxser_enable_must_rx_software_flow_control(
839 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800840 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700841 mxser_disable_must_rx_software_flow_control(
842 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800843 }
Alan Cox216ba022008-10-13 10:40:19 +0100844 if (I_IXOFF(tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700845 mxser_enable_must_tx_software_flow_control(
846 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800847 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700848 mxser_disable_must_tx_software_flow_control(
849 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800850 }
851 }
852
853
854 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
855 outb(cval, info->ioaddr + UART_LCR);
856
857 return ret;
858}
859
Alan Cox216ba022008-10-13 10:40:19 +0100860static void mxser_check_modem_status(struct tty_struct *tty,
861 struct mxser_port *port, int status)
Jiri Slaby1c456072008-02-07 00:16:46 -0800862{
863 /* update input line counters */
864 if (status & UART_MSR_TERI)
865 port->icount.rng++;
866 if (status & UART_MSR_DDSR)
867 port->icount.dsr++;
868 if (status & UART_MSR_DDCD)
869 port->icount.dcd++;
870 if (status & UART_MSR_DCTS)
871 port->icount.cts++;
872 port->mon_data.modem_status = status;
873 wake_up_interruptible(&port->delta_msr_wait);
874
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100875 if ((port->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800876 if (status & UART_MSR_DCD)
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100877 wake_up_interruptible(&port->port.open_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800878 }
879
Alan Cox216ba022008-10-13 10:40:19 +0100880 tty = tty_port_tty_get(&port->port);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100881 if (port->port.flags & ASYNC_CTS_FLOW) {
Alan Cox216ba022008-10-13 10:40:19 +0100882 if (tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800883 if (status & UART_MSR_CTS) {
Alan Cox216ba022008-10-13 10:40:19 +0100884 tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800885
886 if ((port->type != PORT_16550A) &&
887 (!port->board->chip_flag)) {
888 outb(port->IER & ~UART_IER_THRI,
889 port->ioaddr + UART_IER);
890 port->IER |= UART_IER_THRI;
891 outb(port->IER, port->ioaddr +
892 UART_IER);
893 }
Alan Cox216ba022008-10-13 10:40:19 +0100894 tty_wakeup(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800895 }
896 } else {
897 if (!(status & UART_MSR_CTS)) {
Alan Cox216ba022008-10-13 10:40:19 +0100898 tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800899 if (port->type != PORT_16550A &&
900 !port->board->chip_flag) {
901 port->IER &= ~UART_IER_THRI;
902 outb(port->IER, port->ioaddr +
903 UART_IER);
904 }
905 }
906 }
907 }
908}
909
Alan Cox216ba022008-10-13 10:40:19 +0100910static int mxser_startup(struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -0800911{
Alan Cox216ba022008-10-13 10:40:19 +0100912 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800913 unsigned long page;
914 unsigned long flags;
915
916 page = __get_free_page(GFP_KERNEL);
917 if (!page)
918 return -ENOMEM;
919
920 spin_lock_irqsave(&info->slock, flags);
921
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100922 if (info->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800923 free_page(page);
924 spin_unlock_irqrestore(&info->slock, flags);
925 return 0;
926 }
927
928 if (!info->ioaddr || !info->type) {
Alan Cox216ba022008-10-13 10:40:19 +0100929 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800930 free_page(page);
931 spin_unlock_irqrestore(&info->slock, flags);
932 return 0;
933 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100934 if (info->port.xmit_buf)
Jiri Slaby1c456072008-02-07 00:16:46 -0800935 free_page(page);
936 else
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100937 info->port.xmit_buf = (unsigned char *) page;
Jiri Slaby1c456072008-02-07 00:16:46 -0800938
939 /*
940 * Clear the FIFO buffers and disable them
941 * (they will be reenabled in mxser_change_speed())
942 */
943 if (info->board->chip_flag)
944 outb((UART_FCR_CLEAR_RCVR |
945 UART_FCR_CLEAR_XMIT |
946 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
947 else
948 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
949 info->ioaddr + UART_FCR);
950
951 /*
952 * At this point there's no way the LSR could still be 0xFF;
953 * if it is, then bail out, because there's likely no UART
954 * here.
955 */
956 if (inb(info->ioaddr + UART_LSR) == 0xff) {
957 spin_unlock_irqrestore(&info->slock, flags);
958 if (capable(CAP_SYS_ADMIN)) {
Alan Cox216ba022008-10-13 10:40:19 +0100959 if (tty)
960 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800961 return 0;
962 } else
963 return -ENODEV;
964 }
965
966 /*
967 * Clear the interrupt registers.
968 */
969 (void) inb(info->ioaddr + UART_LSR);
970 (void) inb(info->ioaddr + UART_RX);
971 (void) inb(info->ioaddr + UART_IIR);
972 (void) inb(info->ioaddr + UART_MSR);
973
974 /*
975 * Now, initialize the UART
976 */
977 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
978 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
979 outb(info->MCR, info->ioaddr + UART_MCR);
980
981 /*
982 * Finally, enable interrupts
983 */
984 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
985
986 if (info->board->chip_flag)
987 info->IER |= MOXA_MUST_IER_EGDAI;
988 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
989
990 /*
991 * And clear the interrupt registers again for luck.
992 */
993 (void) inb(info->ioaddr + UART_LSR);
994 (void) inb(info->ioaddr + UART_RX);
995 (void) inb(info->ioaddr + UART_IIR);
996 (void) inb(info->ioaddr + UART_MSR);
997
Alan Cox216ba022008-10-13 10:40:19 +0100998 clear_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800999 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1000
1001 /*
1002 * and set the speed of the serial port
1003 */
Alan Cox216ba022008-10-13 10:40:19 +01001004 mxser_change_speed(tty, NULL);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001005 info->port.flags |= ASYNC_INITIALIZED;
Jiri Slaby1c456072008-02-07 00:16:46 -08001006 spin_unlock_irqrestore(&info->slock, flags);
1007
1008 return 0;
1009}
1010
1011/*
1012 * This routine will shutdown a serial port; interrupts maybe disabled, and
1013 * DTR is dropped if the hangup on close termio flag is on.
1014 */
Alan Cox216ba022008-10-13 10:40:19 +01001015static void mxser_shutdown(struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -08001016{
Alan Cox216ba022008-10-13 10:40:19 +01001017 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -08001018 unsigned long flags;
1019
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001020 if (!(info->port.flags & ASYNC_INITIALIZED))
Jiri Slaby1c456072008-02-07 00:16:46 -08001021 return;
1022
1023 spin_lock_irqsave(&info->slock, flags);
1024
1025 /*
1026 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1027 * here so the queue might never be waken up
1028 */
1029 wake_up_interruptible(&info->delta_msr_wait);
1030
1031 /*
1032 * Free the IRQ, if necessary
1033 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001034 if (info->port.xmit_buf) {
1035 free_page((unsigned long) info->port.xmit_buf);
1036 info->port.xmit_buf = NULL;
Jiri Slaby1c456072008-02-07 00:16:46 -08001037 }
1038
1039 info->IER = 0;
1040 outb(0x00, info->ioaddr + UART_IER);
1041
Alan Cox216ba022008-10-13 10:40:19 +01001042 if (tty->termios->c_cflag & HUPCL)
Jiri Slaby1c456072008-02-07 00:16:46 -08001043 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1044 outb(info->MCR, info->ioaddr + UART_MCR);
1045
1046 /* clear Rx/Tx FIFO's */
1047 if (info->board->chip_flag)
1048 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
1049 MOXA_MUST_FCR_GDA_MODE_ENABLE,
1050 info->ioaddr + UART_FCR);
1051 else
1052 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
1053 info->ioaddr + UART_FCR);
1054
1055 /* read data port to reset things */
1056 (void) inb(info->ioaddr + UART_RX);
1057
Alan Cox216ba022008-10-13 10:40:19 +01001058 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001059
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001060 info->port.flags &= ~ASYNC_INITIALIZED;
Jiri Slaby1c456072008-02-07 00:16:46 -08001061
1062 if (info->board->chip_flag)
1063 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1064
1065 spin_unlock_irqrestore(&info->slock, flags);
1066}
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068/*
1069 * This routine is called whenever a serial port is opened. It
1070 * enables interrupts for a serial port, linking in its async structure into
1071 * the IRQ chain. It also performs the serial-specific
1072 * initialization for the tty structure.
1073 */
1074static int mxser_open(struct tty_struct *tty, struct file *filp)
1075{
Jiri Slaby1c456072008-02-07 00:16:46 -08001076 struct mxser_port *info;
1077 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 int retval, line;
1079
1080 line = tty->index;
1081 if (line == MXSER_PORTS)
1082 return 0;
1083 if (line < 0 || line > MXSER_PORTS)
1084 return -ENODEV;
Jiri Slaby1c456072008-02-07 00:16:46 -08001085 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1086 if (!info->ioaddr)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001087 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 tty->driver_data = info;
Alan Cox216ba022008-10-13 10:40:19 +01001090 tty_port_tty_set(&info->port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 /*
1092 * Start up serial port
1093 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001094 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001095 info->port.count++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001096 spin_unlock_irqrestore(&info->slock, flags);
Alan Cox216ba022008-10-13 10:40:19 +01001097 retval = mxser_startup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001099 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 retval = mxser_block_til_ready(tty, filp, info);
1102 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001103 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Cedric Le Goater8cddd702007-02-10 01:46:33 -08001105 /* unmark here for very high baud rate (ex. 921600 bps) used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 tty->low_latency = 1;
1107 return 0;
1108}
1109
Alan Cox978e5952008-04-30 00:53:59 -07001110static void mxser_flush_buffer(struct tty_struct *tty)
1111{
1112 struct mxser_port *info = tty->driver_data;
1113 char fcr;
1114 unsigned long flags;
1115
1116
1117 spin_lock_irqsave(&info->slock, flags);
1118 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1119
1120 fcr = inb(info->ioaddr + UART_FCR);
1121 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1122 info->ioaddr + UART_FCR);
1123 outb(fcr, info->ioaddr + UART_FCR);
1124
1125 spin_unlock_irqrestore(&info->slock, flags);
1126
1127 tty_wakeup(tty);
1128}
1129
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131/*
1132 * This routine is called when the serial port gets closed. First, we
1133 * wait for the last remaining data to be sent. Then, we unlink its
1134 * async structure from the interrupt chain if necessary, and we free
1135 * that IRQ if nothing is left in the chain.
1136 */
1137static void mxser_close(struct tty_struct *tty, struct file *filp)
1138{
Jiri Slaby1c456072008-02-07 00:16:46 -08001139 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 unsigned long timeout;
1142 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 if (tty->index == MXSER_PORTS)
1145 return;
1146 if (!info)
Kirill Smelkov6f08b722005-11-07 00:59:23 -08001147 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 spin_lock_irqsave(&info->slock, flags);
1150
1151 if (tty_hung_up_p(filp)) {
1152 spin_unlock_irqrestore(&info->slock, flags);
1153 return;
1154 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001155 if ((tty->count == 1) && (info->port.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /*
1157 * Uh, oh. tty->count is 1, which means that the tty
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001158 * structure will be freed. Info->port.count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * be one in these conditions. If it's greater than
1160 * one, we've got real problems, since it means the
1161 * serial port won't be shutdown.
1162 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001163 printk(KERN_ERR "mxser_close: bad serial port count; "
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001164 "tty->count is 1, info->port.count is %d\n", info->port.count);
1165 info->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001167 if (--info->port.count < 0) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001168 printk(KERN_ERR "mxser_close: bad serial port count for "
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001169 "ttys%d: %d\n", tty->index, info->port.count);
1170 info->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001172 if (info->port.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 spin_unlock_irqrestore(&info->slock, flags);
1174 return;
1175 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001176 info->port.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 spin_unlock_irqrestore(&info->slock, flags);
1178 /*
1179 * Save the termios structure, since this port may have
1180 * separate termios for callout and dialin.
1181 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001182 if (info->port.flags & ASYNC_NORMAL_ACTIVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 info->normal_termios = *tty->termios;
1184 /*
1185 * Now we wait for the transmit buffer to clear; and we notify
1186 * the line discipline to only process XON/XOFF characters.
1187 */
1188 tty->closing = 1;
Alan Cox44b7d1b2008-07-16 21:57:18 +01001189 if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
1190 tty_wait_until_sent(tty, info->port.closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /*
1192 * At this point we stop accepting input. To do this, we
1193 * disable the receive line status interrupts, and tell the
1194 * interrupt driver to stop checking the data ready bit in the
1195 * line status register.
1196 */
1197 info->IER &= ~UART_IER_RLSI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001198 if (info->board->chip_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001200
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001201 if (info->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001202 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /*
1204 * Before we drop DTR, make sure the UART transmitter
1205 * has completely drained; this is especially
1206 * important if there is a transmit FIFO!
1207 */
1208 timeout = jiffies + HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08001209 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001210 schedule_timeout_interruptible(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (time_after(jiffies, timeout))
1212 break;
1213 }
1214 }
Alan Cox216ba022008-10-13 10:40:19 +01001215 mxser_shutdown(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Alan Cox978e5952008-04-30 00:53:59 -07001217 mxser_flush_buffer(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001218 tty_ldisc_flush(tty);
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 tty->closing = 0;
Alan Cox216ba022008-10-13 10:40:19 +01001221 tty_port_tty_set(&info->port, NULL);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001222 if (info->port.blocked_open) {
Alan Cox44b7d1b2008-07-16 21:57:18 +01001223 if (info->port.close_delay)
1224 schedule_timeout_interruptible(info->port.close_delay);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001225 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001228 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1232{
1233 int c, total = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001234 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 unsigned long flags;
1236
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001237 if (!info->port.xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001241 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1242 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (c <= 0)
1244 break;
1245
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001246 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001248 info->xmit_head = (info->xmit_head + c) &
1249 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 info->xmit_cnt += c;
1251 spin_unlock_irqrestore(&info->slock, flags);
1252
1253 buf += c;
1254 count -= c;
1255 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
Jiri Slaby1c456072008-02-07 00:16:46 -08001258 if (info->xmit_cnt && !tty->stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001259 if (!tty->hw_stopped ||
1260 (info->type == PORT_16550A) ||
Jiri Slaby1c456072008-02-07 00:16:46 -08001261 (info->board->chip_flag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001263 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1264 UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001266 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 spin_unlock_irqrestore(&info->slock, flags);
1268 }
1269 }
1270 return total;
1271}
1272
Alan Cox0be2ead2008-04-30 00:54:03 -07001273static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Jiri Slaby1c456072008-02-07 00:16:46 -08001275 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 unsigned long flags;
1277
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001278 if (!info->port.xmit_buf)
Alan Cox0be2ead2008-04-30 00:54:03 -07001279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
Alan Cox0be2ead2008-04-30 00:54:03 -07001282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001285 info->port.xmit_buf[info->xmit_head++] = ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1287 info->xmit_cnt++;
1288 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001289 if (!tty->stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001290 if (!tty->hw_stopped ||
1291 (info->type == PORT_16550A) ||
Jiri Slaby1c456072008-02-07 00:16:46 -08001292 info->board->chip_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001294 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001296 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 spin_unlock_irqrestore(&info->slock, flags);
1298 }
1299 }
Alan Cox0be2ead2008-04-30 00:54:03 -07001300 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
1303
1304static void mxser_flush_chars(struct tty_struct *tty)
1305{
Jiri Slaby1c456072008-02-07 00:16:46 -08001306 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 unsigned long flags;
1308
Jiri Slabyace7dd92008-07-25 01:48:22 -07001309 if (info->xmit_cnt <= 0 || tty->stopped || !info->port.xmit_buf ||
1310 (tty->hw_stopped && info->type != PORT_16550A &&
1311 !info->board->chip_flag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return;
1313
1314 spin_lock_irqsave(&info->slock, flags);
1315
Jiri Slaby1c456072008-02-07 00:16:46 -08001316 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001318 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 spin_unlock_irqrestore(&info->slock, flags);
1321}
1322
1323static int mxser_write_room(struct tty_struct *tty)
1324{
Jiri Slaby1c456072008-02-07 00:16:46 -08001325 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 int ret;
1327
1328 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
Jiri Slabyace7dd92008-07-25 01:48:22 -07001329 return ret < 0 ? 0 : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
1332static int mxser_chars_in_buffer(struct tty_struct *tty)
1333{
Jiri Slaby1c456072008-02-07 00:16:46 -08001334 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return info->xmit_cnt;
1336}
1337
Jiri Slaby1c456072008-02-07 00:16:46 -08001338/*
1339 * ------------------------------------------------------------
1340 * friends of mxser_ioctl()
1341 * ------------------------------------------------------------
1342 */
Alan Cox216ba022008-10-13 10:40:19 +01001343static int mxser_get_serial_info(struct tty_struct *tty,
Jiri Slaby1c456072008-02-07 00:16:46 -08001344 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Alan Cox216ba022008-10-13 10:40:19 +01001346 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -08001347 struct serial_struct tmp = {
1348 .type = info->type,
Alan Cox216ba022008-10-13 10:40:19 +01001349 .line = tty->index,
Jiri Slaby1c456072008-02-07 00:16:46 -08001350 .port = info->ioaddr,
1351 .irq = info->board->irq,
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001352 .flags = info->port.flags,
Jiri Slaby1c456072008-02-07 00:16:46 -08001353 .baud_base = info->baud_base,
Alan Cox44b7d1b2008-07-16 21:57:18 +01001354 .close_delay = info->port.close_delay,
1355 .closing_wait = info->port.closing_wait,
Jiri Slaby1c456072008-02-07 00:16:46 -08001356 .custom_divisor = info->custom_divisor,
1357 .hub6 = 0
1358 };
1359 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1360 return -EFAULT;
1361 return 0;
1362}
1363
Alan Cox216ba022008-10-13 10:40:19 +01001364static int mxser_set_serial_info(struct tty_struct *tty,
Jiri Slaby1c456072008-02-07 00:16:46 -08001365 struct serial_struct __user *new_info)
1366{
Alan Cox216ba022008-10-13 10:40:19 +01001367 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -08001368 struct serial_struct new_serial;
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001369 speed_t baud;
Jiri Slaby1c456072008-02-07 00:16:46 -08001370 unsigned long sl_flags;
1371 unsigned int flags;
1372 int retval = 0;
1373
1374 if (!new_info || !info->ioaddr)
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001375 return -ENODEV;
Jiri Slaby1c456072008-02-07 00:16:46 -08001376 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1377 return -EFAULT;
1378
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001379 if (new_serial.irq != info->board->irq ||
1380 new_serial.port != info->ioaddr)
1381 return -EINVAL;
Jiri Slaby1c456072008-02-07 00:16:46 -08001382
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001383 flags = info->port.flags & ASYNC_SPD_MASK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001384
1385 if (!capable(CAP_SYS_ADMIN)) {
1386 if ((new_serial.baud_base != info->baud_base) ||
Alan Cox44b7d1b2008-07-16 21:57:18 +01001387 (new_serial.close_delay != info->port.close_delay) ||
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001388 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK)))
Jiri Slaby1c456072008-02-07 00:16:46 -08001389 return -EPERM;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001390 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
Jiri Slaby1c456072008-02-07 00:16:46 -08001391 (new_serial.flags & ASYNC_USR_MASK));
1392 } else {
1393 /*
1394 * OK, past this point, all the error checking has been done.
1395 * At this point, we start making changes.....
1396 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001397 info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
Jiri Slaby1c456072008-02-07 00:16:46 -08001398 (new_serial.flags & ASYNC_FLAGS));
Alan Cox44b7d1b2008-07-16 21:57:18 +01001399 info->port.close_delay = new_serial.close_delay * HZ / 100;
1400 info->port.closing_wait = new_serial.closing_wait * HZ / 100;
Alan Cox216ba022008-10-13 10:40:19 +01001401 tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY)
1402 ? 1 : 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001403 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001404 (new_serial.baud_base != info->baud_base ||
1405 new_serial.custom_divisor !=
1406 info->custom_divisor)) {
1407 baud = new_serial.baud_base / new_serial.custom_divisor;
Alan Cox216ba022008-10-13 10:40:19 +01001408 tty_encode_baud_rate(tty, baud, baud);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001409 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001410 }
1411
1412 info->type = new_serial.type;
1413
1414 process_txrx_fifo(info);
1415
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001416 if (info->port.flags & ASYNC_INITIALIZED) {
1417 if (flags != (info->port.flags & ASYNC_SPD_MASK)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001418 spin_lock_irqsave(&info->slock, sl_flags);
Alan Cox216ba022008-10-13 10:40:19 +01001419 mxser_change_speed(tty, NULL);
Jiri Slaby1c456072008-02-07 00:16:46 -08001420 spin_unlock_irqrestore(&info->slock, sl_flags);
1421 }
1422 } else
Alan Cox216ba022008-10-13 10:40:19 +01001423 retval = mxser_startup(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001424
1425 return retval;
1426}
1427
1428/*
1429 * mxser_get_lsr_info - get line status register info
1430 *
1431 * Purpose: Let user call ioctl() to get info when the UART physically
1432 * is emptied. On bus types like RS485, the transmitter must
1433 * release the bus after transmitting. This must be done when
1434 * the transmit shift register is empty, not be done when the
1435 * transmit holding register is empty. This functionality
1436 * allows an RS485 driver to be written in user space.
1437 */
1438static int mxser_get_lsr_info(struct mxser_port *info,
1439 unsigned int __user *value)
1440{
1441 unsigned char status;
1442 unsigned int result;
1443 unsigned long flags;
1444
1445 spin_lock_irqsave(&info->slock, flags);
1446 status = inb(info->ioaddr + UART_LSR);
1447 spin_unlock_irqrestore(&info->slock, flags);
1448 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1449 return put_user(result, value);
1450}
1451
Jiri Slaby1c456072008-02-07 00:16:46 -08001452static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1453{
1454 struct mxser_port *info = tty->driver_data;
1455 unsigned char control, status;
1456 unsigned long flags;
1457
1458
1459 if (tty->index == MXSER_PORTS)
1460 return -ENOIOCTLCMD;
1461 if (test_bit(TTY_IO_ERROR, &tty->flags))
1462 return -EIO;
1463
1464 control = info->MCR;
1465
1466 spin_lock_irqsave(&info->slock, flags);
1467 status = inb(info->ioaddr + UART_MSR);
1468 if (status & UART_MSR_ANY_DELTA)
Alan Cox216ba022008-10-13 10:40:19 +01001469 mxser_check_modem_status(tty, info, status);
Jiri Slaby1c456072008-02-07 00:16:46 -08001470 spin_unlock_irqrestore(&info->slock, flags);
1471 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1472 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1473 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1474 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1475 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1476 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1477}
1478
1479static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1480 unsigned int set, unsigned int clear)
1481{
1482 struct mxser_port *info = tty->driver_data;
1483 unsigned long flags;
1484
1485
1486 if (tty->index == MXSER_PORTS)
1487 return -ENOIOCTLCMD;
1488 if (test_bit(TTY_IO_ERROR, &tty->flags))
1489 return -EIO;
1490
1491 spin_lock_irqsave(&info->slock, flags);
1492
1493 if (set & TIOCM_RTS)
1494 info->MCR |= UART_MCR_RTS;
1495 if (set & TIOCM_DTR)
1496 info->MCR |= UART_MCR_DTR;
1497
1498 if (clear & TIOCM_RTS)
1499 info->MCR &= ~UART_MCR_RTS;
1500 if (clear & TIOCM_DTR)
1501 info->MCR &= ~UART_MCR_DTR;
1502
1503 outb(info->MCR, info->ioaddr + UART_MCR);
1504 spin_unlock_irqrestore(&info->slock, flags);
1505 return 0;
1506}
1507
1508static int __init mxser_program_mode(int port)
1509{
1510 int id, i, j, n;
1511
1512 outb(0, port);
1513 outb(0, port);
1514 outb(0, port);
1515 (void)inb(port);
1516 (void)inb(port);
1517 outb(0, port);
1518 (void)inb(port);
1519
1520 id = inb(port + 1) & 0x1F;
1521 if ((id != C168_ASIC_ID) &&
1522 (id != C104_ASIC_ID) &&
1523 (id != C102_ASIC_ID) &&
1524 (id != CI132_ASIC_ID) &&
1525 (id != CI134_ASIC_ID) &&
1526 (id != CI104J_ASIC_ID))
1527 return -1;
1528 for (i = 0, j = 0; i < 4; i++) {
1529 n = inb(port + 2);
1530 if (n == 'M') {
1531 j = 1;
1532 } else if ((j == 1) && (n == 1)) {
1533 j = 2;
1534 break;
1535 } else
1536 j = 0;
1537 }
1538 if (j != 2)
1539 id = -2;
1540 return id;
1541}
1542
1543static void __init mxser_normal_mode(int port)
1544{
1545 int i, n;
1546
1547 outb(0xA5, port + 1);
1548 outb(0x80, port + 3);
1549 outb(12, port + 0); /* 9600 bps */
1550 outb(0, port + 1);
1551 outb(0x03, port + 3); /* 8 data bits */
1552 outb(0x13, port + 4); /* loop back mode */
1553 for (i = 0; i < 16; i++) {
1554 n = inb(port + 5);
1555 if ((n & 0x61) == 0x60)
1556 break;
1557 if ((n & 1) == 1)
1558 (void)inb(port);
1559 }
1560 outb(0x00, port + 4);
1561}
1562
1563#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1564#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1565#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1566#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1567#define EN_CCMD 0x000 /* Chip's command register */
1568#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1569#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1570#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1571#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1572#define EN0_DCFG 0x00E /* Data configuration reg WR */
1573#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1574#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1575#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1576static int __init mxser_read_register(int port, unsigned short *regs)
1577{
1578 int i, k, value, id;
1579 unsigned int j;
1580
1581 id = mxser_program_mode(port);
1582 if (id < 0)
1583 return id;
1584 for (i = 0; i < 14; i++) {
1585 k = (i & 0x3F) | 0x180;
1586 for (j = 0x100; j > 0; j >>= 1) {
1587 outb(CHIP_CS, port);
1588 if (k & j) {
1589 outb(CHIP_CS | CHIP_DO, port);
1590 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1591 } else {
1592 outb(CHIP_CS, port);
1593 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1594 }
1595 }
1596 (void)inb(port);
1597 value = 0;
1598 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1599 outb(CHIP_CS, port);
1600 outb(CHIP_CS | CHIP_SK, port);
1601 if (inb(port) & CHIP_DI)
1602 value |= j;
1603 }
1604 regs[i] = value;
1605 outb(0, port);
1606 }
1607 mxser_normal_mode(port);
1608 return id;
1609}
1610
1611static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1612{
1613 struct mxser_port *port;
Alan Cox216ba022008-10-13 10:40:19 +01001614 struct tty_struct *tty;
Jiri Slaby1c456072008-02-07 00:16:46 -08001615 int result, status;
1616 unsigned int i, j;
Alan Cox9d6d1622008-04-30 00:53:20 -07001617 int ret = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001618
1619 switch (cmd) {
1620 case MOXA_GET_MAJOR:
Jiri Slaby8f3d1372008-07-29 22:33:38 -07001621 if (printk_ratelimit())
1622 printk(KERN_WARNING "mxser: '%s' uses deprecated ioctl "
1623 "%x (GET_MAJOR), fix your userspace\n",
1624 current->comm, cmd);
Jiri Slaby1c456072008-02-07 00:16:46 -08001625 return put_user(ttymajor, (int __user *)argp);
1626
1627 case MOXA_CHKPORTENABLE:
1628 result = 0;
Alan Cox9d6d1622008-04-30 00:53:20 -07001629 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001630 for (i = 0; i < MXSER_BOARDS; i++)
1631 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1632 if (mxser_boards[i].ports[j].ioaddr)
1633 result |= (1 << i);
Alan Cox9d6d1622008-04-30 00:53:20 -07001634 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001635 return put_user(result, (unsigned long __user *)argp);
1636 case MOXA_GETDATACOUNT:
Alan Cox9d6d1622008-04-30 00:53:20 -07001637 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001638 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
Alan Cox9d6d1622008-04-30 00:53:20 -07001639 ret = -EFAULT;
1640 unlock_kernel();
1641 return ret;
Jiri Slaby72800df2008-07-25 01:48:20 -07001642 case MOXA_GETMSTATUS: {
1643 struct mxser_mstatus ms, __user *msu = argp;
Alan Cox9d6d1622008-04-30 00:53:20 -07001644 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001645 for (i = 0; i < MXSER_BOARDS; i++)
1646 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1647 port = &mxser_boards[i].ports[j];
Jiri Slaby72800df2008-07-25 01:48:20 -07001648 memset(&ms, 0, sizeof(ms));
Jiri Slaby1c456072008-02-07 00:16:46 -08001649
Jiri Slaby72800df2008-07-25 01:48:20 -07001650 if (!port->ioaddr)
1651 goto copy;
Alan Cox216ba022008-10-13 10:40:19 +01001652
1653 tty = tty_port_tty_get(&port->port);
Jiri Slaby1c456072008-02-07 00:16:46 -08001654
Alan Cox216ba022008-10-13 10:40:19 +01001655 if (!tty || !tty->termios)
Jiri Slaby72800df2008-07-25 01:48:20 -07001656 ms.cflag = port->normal_termios.c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -08001657 else
Alan Cox216ba022008-10-13 10:40:19 +01001658 ms.cflag = tty->termios->c_cflag;
1659 tty_kref_put(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001660 status = inb(port->ioaddr + UART_MSR);
Jiri Slaby72800df2008-07-25 01:48:20 -07001661 if (status & UART_MSR_DCD)
1662 ms.dcd = 1;
1663 if (status & UART_MSR_DSR)
1664 ms.dsr = 1;
1665 if (status & UART_MSR_CTS)
1666 ms.cts = 1;
1667 copy:
1668 if (copy_to_user(msu, &ms, sizeof(ms))) {
1669 unlock_kernel();
1670 return -EFAULT;
1671 }
1672 msu++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001673 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001674 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001675 return 0;
Jiri Slaby72800df2008-07-25 01:48:20 -07001676 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001677 case MOXA_ASPP_MON_EXT: {
Jiri Slaby72800df2008-07-25 01:48:20 -07001678 struct mxser_mon_ext *me; /* it's 2k, stack unfriendly */
1679 unsigned int cflag, iflag, p;
1680 u8 opmode;
1681
1682 me = kzalloc(sizeof(*me), GFP_KERNEL);
1683 if (!me)
1684 return -ENOMEM;
Jiri Slaby1c456072008-02-07 00:16:46 -08001685
Alan Cox9d6d1622008-04-30 00:53:20 -07001686 lock_kernel();
Jiri Slaby72800df2008-07-25 01:48:20 -07001687 for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
1688 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
1689 if (p >= ARRAY_SIZE(me->rx_cnt)) {
1690 i = MXSER_BOARDS;
1691 break;
1692 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001693 port = &mxser_boards[i].ports[j];
1694 if (!port->ioaddr)
1695 continue;
1696
Jiri Slaby72800df2008-07-25 01:48:20 -07001697 status = mxser_get_msr(port->ioaddr, 0, p);
Jiri Slaby1c456072008-02-07 00:16:46 -08001698
1699 if (status & UART_MSR_TERI)
1700 port->icount.rng++;
1701 if (status & UART_MSR_DDSR)
1702 port->icount.dsr++;
1703 if (status & UART_MSR_DDCD)
1704 port->icount.dcd++;
1705 if (status & UART_MSR_DCTS)
1706 port->icount.cts++;
1707
1708 port->mon_data.modem_status = status;
Jiri Slaby72800df2008-07-25 01:48:20 -07001709 me->rx_cnt[p] = port->mon_data.rxcnt;
1710 me->tx_cnt[p] = port->mon_data.txcnt;
1711 me->up_rxcnt[p] = port->mon_data.up_rxcnt;
1712 me->up_txcnt[p] = port->mon_data.up_txcnt;
1713 me->modem_status[p] =
Jiri Slaby1c456072008-02-07 00:16:46 -08001714 port->mon_data.modem_status;
Alan Cox216ba022008-10-13 10:40:19 +01001715 tty = tty_port_tty_get(&port->port);
Jiri Slaby1c456072008-02-07 00:16:46 -08001716
Alan Cox216ba022008-10-13 10:40:19 +01001717 if (!tty || !tty->termios) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001718 cflag = port->normal_termios.c_cflag;
1719 iflag = port->normal_termios.c_iflag;
Alan Cox216ba022008-10-13 10:40:19 +01001720 me->baudrate[p] = tty_termios_baud_rate(&port->normal_termios);
Jiri Slaby1c456072008-02-07 00:16:46 -08001721 } else {
Alan Cox216ba022008-10-13 10:40:19 +01001722 cflag = tty->termios->c_cflag;
1723 iflag = tty->termios->c_iflag;
1724 me->baudrate[p] = tty_get_baud_rate(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001725 }
Alan Cox216ba022008-10-13 10:40:19 +01001726 tty_kref_put(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001727
Jiri Slaby72800df2008-07-25 01:48:20 -07001728 me->databits[p] = cflag & CSIZE;
1729 me->stopbits[p] = cflag & CSTOPB;
1730 me->parity[p] = cflag & (PARENB | PARODD |
1731 CMSPAR);
Jiri Slaby1c456072008-02-07 00:16:46 -08001732
1733 if (cflag & CRTSCTS)
Jiri Slaby72800df2008-07-25 01:48:20 -07001734 me->flowctrl[p] |= 0x03;
Jiri Slaby1c456072008-02-07 00:16:46 -08001735
1736 if (iflag & (IXON | IXOFF))
Jiri Slaby72800df2008-07-25 01:48:20 -07001737 me->flowctrl[p] |= 0x0C;
Jiri Slaby1c456072008-02-07 00:16:46 -08001738
1739 if (port->type == PORT_16550A)
Jiri Slaby72800df2008-07-25 01:48:20 -07001740 me->fifo[p] = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -08001741
Jiri Slaby72800df2008-07-25 01:48:20 -07001742 opmode = inb(port->opmode_ioaddr) >>
1743 ((p % 4) * 2);
Jiri Slaby1c456072008-02-07 00:16:46 -08001744 opmode &= OP_MODE_MASK;
Jiri Slaby72800df2008-07-25 01:48:20 -07001745 me->iftype[p] = opmode;
Jiri Slaby1c456072008-02-07 00:16:46 -08001746 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001747 }
1748 unlock_kernel();
Jiri Slaby72800df2008-07-25 01:48:20 -07001749 if (copy_to_user(argp, me, sizeof(*me)))
1750 ret = -EFAULT;
1751 kfree(me);
1752 return ret;
Alan Cox9d6d1622008-04-30 00:53:20 -07001753 }
1754 default:
Jiri Slaby1c456072008-02-07 00:16:46 -08001755 return -ENOIOCTLCMD;
1756 }
1757 return 0;
1758}
1759
1760static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1761 struct async_icount *cprev)
1762{
1763 struct async_icount cnow;
1764 unsigned long flags;
1765 int ret;
1766
1767 spin_lock_irqsave(&info->slock, flags);
1768 cnow = info->icount; /* atomic copy */
1769 spin_unlock_irqrestore(&info->slock, flags);
1770
1771 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1772 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1773 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1774 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1775
1776 *cprev = cnow;
1777
1778 return ret;
1779}
1780
1781static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1782 unsigned int cmd, unsigned long arg)
1783{
1784 struct mxser_port *info = tty->driver_data;
1785 struct async_icount cnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 unsigned long flags;
1787 void __user *argp = (void __user *)arg;
Jiri Slaby1c456072008-02-07 00:16:46 -08001788 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001791 return mxser_ioctl_special(cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001794 int p;
1795 unsigned long opmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1797 int shiftbit;
1798 unsigned char val, mask;
1799
Jiri Slaby1c456072008-02-07 00:16:46 -08001800 p = tty->index % 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (cmd == MOXA_SET_OP_MODE) {
1802 if (get_user(opmode, (int __user *) argp))
1803 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001804 if (opmode != RS232_MODE &&
1805 opmode != RS485_2WIRE_MODE &&
1806 opmode != RS422_MODE &&
1807 opmode != RS485_4WIRE_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return -EFAULT;
Alan Cox9d6d1622008-04-30 00:53:20 -07001809 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 mask = ModeMask[p];
1811 shiftbit = p * 2;
1812 val = inb(info->opmode_ioaddr);
1813 val &= mask;
1814 val |= (opmode << shiftbit);
1815 outb(val, info->opmode_ioaddr);
Alan Cox9d6d1622008-04-30 00:53:20 -07001816 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 } else {
Alan Cox9d6d1622008-04-30 00:53:20 -07001818 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 shiftbit = p * 2;
1820 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1821 opmode &= OP_MODE_MASK;
Alan Cox9d6d1622008-04-30 00:53:20 -07001822 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001823 if (put_user(opmode, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return -EFAULT;
1825 }
1826 return 0;
1827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Jiri Slaby1c456072008-02-07 00:16:46 -08001829 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1830 test_bit(TTY_IO_ERROR, &tty->flags))
1831 return -EIO;
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 case TIOCGSERIAL:
Alan Cox9d6d1622008-04-30 00:53:20 -07001835 lock_kernel();
Alan Cox216ba022008-10-13 10:40:19 +01001836 retval = mxser_get_serial_info(tty, argp);
Alan Cox9d6d1622008-04-30 00:53:20 -07001837 unlock_kernel();
1838 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 case TIOCSSERIAL:
Alan Cox9d6d1622008-04-30 00:53:20 -07001840 lock_kernel();
Alan Cox216ba022008-10-13 10:40:19 +01001841 retval = mxser_set_serial_info(tty, argp);
Alan Cox9d6d1622008-04-30 00:53:20 -07001842 unlock_kernel();
1843 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 case TIOCSERGETLSR: /* Get line status register */
Alan Cox9d6d1622008-04-30 00:53:20 -07001845 return mxser_get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 /*
1847 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1848 * - mask passed in arg for lines of interest
1849 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1850 * Caller should use TIOCGICOUNT to see which one it was
1851 */
Jiri Slabyfc838152007-04-23 14:41:04 -07001852 case TIOCMIWAIT:
1853 spin_lock_irqsave(&info->slock, flags);
1854 cnow = info->icount; /* note the counters on entry */
1855 spin_unlock_irqrestore(&info->slock, flags);
1856
Jiri Slaby1c456072008-02-07 00:16:46 -08001857 return wait_event_interruptible(info->delta_msr_wait,
1858 mxser_cflags_changed(info, arg, &cnow));
1859 /*
1860 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1861 * Return: write counters to the user passed counter struct
1862 * NB: both 1->0 and 0->1 transitions are counted except for
1863 * RI where only 0->1 is counted.
1864 */
Jiri Slaby41aee9a2008-07-25 01:48:19 -07001865 case TIOCGICOUNT: {
1866 struct serial_icounter_struct icnt = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 spin_lock_irqsave(&info->slock, flags);
1868 cnow = info->icount;
1869 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby41aee9a2008-07-25 01:48:19 -07001870
1871 icnt.frame = cnow.frame;
1872 icnt.brk = cnow.brk;
1873 icnt.overrun = cnow.overrun;
1874 icnt.buf_overrun = cnow.buf_overrun;
1875 icnt.parity = cnow.parity;
1876 icnt.rx = cnow.rx;
1877 icnt.tx = cnow.tx;
1878 icnt.cts = cnow.cts;
1879 icnt.dsr = cnow.dsr;
1880 icnt.rng = cnow.rng;
1881 icnt.dcd = cnow.dcd;
1882
1883 return copy_to_user(argp, &icnt, sizeof(icnt)) ? -EFAULT : 0;
1884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 case MOXA_HighSpeedOn:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001886 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
Jiri Slaby1c456072008-02-07 00:16:46 -08001887 case MOXA_SDS_RSTICOUNTER:
Alan Cox9d6d1622008-04-30 00:53:20 -07001888 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001889 info->mon_data.rxcnt = 0;
1890 info->mon_data.txcnt = 0;
Alan Cox9d6d1622008-04-30 00:53:20 -07001891 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
1893
1894 case MOXA_ASPP_OQUEUE:{
Jiri Slaby1c456072008-02-07 00:16:46 -08001895 int len, lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Alan Cox9d6d1622008-04-30 00:53:20 -07001897 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001898 len = mxser_chars_in_buffer(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001899 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001900 len += (lsr ? 0 : 1);
Alan Cox9d6d1622008-04-30 00:53:20 -07001901 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Jiri Slaby1c456072008-02-07 00:16:46 -08001903 return put_user(len, (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001905 case MOXA_ASPP_MON: {
1906 int mcr, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Alan Cox9d6d1622008-04-30 00:53:20 -07001908 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001909 status = mxser_get_msr(info->ioaddr, 1, tty->index);
Alan Cox216ba022008-10-13 10:40:19 +01001910 mxser_check_modem_status(tty, info, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Jiri Slaby1c456072008-02-07 00:16:46 -08001912 mcr = inb(info->ioaddr + UART_MCR);
1913 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1914 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1915 else
1916 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Jiri Slaby1c456072008-02-07 00:16:46 -08001918 if (mcr & MOXA_MUST_MCR_TX_XON)
1919 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1920 else
1921 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1922
Alan Cox216ba022008-10-13 10:40:19 +01001923 if (tty->hw_stopped)
Jiri Slaby1c456072008-02-07 00:16:46 -08001924 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1925 else
1926 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
Alan Cox9d6d1622008-04-30 00:53:20 -07001927 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001928 if (copy_to_user(argp, &info->mon_data,
1929 sizeof(struct mxser_mon)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001933 }
1934 case MOXA_ASPP_LSTATUS: {
1935 if (put_user(info->err_shadow, (unsigned char __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Jiri Slaby1c456072008-02-07 00:16:46 -08001938 info->err_shadow = 0;
1939 return 0;
1940 }
1941 case MOXA_SET_BAUD_METHOD: {
1942 int method;
1943
1944 if (get_user(method, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001946 mxser_set_baud_method[tty->index] = method;
1947 return put_user(method, (int __user *)argp);
1948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 default:
1950 return -ENOIOCTLCMD;
1951 }
1952 return 0;
1953}
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955static void mxser_stoprx(struct tty_struct *tty)
1956{
Jiri Slaby1c456072008-02-07 00:16:46 -08001957 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 info->ldisc_stop_rx = 1;
1960 if (I_IXOFF(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001961 if (info->board->chip_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001963 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 info->x_char = STOP_CHAR(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001966 outb(0, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001968 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970 }
1971
Alan Cox216ba022008-10-13 10:40:19 +01001972 if (tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001973 info->MCR &= ~UART_MCR_RTS;
1974 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
1976}
1977
1978/*
1979 * This routine is called by the upper-layer tty layer to signal that
1980 * incoming characters should be throttled.
1981 */
1982static void mxser_throttle(struct tty_struct *tty)
1983{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
1987static void mxser_unthrottle(struct tty_struct *tty)
1988{
Jiri Slaby1c456072008-02-07 00:16:46 -08001989 struct mxser_port *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001990
Jiri Slaby1c456072008-02-07 00:16:46 -08001991 /* startrx */
1992 info->ldisc_stop_rx = 0;
1993 if (I_IXOFF(tty)) {
1994 if (info->x_char)
1995 info->x_char = 0;
1996 else {
1997 if (info->board->chip_flag) {
1998 info->IER |= MOXA_MUST_RECV_ISR;
1999 outb(info->IER, info->ioaddr + UART_IER);
2000 } else {
2001 info->x_char = START_CHAR(tty);
2002 outb(0, info->ioaddr + UART_IER);
2003 info->IER |= UART_IER_THRI;
2004 outb(info->IER, info->ioaddr + UART_IER);
2005 }
2006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
2008
Alan Cox216ba022008-10-13 10:40:19 +01002009 if (tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002010 info->MCR |= UART_MCR_RTS;
2011 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013}
2014
2015/*
2016 * mxser_stop() and mxser_start()
2017 *
2018 * This routines are called before setting or resetting tty->stopped.
2019 * They enable or disable transmitter interrupts, as necessary.
2020 */
2021static void mxser_stop(struct tty_struct *tty)
2022{
Jiri Slaby1c456072008-02-07 00:16:46 -08002023 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 unsigned long flags;
2025
2026 spin_lock_irqsave(&info->slock, flags);
2027 if (info->IER & UART_IER_THRI) {
2028 info->IER &= ~UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08002029 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
2031 spin_unlock_irqrestore(&info->slock, flags);
2032}
2033
2034static void mxser_start(struct tty_struct *tty)
2035{
Jiri Slaby1c456072008-02-07 00:16:46 -08002036 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 unsigned long flags;
2038
2039 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002040 if (info->xmit_cnt && info->port.xmit_buf) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002041 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08002043 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045 spin_unlock_irqrestore(&info->slock, flags);
2046}
2047
Jiri Slaby1c456072008-02-07 00:16:46 -08002048static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2049{
2050 struct mxser_port *info = tty->driver_data;
2051 unsigned long flags;
2052
2053 spin_lock_irqsave(&info->slock, flags);
Alan Cox216ba022008-10-13 10:40:19 +01002054 mxser_change_speed(tty, old_termios);
Jiri Slaby1c456072008-02-07 00:16:46 -08002055 spin_unlock_irqrestore(&info->slock, flags);
2056
2057 if ((old_termios->c_cflag & CRTSCTS) &&
2058 !(tty->termios->c_cflag & CRTSCTS)) {
2059 tty->hw_stopped = 0;
2060 mxser_start(tty);
2061 }
2062
2063 /* Handle sw stopped */
2064 if ((old_termios->c_iflag & IXON) &&
2065 !(tty->termios->c_iflag & IXON)) {
2066 tty->stopped = 0;
2067
2068 if (info->board->chip_flag) {
2069 spin_lock_irqsave(&info->slock, flags);
Christoph Hellwig148ff862008-04-30 00:54:29 -07002070 mxser_disable_must_rx_software_flow_control(
2071 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -08002072 spin_unlock_irqrestore(&info->slock, flags);
2073 }
2074
2075 mxser_start(tty);
2076 }
2077}
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079/*
2080 * mxser_wait_until_sent() --- wait until the transmitter is empty
2081 */
2082static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2083{
Jiri Slaby1c456072008-02-07 00:16:46 -08002084 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 unsigned long orig_jiffies, char_time;
2086 int lsr;
2087
2088 if (info->type == PORT_UNKNOWN)
2089 return;
2090
2091 if (info->xmit_fifo_size == 0)
2092 return; /* Just in case.... */
2093
2094 orig_jiffies = jiffies;
2095 /*
2096 * Set the check interval to be 1/5 of the estimated time to
2097 * send a single character, and make it at least 1. The check
2098 * interval should also be less than the timeout.
2099 *
2100 * Note: we have to use pretty tight timings here to satisfy
2101 * the NIST-PCTS.
2102 */
2103 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2104 char_time = char_time / 5;
2105 if (char_time == 0)
2106 char_time = 1;
2107 if (timeout && timeout < char_time)
2108 char_time = timeout;
2109 /*
2110 * If the transmitter hasn't cleared in twice the approximate
2111 * amount of time to send the entire FIFO, it probably won't
2112 * ever clear. This assumes the UART isn't doing flow
2113 * control, which is currently the case. Hence, if it ever
2114 * takes longer than info->timeout, this is probably due to a
2115 * UART bug of some kind. So, we clamp the timeout parameter at
2116 * 2*info->timeout.
2117 */
2118 if (!timeout || timeout > 2 * info->timeout)
2119 timeout = 2 * info->timeout;
2120#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002121 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2122 timeout, char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 printk("jiff=%lu...", jiffies);
2124#endif
Alan Cox978e5952008-04-30 00:53:59 -07002125 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08002126 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2128 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2129#endif
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07002130 schedule_timeout_interruptible(char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (signal_pending(current))
2132 break;
2133 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2134 break;
2135 }
2136 set_current_state(TASK_RUNNING);
Alan Cox978e5952008-04-30 00:53:59 -07002137 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
2139#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2140 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2141#endif
2142}
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144/*
2145 * This routine is called by tty_hangup() when a hangup is signaled.
2146 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002147static void mxser_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Jiri Slaby1c456072008-02-07 00:16:46 -08002149 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 mxser_flush_buffer(tty);
Alan Cox216ba022008-10-13 10:40:19 +01002152 mxser_shutdown(tty);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002153 info->port.count = 0;
2154 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
Alan Cox216ba022008-10-13 10:40:19 +01002155 tty_port_tty_set(&info->port, NULL);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002156 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159/*
2160 * mxser_rs_break() --- routine which turns the break handling on or off
2161 */
Alan Cox9e989662008-07-22 11:18:03 +01002162static int mxser_rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Jiri Slaby1c456072008-02-07 00:16:46 -08002164 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 unsigned long flags;
2166
2167 spin_lock_irqsave(&info->slock, flags);
2168 if (break_state == -1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002169 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2170 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002172 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2173 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 spin_unlock_irqrestore(&info->slock, flags);
Alan Cox9e989662008-07-22 11:18:03 +01002175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
Alan Cox216ba022008-10-13 10:40:19 +01002178static void mxser_receive_chars(struct tty_struct *tty,
2179 struct mxser_port *port, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 unsigned char ch, gdl;
2182 int ignored = 0;
2183 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 int recv_room;
2185 int max = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Alan Cox33f0f882006-01-09 20:54:13 -08002187 recv_room = tty->receive_room;
Alan Cox216ba022008-10-13 10:40:19 +01002188 if (recv_room == 0 && !port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 mxser_stoprx(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08002190 if (port->board->chip_flag != MOXA_OTHER_UART) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Jiri Slaby1c456072008-02-07 00:16:46 -08002192 if (*status & UART_LSR_SPECIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 goto intr_old;
Jiri Slaby1c456072008-02-07 00:16:46 -08002194 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002195 (*status & MOXA_MUST_LSR_RERR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 goto intr_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (*status & MOXA_MUST_LSR_RERR)
2198 goto intr_old;
2199
Jiri Slaby1c456072008-02-07 00:16:46 -08002200 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Jiri Slaby1c456072008-02-07 00:16:46 -08002202 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 gdl &= MOXA_MUST_GDL_MASK;
2204 if (gdl >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002205 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
2208 while (gdl--) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002209 ch = inb(port->ioaddr + UART_RX);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002210 tty_insert_flip_char(tty, ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
2213 goto end_intr;
2214 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002215intr_old:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
2217 do {
2218 if (max-- < 0)
2219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Jiri Slaby1c456072008-02-07 00:16:46 -08002221 ch = inb(port->ioaddr + UART_RX);
2222 if (port->board->chip_flag && (*status & UART_LSR_OE))
2223 outb(0x23, port->ioaddr + UART_FCR);
2224 *status &= port->read_status_mask;
2225 if (*status & port->ignore_status_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (++ignored > 100)
2227 break;
2228 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002229 char flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 if (*status & UART_LSR_SPECIAL) {
2231 if (*status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002232 flag = TTY_BREAK;
Jiri Slaby1c456072008-02-07 00:16:46 -08002233 port->icount.brk++;
2234
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002235 if (port->port.flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 do_SAK(tty);
2237 } else if (*status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002238 flag = TTY_PARITY;
Jiri Slaby1c456072008-02-07 00:16:46 -08002239 port->icount.parity++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 } else if (*status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002241 flag = TTY_FRAME;
Jiri Slaby1c456072008-02-07 00:16:46 -08002242 port->icount.frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 } else if (*status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002244 flag = TTY_OVERRUN;
Jiri Slaby1c456072008-02-07 00:16:46 -08002245 port->icount.overrun++;
2246 } else
2247 flag = TTY_BREAK;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002248 }
2249 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 cnt++;
2251 if (cnt >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002252 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 break;
2255 }
2256
2257 }
2258
Jiri Slaby1c456072008-02-07 00:16:46 -08002259 if (port->board->chip_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Jiri Slaby1c456072008-02-07 00:16:46 -08002262 *status = inb(port->ioaddr + UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 } while (*status & UART_LSR_DR);
2264
Jiri Slaby1c456072008-02-07 00:16:46 -08002265end_intr:
Alan Cox216ba022008-10-13 10:40:19 +01002266 mxvar_log.rxcnt[tty->index] += cnt;
Jiri Slaby1c456072008-02-07 00:16:46 -08002267 port->mon_data.rxcnt += cnt;
2268 port->mon_data.up_rxcnt += cnt;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002269
Jiri Slaby1c456072008-02-07 00:16:46 -08002270 /*
2271 * We are called from an interrupt context with &port->slock
2272 * being held. Drop it temporarily in order to prevent
2273 * recursive locking.
2274 */
2275 spin_unlock(&port->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 tty_flip_buffer_push(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08002277 spin_lock(&port->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
Alan Cox216ba022008-10-13 10:40:19 +01002280static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
2282 int count, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Jiri Slaby1c456072008-02-07 00:16:46 -08002284 if (port->x_char) {
2285 outb(port->x_char, port->ioaddr + UART_TX);
2286 port->x_char = 0;
Alan Cox216ba022008-10-13 10:40:19 +01002287 mxvar_log.txcnt[tty->index]++;
Jiri Slaby1c456072008-02-07 00:16:46 -08002288 port->mon_data.txcnt++;
2289 port->mon_data.up_txcnt++;
2290 port->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return;
2292 }
2293
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002294 if (port->port.xmit_buf == NULL)
Jiri Slaby1c456072008-02-07 00:16:46 -08002295 return;
2296
Alan Cox216ba022008-10-13 10:40:19 +01002297 if (port->xmit_cnt <= 0 || tty->stopped ||
2298 (tty->hw_stopped &&
Jiri Slaby1c456072008-02-07 00:16:46 -08002299 (port->type != PORT_16550A) &&
2300 (!port->board->chip_flag))) {
2301 port->IER &= ~UART_IER_THRI;
2302 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return;
2304 }
2305
Jiri Slaby1c456072008-02-07 00:16:46 -08002306 cnt = port->xmit_cnt;
2307 count = port->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 do {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002309 outb(port->port.xmit_buf[port->xmit_tail++],
Jiri Slaby1c456072008-02-07 00:16:46 -08002310 port->ioaddr + UART_TX);
2311 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2312 if (--port->xmit_cnt <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 break;
2314 } while (--count > 0);
Alan Cox216ba022008-10-13 10:40:19 +01002315 mxvar_log.txcnt[tty->index] += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Jiri Slaby1c456072008-02-07 00:16:46 -08002317 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2318 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2319 port->icount.tx += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Alan Cox216ba022008-10-13 10:40:19 +01002321 if (port->xmit_cnt < WAKEUP_CHARS && tty)
2322 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Jiri Slaby1c456072008-02-07 00:16:46 -08002324 if (port->xmit_cnt <= 0) {
2325 port->IER &= ~UART_IER_THRI;
2326 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
2330/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002331 * This is the serial driver's generic interrupt routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002333static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
Jiri Slaby1c456072008-02-07 00:16:46 -08002335 int status, iir, i;
2336 struct mxser_board *brd = NULL;
2337 struct mxser_port *port;
2338 int max, irqbits, bits, msr;
2339 unsigned int int_cnt, pass_counter = 0;
2340 int handled = IRQ_NONE;
Alan Cox216ba022008-10-13 10:40:19 +01002341 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Jiri Slaby1c456072008-02-07 00:16:46 -08002343 for (i = 0; i < MXSER_BOARDS; i++)
2344 if (dev_id == &mxser_boards[i]) {
2345 brd = dev_id;
2346 break;
2347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Jiri Slaby1c456072008-02-07 00:16:46 -08002349 if (i == MXSER_BOARDS)
2350 goto irq_stop;
2351 if (brd == NULL)
2352 goto irq_stop;
2353 max = brd->info->nports;
2354 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2355 irqbits = inb(brd->vector) & brd->vector_mask;
2356 if (irqbits == brd->vector_mask)
2357 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Jiri Slaby1c456072008-02-07 00:16:46 -08002359 handled = IRQ_HANDLED;
2360 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2361 if (irqbits == brd->vector_mask)
2362 break;
2363 if (bits & irqbits)
2364 continue;
2365 port = &brd->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Jiri Slaby1c456072008-02-07 00:16:46 -08002367 int_cnt = 0;
2368 spin_lock(&port->slock);
2369 do {
2370 iir = inb(port->ioaddr + UART_IIR);
2371 if (iir & UART_IIR_NO_INT)
2372 break;
2373 iir &= MOXA_MUST_IIR_MASK;
Alan Cox216ba022008-10-13 10:40:19 +01002374 tty = tty_port_tty_get(&port->port);
2375 if (!tty ||
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002376 (port->port.flags & ASYNC_CLOSING) ||
2377 !(port->port.flags &
Jiri Slaby1c456072008-02-07 00:16:46 -08002378 ASYNC_INITIALIZED)) {
2379 status = inb(port->ioaddr + UART_LSR);
2380 outb(0x27, port->ioaddr + UART_FCR);
2381 inb(port->ioaddr + UART_MSR);
Alan Cox216ba022008-10-13 10:40:19 +01002382 tty_kref_put(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08002383 break;
2384 }
2385
2386 status = inb(port->ioaddr + UART_LSR);
2387
2388 if (status & UART_LSR_PE)
2389 port->err_shadow |= NPPI_NOTIFY_PARITY;
2390 if (status & UART_LSR_FE)
2391 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2392 if (status & UART_LSR_OE)
2393 port->err_shadow |=
2394 NPPI_NOTIFY_HW_OVERRUN;
2395 if (status & UART_LSR_BI)
2396 port->err_shadow |= NPPI_NOTIFY_BREAK;
2397
2398 if (port->board->chip_flag) {
2399 if (iir == MOXA_MUST_IIR_GDA ||
2400 iir == MOXA_MUST_IIR_RDA ||
2401 iir == MOXA_MUST_IIR_RTO ||
2402 iir == MOXA_MUST_IIR_LSR)
Alan Cox216ba022008-10-13 10:40:19 +01002403 mxser_receive_chars(tty, port,
Jiri Slaby1c456072008-02-07 00:16:46 -08002404 &status);
2405
2406 } else {
2407 status &= port->read_status_mask;
2408 if (status & UART_LSR_DR)
Alan Cox216ba022008-10-13 10:40:19 +01002409 mxser_receive_chars(tty, port,
Jiri Slaby1c456072008-02-07 00:16:46 -08002410 &status);
2411 }
2412 msr = inb(port->ioaddr + UART_MSR);
2413 if (msr & UART_MSR_ANY_DELTA)
Alan Cox216ba022008-10-13 10:40:19 +01002414 mxser_check_modem_status(tty, port, msr);
Jiri Slaby1c456072008-02-07 00:16:46 -08002415
2416 if (port->board->chip_flag) {
2417 if (iir == 0x02 && (status &
2418 UART_LSR_THRE))
Alan Cox216ba022008-10-13 10:40:19 +01002419 mxser_transmit_chars(tty, port);
Jiri Slaby1c456072008-02-07 00:16:46 -08002420 } else {
2421 if (status & UART_LSR_THRE)
Alan Cox216ba022008-10-13 10:40:19 +01002422 mxser_transmit_chars(tty, port);
Jiri Slaby1c456072008-02-07 00:16:46 -08002423 }
Alan Cox216ba022008-10-13 10:40:19 +01002424 tty_kref_put(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08002425 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2426 spin_unlock(&port->slock);
2427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
2429
Jiri Slaby1c456072008-02-07 00:16:46 -08002430irq_stop:
2431 return handled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
Jiri Slaby1c456072008-02-07 00:16:46 -08002434static const struct tty_operations mxser_ops = {
2435 .open = mxser_open,
2436 .close = mxser_close,
2437 .write = mxser_write,
2438 .put_char = mxser_put_char,
2439 .flush_chars = mxser_flush_chars,
2440 .write_room = mxser_write_room,
2441 .chars_in_buffer = mxser_chars_in_buffer,
2442 .flush_buffer = mxser_flush_buffer,
2443 .ioctl = mxser_ioctl,
2444 .throttle = mxser_throttle,
2445 .unthrottle = mxser_unthrottle,
2446 .set_termios = mxser_set_termios,
2447 .stop = mxser_stop,
2448 .start = mxser_start,
2449 .hangup = mxser_hangup,
2450 .break_ctl = mxser_rs_break,
2451 .wait_until_sent = mxser_wait_until_sent,
2452 .tiocmget = mxser_tiocmget,
2453 .tiocmset = mxser_tiocmset,
2454};
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002457 * The MOXA Smartio/Industio serial driver boot-time initialization code!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002459
2460static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2461 unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Jiri Slaby1c456072008-02-07 00:16:46 -08002463 if (irq)
2464 free_irq(brd->irq, brd);
2465 if (pdev != NULL) { /* PCI */
2466#ifdef CONFIG_PCI
2467 pci_release_region(pdev, 2);
2468 pci_release_region(pdev, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 } else {
Jiri Slaby1c456072008-02-07 00:16:46 -08002471 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2472 release_region(brd->vector, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
Jiri Slaby1c456072008-02-07 00:16:46 -08002476static int __devinit mxser_initbrd(struct mxser_board *brd,
2477 struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Jiri Slaby1c456072008-02-07 00:16:46 -08002479 struct mxser_port *info;
2480 unsigned int i;
2481 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Jiri Slaby83766bc2008-07-25 01:48:21 -07002483 printk(KERN_INFO "mxser: max. baud rate = %d bps\n",
2484 brd->ports[0].max_baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Jiri Slaby1c456072008-02-07 00:16:46 -08002486 for (i = 0; i < brd->info->nports; i++) {
2487 info = &brd->ports[i];
Alan Cox44b7d1b2008-07-16 21:57:18 +01002488 tty_port_init(&info->port);
Jiri Slaby1c456072008-02-07 00:16:46 -08002489 info->board = brd;
2490 info->stop_rx = 0;
2491 info->ldisc_stop_rx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Jiri Slaby1c456072008-02-07 00:16:46 -08002493 /* Enhance mode enabled here */
2494 if (brd->chip_flag != MOXA_OTHER_UART)
Christoph Hellwig148ff862008-04-30 00:54:29 -07002495 mxser_enable_must_enchance_mode(info->ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002497 info->port.flags = ASYNC_SHARE_IRQ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002498 info->type = brd->uart_type;
2499
2500 process_txrx_fifo(info);
2501
2502 info->custom_divisor = info->baud_base * 16;
Alan Cox44b7d1b2008-07-16 21:57:18 +01002503 info->port.close_delay = 5 * HZ / 10;
2504 info->port.closing_wait = 30 * HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002505 info->normal_termios = mxvar_sdriver->init_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -08002506 init_waitqueue_head(&info->delta_msr_wait);
2507 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2508 info->err_shadow = 0;
2509 spin_lock_init(&info->slock);
2510
2511 /* before set INT ISR, disable all int */
2512 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2513 info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 }
2515
Jiri Slaby1c456072008-02-07 00:16:46 -08002516 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2517 brd);
2518 if (retval) {
2519 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2520 "conflict with another device.\n",
2521 brd->info->name, brd->irq);
2522 /* We hold resources, we need to release them. */
2523 mxser_release_res(brd, pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002525 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
Jiri Slaby1c456072008-02-07 00:16:46 -08002528static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
2530 int id, i, bits;
2531 unsigned short regs[16], irq;
2532 unsigned char scratch, scratch2;
2533
Jiri Slaby1c456072008-02-07 00:16:46 -08002534 brd->chip_flag = MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 id = mxser_read_register(cap, regs);
Jiri Slaby1c456072008-02-07 00:16:46 -08002537 switch (id) {
2538 case C168_ASIC_ID:
2539 brd->info = &mxser_cards[0];
2540 break;
2541 case C104_ASIC_ID:
2542 brd->info = &mxser_cards[1];
2543 break;
2544 case CI104J_ASIC_ID:
2545 brd->info = &mxser_cards[2];
2546 break;
2547 case C102_ASIC_ID:
2548 brd->info = &mxser_cards[5];
2549 break;
2550 case CI132_ASIC_ID:
2551 brd->info = &mxser_cards[6];
2552 break;
2553 case CI134_ASIC_ID:
2554 brd->info = &mxser_cards[7];
2555 break;
2556 default:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002557 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
2560 irq = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002561 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2562 Flag-hack checks if configuration should be read as 2-port here. */
2563 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 irq = regs[9] & 0xF000;
2565 irq = irq | (irq >> 4);
2566 if (irq != (regs[9] & 0xFF00))
Jiri Slaby83766bc2008-07-25 01:48:21 -07002567 goto err_irqconflict;
Jiri Slaby1c456072008-02-07 00:16:46 -08002568 } else if (brd->info->nports == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 irq = regs[9] & 0xF000;
2570 irq = irq | (irq >> 4);
2571 irq = irq | (irq >> 8);
2572 if (irq != regs[9])
Jiri Slaby83766bc2008-07-25 01:48:21 -07002573 goto err_irqconflict;
Jiri Slaby1c456072008-02-07 00:16:46 -08002574 } else if (brd->info->nports == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 irq = regs[9] & 0xF000;
2576 irq = irq | (irq >> 4);
2577 irq = irq | (irq >> 8);
2578 if ((irq != regs[9]) || (irq != regs[10]))
Jiri Slaby83766bc2008-07-25 01:48:21 -07002579 goto err_irqconflict;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
2581
Jiri Slaby83766bc2008-07-25 01:48:21 -07002582 if (!irq) {
2583 printk(KERN_ERR "mxser: interrupt number unset\n");
2584 return -EIO;
2585 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002586 brd->irq = ((int)(irq & 0xF000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 for (i = 0; i < 8; i++)
Jiri Slaby1c456072008-02-07 00:16:46 -08002588 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002589 if ((regs[12] & 0x80) == 0) {
2590 printk(KERN_ERR "mxser: invalid interrupt vector\n");
2591 return -EIO;
2592 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002593 brd->vector = (int)regs[11]; /* interrupt vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (id == 1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002595 brd->vector_mask = 0x00FF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002597 brd->vector_mask = 0x000F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2599 if (regs[12] & bits) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002600 brd->ports[i].baud_base = 921600;
2601 brd->ports[i].max_baud = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 } else {
Jiri Slaby1c456072008-02-07 00:16:46 -08002603 brd->ports[i].baud_base = 115200;
2604 brd->ports[i].max_baud = 115200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
2606 }
2607 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2608 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2609 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2610 outb(scratch2, cap + UART_LCR);
2611 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2612 scratch = inb(cap + UART_IIR);
2613
2614 if (scratch & 0xC0)
Jiri Slaby1c456072008-02-07 00:16:46 -08002615 brd->uart_type = PORT_16550A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002617 brd->uart_type = PORT_16450;
2618 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
Jiri Slaby83766bc2008-07-25 01:48:21 -07002619 "mxser(IO)")) {
2620 printk(KERN_ERR "mxser: can't request ports I/O region: "
2621 "0x%.8lx-0x%.8lx\n",
2622 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2623 8 * brd->info->nports - 1);
2624 return -EIO;
2625 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002626 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2627 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby83766bc2008-07-25 01:48:21 -07002628 printk(KERN_ERR "mxser: can't request interrupt vector region: "
2629 "0x%.8lx-0x%.8lx\n",
2630 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2631 8 * brd->info->nports - 1);
2632 return -EIO;
Jiri Slaby1c456072008-02-07 00:16:46 -08002633 }
2634 return brd->info->nports;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002635
2636err_irqconflict:
2637 printk(KERN_ERR "mxser: invalid interrupt number\n");
2638 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
2640
Jiri Slaby1c456072008-02-07 00:16:46 -08002641static int __devinit mxser_probe(struct pci_dev *pdev,
2642 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643{
Jiri Slaby1c456072008-02-07 00:16:46 -08002644#ifdef CONFIG_PCI
2645 struct mxser_board *brd;
2646 unsigned int i, j;
2647 unsigned long ioaddress;
2648 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Jiri Slaby1c456072008-02-07 00:16:46 -08002650 for (i = 0; i < MXSER_BOARDS; i++)
2651 if (mxser_boards[i].info == NULL)
2652 break;
2653
2654 if (i >= MXSER_BOARDS) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002655 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
2656 "not configured\n", MXSER_BOARDS);
Jiri Slaby1c456072008-02-07 00:16:46 -08002657 goto err;
2658 }
2659
2660 brd = &mxser_boards[i];
2661 brd->idx = i * MXSER_PORTS_PER_BOARD;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002662 dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
Jiri Slaby1c456072008-02-07 00:16:46 -08002663 mxser_cards[ent->driver_data].name,
2664 pdev->bus->number, PCI_SLOT(pdev->devfn));
2665
2666 retval = pci_enable_device(pdev);
2667 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002668 dev_err(&pdev->dev, "PCI enable failed\n");
Jiri Slaby1c456072008-02-07 00:16:46 -08002669 goto err;
2670 }
2671
2672 /* io address */
2673 ioaddress = pci_resource_start(pdev, 2);
2674 retval = pci_request_region(pdev, 2, "mxser(IO)");
2675 if (retval)
2676 goto err;
2677
2678 brd->info = &mxser_cards[ent->driver_data];
2679 for (i = 0; i < brd->info->nports; i++)
2680 brd->ports[i].ioaddr = ioaddress + 8 * i;
2681
2682 /* vector */
2683 ioaddress = pci_resource_start(pdev, 3);
2684 retval = pci_request_region(pdev, 3, "mxser(vector)");
2685 if (retval)
2686 goto err_relio;
2687 brd->vector = ioaddress;
2688
2689 /* irq */
2690 brd->irq = pdev->irq;
2691
2692 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2693 brd->uart_type = PORT_16550A;
2694 brd->vector_mask = 0;
2695
2696 for (i = 0; i < brd->info->nports; i++) {
2697 for (j = 0; j < UART_INFO_NUM; j++) {
2698 if (Gpci_uart_info[j].type == brd->chip_flag) {
2699 brd->ports[i].max_baud =
2700 Gpci_uart_info[j].max_baud;
2701
2702 /* exception....CP-102 */
2703 if (brd->info->flags & MXSER_HIGHBAUD)
2704 brd->ports[i].max_baud = 921600;
2705 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 }
2707 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002708 }
2709
2710 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2711 for (i = 0; i < brd->info->nports; i++) {
2712 if (i < 4)
2713 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2714 else
2715 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002717 outb(0, ioaddress + 4); /* default set to RS232 mode */
2718 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002720
2721 for (i = 0; i < brd->info->nports; i++) {
2722 brd->vector_mask |= (1 << i);
2723 brd->ports[i].baud_base = 921600;
2724 }
2725
2726 /* mxser_initbrd will hook ISR. */
2727 retval = mxser_initbrd(brd, pdev);
2728 if (retval)
2729 goto err_null;
2730
2731 for (i = 0; i < brd->info->nports; i++)
2732 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2733
2734 pci_set_drvdata(pdev, brd);
2735
2736 return 0;
2737err_relio:
2738 pci_release_region(pdev, 2);
2739err_null:
2740 brd->info = NULL;
2741err:
2742 return retval;
2743#else
2744 return -ENODEV;
2745#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
Jiri Slaby1c456072008-02-07 00:16:46 -08002748static void __devexit mxser_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
Jiri Slaby1c456072008-02-07 00:16:46 -08002750 struct mxser_board *brd = pci_get_drvdata(pdev);
2751 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Jiri Slaby1c456072008-02-07 00:16:46 -08002753 for (i = 0; i < brd->info->nports; i++)
2754 tty_unregister_device(mxvar_sdriver, brd->idx + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Jiri Slaby1c456072008-02-07 00:16:46 -08002756 mxser_release_res(brd, pdev, 1);
2757 brd->info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758}
2759
Jiri Slaby1c456072008-02-07 00:16:46 -08002760static struct pci_driver mxser_driver = {
2761 .name = "mxser",
2762 .id_table = mxser_pcibrds,
2763 .probe = mxser_probe,
2764 .remove = __devexit_p(mxser_remove)
2765};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Jiri Slaby1c456072008-02-07 00:16:46 -08002767static int __init mxser_module_init(void)
2768{
2769 struct mxser_board *brd;
Jiri Slaby1df00922008-07-25 01:48:22 -07002770 unsigned int b, i, m;
2771 int retval;
Jiri Slaby1c456072008-02-07 00:16:46 -08002772
Jiri Slaby1c456072008-02-07 00:16:46 -08002773 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2774 if (!mxvar_sdriver)
2775 return -ENOMEM;
2776
2777 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2778 MXSER_VERSION);
2779
2780 /* Initialize the tty_driver structure */
2781 mxvar_sdriver->owner = THIS_MODULE;
2782 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2783 mxvar_sdriver->name = "ttyMI";
2784 mxvar_sdriver->major = ttymajor;
2785 mxvar_sdriver->minor_start = 0;
2786 mxvar_sdriver->num = MXSER_PORTS + 1;
2787 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2788 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2789 mxvar_sdriver->init_termios = tty_std_termios;
2790 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2791 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2792 tty_set_operations(mxvar_sdriver, &mxser_ops);
2793
2794 retval = tty_register_driver(mxvar_sdriver);
2795 if (retval) {
2796 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2797 "tty driver !\n");
2798 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002800
Jiri Slaby1c456072008-02-07 00:16:46 -08002801 /* Start finding ISA boards here */
Jiri Slaby1df00922008-07-25 01:48:22 -07002802 for (m = 0, b = 0; b < MXSER_BOARDS; b++) {
2803 if (!ioaddr[b])
2804 continue;
Jiri Slaby1c456072008-02-07 00:16:46 -08002805
Jiri Slaby1df00922008-07-25 01:48:22 -07002806 brd = &mxser_boards[m];
2807 retval = mxser_get_ISA_conf(!ioaddr[b], brd);
2808 if (retval <= 0) {
2809 brd->info = NULL;
2810 continue;
Jiri Slaby1c456072008-02-07 00:16:46 -08002811 }
2812
Jiri Slaby1df00922008-07-25 01:48:22 -07002813 printk(KERN_INFO "mxser: found MOXA %s board (CAP=0x%lx)\n",
2814 brd->info->name, ioaddr[b]);
2815
2816 /* mxser_initbrd will hook ISR. */
2817 if (mxser_initbrd(brd, NULL) < 0) {
2818 brd->info = NULL;
2819 continue;
2820 }
2821
2822 brd->idx = m * MXSER_PORTS_PER_BOARD;
2823 for (i = 0; i < brd->info->nports; i++)
2824 tty_register_device(mxvar_sdriver, brd->idx + i, NULL);
2825
2826 m++;
2827 }
2828
Jiri Slaby1c456072008-02-07 00:16:46 -08002829 retval = pci_register_driver(&mxser_driver);
2830 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002831 printk(KERN_ERR "mxser: can't register pci driver\n");
Jiri Slaby1c456072008-02-07 00:16:46 -08002832 if (!m) {
2833 retval = -ENODEV;
2834 goto err_unr;
2835 } /* else: we have some ISA cards under control */
2836 }
2837
Jiri Slaby1c456072008-02-07 00:16:46 -08002838 return 0;
2839err_unr:
2840 tty_unregister_driver(mxvar_sdriver);
2841err_put:
2842 put_tty_driver(mxvar_sdriver);
2843 return retval;
2844}
2845
2846static void __exit mxser_module_exit(void)
2847{
2848 unsigned int i, j;
2849
Jiri Slaby1c456072008-02-07 00:16:46 -08002850 pci_unregister_driver(&mxser_driver);
2851
2852 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2853 if (mxser_boards[i].info != NULL)
2854 for (j = 0; j < mxser_boards[i].info->nports; j++)
2855 tty_unregister_device(mxvar_sdriver,
2856 mxser_boards[i].idx + j);
2857 tty_unregister_driver(mxvar_sdriver);
2858 put_tty_driver(mxvar_sdriver);
2859
2860 for (i = 0; i < MXSER_BOARDS; i++)
2861 if (mxser_boards[i].info != NULL)
2862 mxser_release_res(&mxser_boards[i], NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863}
2864
2865module_init(mxser_module_init);
2866module_exit(mxser_module_exit);