blob: 5d7901096048d23d2bdc11042e9e0c1f62b2da5d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
3 *
4 * Copyright (C) 1999-2001 Moxa Technologies (support@moxa.com.tw).
5 *
6 * This code is loosely based on the Linux serial driver, written by
7 * Linus Torvalds, Theodore T'so and others.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070012 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * Original release 10/26/00
24 *
25 * 02/06/01 Support MOXA Industio family boards.
26 * 02/06/01 Support TIOCGICOUNT.
27 * 02/06/01 Fix the problem for connecting to serial mouse.
28 * 02/06/01 Fix the problem for H/W flow control.
29 * 02/06/01 Fix the compling warning when CONFIG_PCI
30 * don't be defined.
31 *
32 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
33 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
34 * - Fixed x86_64 cleanness
35 * - Fixed sleep with spinlock held in mxser_send_break
36 */
37
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/errno.h>
41#include <linux/signal.h>
42#include <linux/sched.h>
43#include <linux/timer.h>
44#include <linux/interrupt.h>
45#include <linux/tty.h>
46#include <linux/tty_flip.h>
47#include <linux/serial.h>
48#include <linux/serial_reg.h>
49#include <linux/major.h>
50#include <linux/string.h>
51#include <linux/fcntl.h>
52#include <linux/ptrace.h>
53#include <linux/gfp.h>
54#include <linux/ioport.h>
55#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/delay.h>
57#include <linux/pci.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070058#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#include <asm/system.h>
61#include <asm/io.h>
62#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <asm/uaccess.h>
64
65#include "mxser.h"
66
67#define MXSER_VERSION "1.8"
68#define MXSERMAJOR 174
69#define MXSERCUMAJOR 175
70
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070071#define MXSER_EVENT_TXLOW 1
72#define MXSER_EVENT_HANGUP 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#define MXSER_BOARDS 4 /* Max. boards */
75#define MXSER_PORTS 32 /* Max. ports */
76#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
77#define MXSER_ISR_PASS_LIMIT 256
78
79#define MXSER_ERR_IOADDR -1
80#define MXSER_ERR_IRQ -2
81#define MXSER_ERR_IRQ_CONFLIT -3
82#define MXSER_ERR_VECTOR -4
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define WAKEUP_CHARS 256
85
86#define UART_MCR_AFE 0x20
87#define UART_LSR_SPECIAL 0x1E
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -070090#define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED : IRQF_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#define C168_ASIC_ID 1
93#define C104_ASIC_ID 2
94#define C102_ASIC_ID 0xB
95#define CI132_ASIC_ID 4
96#define CI134_ASIC_ID 3
97#define CI104J_ASIC_ID 5
98
99enum {
100 MXSER_BOARD_C168_ISA = 1,
101 MXSER_BOARD_C104_ISA,
102 MXSER_BOARD_CI104J,
103 MXSER_BOARD_C168_PCI,
104 MXSER_BOARD_C104_PCI,
105 MXSER_BOARD_C102_ISA,
106 MXSER_BOARD_CI132,
107 MXSER_BOARD_CI134,
108 MXSER_BOARD_CP132,
109 MXSER_BOARD_CP114,
110 MXSER_BOARD_CT114,
111 MXSER_BOARD_CP102,
112 MXSER_BOARD_CP104U,
113 MXSER_BOARD_CP168U,
114 MXSER_BOARD_CP132U,
115 MXSER_BOARD_CP134U,
116 MXSER_BOARD_CP104JU,
117 MXSER_BOARD_RC7000,
118 MXSER_BOARD_CP118U,
119 MXSER_BOARD_CP102UL,
120 MXSER_BOARD_CP102U,
121};
122
123static char *mxser_brdname[] = {
124 "C168 series",
125 "C104 series",
126 "CI-104J series",
127 "C168H/PCI series",
128 "C104H/PCI series",
129 "C102 series",
130 "CI-132 series",
131 "CI-134 series",
132 "CP-132 series",
133 "CP-114 series",
134 "CT-114 series",
135 "CP-102 series",
136 "CP-104U series",
137 "CP-168U series",
138 "CP-132U series",
139 "CP-134U series",
140 "CP-104JU series",
141 "Moxa UC7000 Serial",
142 "CP-118U series",
143 "CP-102UL series",
144 "CP-102U series",
145};
146
147static int mxser_numports[] = {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700148 8, /* C168-ISA */
149 4, /* C104-ISA */
150 4, /* CI104J */
151 8, /* C168-PCI */
152 4, /* C104-PCI */
153 2, /* C102-ISA */
154 2, /* CI132 */
155 4, /* CI134 */
156 2, /* CP132 */
157 4, /* CP114 */
158 4, /* CT114 */
159 2, /* CP102 */
160 4, /* CP104U */
161 8, /* CP168U */
162 2, /* CP132U */
163 4, /* CP134U */
164 4, /* CP104JU */
165 8, /* RC7000 */
166 8, /* CP118U */
167 2, /* CP102UL */
168 2, /* CP102U */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170
171#define UART_TYPE_NUM 2
172
173static const unsigned int Gmoxa_uart_id[UART_TYPE_NUM] = {
174 MOXA_MUST_MU150_HWID,
175 MOXA_MUST_MU860_HWID
176};
177
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700178/* This is only for PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#define UART_INFO_NUM 3
180struct mxpciuart_info {
181 int type;
182 int tx_fifo;
183 int rx_fifo;
184 int xmit_fifo_size;
185 int rx_high_water;
186 int rx_trigger;
187 int rx_low_water;
188 long max_baud;
189};
190
191static const struct mxpciuart_info Gpci_uart_info[UART_INFO_NUM] = {
192 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
193 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
194 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
195};
196
197
198#ifdef CONFIG_PCI
199
200static struct pci_device_id mxser_pcibrds[] = {
201 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_C168_PCI},
202 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_C104_PCI},
203 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP132},
204 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP114},
205 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CT114},
206 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102},
207 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP104U},
208 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP168U},
209 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP132U},
210 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP134U},
211 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP104JU},
212 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_RC7000},
213 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP118U},
214 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102UL},
215 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102U},
216 {0}
217};
218
219MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
220
221
222#endif
223
224typedef struct _moxa_pci_info {
225 unsigned short busNum;
226 unsigned short devNum;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700227 struct pci_dev *pdev; /* add by Victor Yu. 06-23-2003 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228} moxa_pci_info;
229
230static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
231static int ttymajor = MXSERMAJOR;
232static int calloutmajor = MXSERCUMAJOR;
233static int verbose = 0;
234
235/* Variables for insmod */
236
237MODULE_AUTHOR("Casper Yang");
238MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800239module_param_array(ioaddr, int, NULL, 0);
240module_param(ttymajor, int, 0);
241module_param(calloutmajor, int, 0);
242module_param(verbose, bool, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243MODULE_LICENSE("GPL");
244
245struct mxser_log {
246 int tick;
247 unsigned long rxcnt[MXSER_PORTS];
248 unsigned long txcnt[MXSER_PORTS];
249};
250
251
252struct mxser_mon {
253 unsigned long rxcnt;
254 unsigned long txcnt;
255 unsigned long up_rxcnt;
256 unsigned long up_txcnt;
257 int modem_status;
258 unsigned char hold_reason;
259};
260
261struct mxser_mon_ext {
262 unsigned long rx_cnt[32];
263 unsigned long tx_cnt[32];
264 unsigned long up_rxcnt[32];
265 unsigned long up_txcnt[32];
266 int modem_status[32];
267
268 long baudrate[32];
269 int databits[32];
270 int stopbits[32];
271 int parity[32];
272 int flowctrl[32];
273 int fifo[32];
274 int iftype[32];
275};
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277struct mxser_hwconf {
278 int board_type;
279 int ports;
280 int irq;
281 int vector;
282 int vector_mask;
283 int uart_type;
284 int ioaddr[MXSER_PORTS_PER_BOARD];
285 int baud_base[MXSER_PORTS_PER_BOARD];
286 moxa_pci_info pciInfo;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700287 int IsMoxaMustChipFlag; /* add by Victor Yu. 08-30-2002 */
288 int MaxCanSetBaudRate[MXSER_PORTS_PER_BOARD]; /* add by Victor Yu. 09-04-2002 */
289 int opmode_ioaddr[MXSER_PORTS_PER_BOARD]; /* add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290};
291
292struct mxser_struct {
293 int port;
294 int base; /* port base address */
295 int irq; /* port using irq no. */
296 int vector; /* port irq vector */
297 int vectormask; /* port vector mask */
298 int rx_high_water;
299 int rx_trigger; /* Rx fifo trigger level */
300 int rx_low_water;
301 int baud_base; /* max. speed */
302 int flags; /* defined in tty.h */
303 int type; /* UART type */
304 struct tty_struct *tty;
305 int read_status_mask;
306 int ignore_status_mask;
307 int xmit_fifo_size;
308 int custom_divisor;
309 int x_char; /* xon/xoff character */
310 int close_delay;
311 unsigned short closing_wait;
312 int IER; /* Interrupt Enable Register */
313 int MCR; /* Modem control register */
314 unsigned long event;
315 int count; /* # of fd on device */
316 int blocked_open; /* # of blocked opens */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned char *xmit_buf;
318 int xmit_head;
319 int xmit_tail;
320 int xmit_cnt;
321 struct work_struct tqueue;
Alan Cox606d0992006-12-08 02:38:45 -0800322 struct ktermios normal_termios;
323 struct ktermios callout_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 wait_queue_head_t open_wait;
325 wait_queue_head_t close_wait;
326 wait_queue_head_t delta_msr_wait;
327 struct async_icount icount; /* kernel counters for the 4 input interrupts */
328 int timeout;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700329 int IsMoxaMustChipFlag; /* add by Victor Yu. 08-30-2002 */
330 int MaxCanSetBaudRate; /* add by Victor Yu. 09-04-2002 */
331 int opmode_ioaddr; /* add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 unsigned char stop_rx;
333 unsigned char ldisc_stop_rx;
334 long realbaud;
335 struct mxser_mon mon_data;
336 unsigned char err_shadow;
337 spinlock_t slock;
338};
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340struct mxser_mstatus {
341 tcflag_t cflag;
342 int cts;
343 int dsr;
344 int ri;
345 int dcd;
346};
347
348static struct mxser_mstatus GMStatus[MXSER_PORTS];
349
350static int mxserBoardCAP[MXSER_BOARDS] = {
351 0, 0, 0, 0
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700352 /* 0x180, 0x280, 0x200, 0x320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353};
354
355static struct tty_driver *mxvar_sdriver;
356static struct mxser_struct mxvar_table[MXSER_PORTS];
357static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
Alan Cox606d0992006-12-08 02:38:45 -0800358static struct ktermios *mxvar_termios[MXSER_PORTS + 1];
359static struct ktermios *mxvar_termios_locked[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360static struct mxser_log mxvar_log;
361static int mxvar_diagflag;
362static unsigned char mxser_msr[MXSER_PORTS + 1];
363static struct mxser_mon_ext mon_data_ext;
364static int mxser_set_baud_method[MXSER_PORTS + 1];
365static spinlock_t gm_lock;
366
367/*
368 * This is used to figure out the divisor speeds and the timeouts
369 */
370
371static struct mxser_hwconf mxsercfg[MXSER_BOARDS];
372
373/*
374 * static functions:
375 */
376
377static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
378static int mxser_init(void);
379
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700380/* static void mxser_poll(unsigned long); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
David Howellsc4028952006-11-22 14:57:56 +0000382static void mxser_do_softint(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383static int mxser_open(struct tty_struct *, struct file *);
384static void mxser_close(struct tty_struct *, struct file *);
385static int mxser_write(struct tty_struct *, const unsigned char *, int);
386static int mxser_write_room(struct tty_struct *);
387static void mxser_flush_buffer(struct tty_struct *);
388static int mxser_chars_in_buffer(struct tty_struct *);
389static void mxser_flush_chars(struct tty_struct *);
390static void mxser_put_char(struct tty_struct *, unsigned char);
391static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
392static int mxser_ioctl_special(unsigned int, void __user *);
393static void mxser_throttle(struct tty_struct *);
394static void mxser_unthrottle(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800395static void mxser_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396static void mxser_stop(struct tty_struct *);
397static void mxser_start(struct tty_struct *);
398static void mxser_hangup(struct tty_struct *);
399static void mxser_rs_break(struct tty_struct *, int);
David Howells7d12e782006-10-05 14:55:46 +0100400static irqreturn_t mxser_interrupt(int, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401static void mxser_receive_chars(struct mxser_struct *, int *);
402static void mxser_transmit_chars(struct mxser_struct *);
403static void mxser_check_modem_status(struct mxser_struct *, int);
404static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
405static int mxser_startup(struct mxser_struct *);
406static void mxser_shutdown(struct mxser_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800407static int mxser_change_speed(struct mxser_struct *, struct ktermios *old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct __user *);
409static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct __user *);
410static int mxser_get_lsr_info(struct mxser_struct *, unsigned int __user *);
411static void mxser_send_break(struct mxser_struct *, int);
412static int mxser_tiocmget(struct tty_struct *, struct file *);
413static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
414static int mxser_set_baud(struct mxser_struct *info, long newspd);
415static void mxser_wait_until_sent(struct tty_struct *tty, int timeout);
416
417static void mxser_startrx(struct tty_struct *tty);
418static void mxser_stoprx(struct tty_struct *tty);
419
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700420#ifdef CONFIG_PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static int CheckIsMoxaMust(int io)
422{
423 u8 oldmcr, hwid;
424 int i;
425
426 outb(0, io + UART_LCR);
427 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
428 oldmcr = inb(io + UART_MCR);
429 outb(0, io + UART_MCR);
430 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
431 if ((hwid = inb(io + UART_MCR)) != 0) {
432 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700433 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
436 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
437 for (i = 0; i < UART_TYPE_NUM; i++) {
438 if (hwid == Gmoxa_uart_id[i])
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700439 return (int)hwid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441 return MOXA_OTHER_UART;
442}
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700443#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700445/* above is modified by Victor Yu. 08-15-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700447static const struct tty_operations mxser_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 .open = mxser_open,
449 .close = mxser_close,
450 .write = mxser_write,
451 .put_char = mxser_put_char,
452 .flush_chars = mxser_flush_chars,
453 .write_room = mxser_write_room,
454 .chars_in_buffer = mxser_chars_in_buffer,
455 .flush_buffer = mxser_flush_buffer,
456 .ioctl = mxser_ioctl,
457 .throttle = mxser_throttle,
458 .unthrottle = mxser_unthrottle,
459 .set_termios = mxser_set_termios,
460 .stop = mxser_stop,
461 .start = mxser_start,
462 .hangup = mxser_hangup,
Kirill Smelkov57432342005-11-07 00:59:20 -0800463 .break_ctl = mxser_rs_break,
464 .wait_until_sent = mxser_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 .tiocmget = mxser_tiocmget,
466 .tiocmset = mxser_tiocmset,
467};
468
469/*
470 * The MOXA Smartio/Industio serial driver boot-time initialization code!
471 */
472
473static int __init mxser_module_init(void)
474{
475 int ret;
476
477 if (verbose)
478 printk(KERN_DEBUG "Loading module mxser ...\n");
479 ret = mxser_init();
480 if (verbose)
481 printk(KERN_DEBUG "Done.\n");
482 return ret;
483}
484
485static void __exit mxser_module_exit(void)
486{
Kirill Smelkov64698b62005-11-07 00:59:22 -0800487 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (verbose)
490 printk(KERN_DEBUG "Unloading module mxser ...\n");
491
Kirill Smelkov64698b62005-11-07 00:59:22 -0800492 err = tty_unregister_driver(mxvar_sdriver);
493 if (!err)
494 put_tty_driver(mxvar_sdriver);
495 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
497
498 for (i = 0; i < MXSER_BOARDS; i++) {
499 struct pci_dev *pdev;
500
501 if (mxsercfg[i].board_type == -1)
502 continue;
503 else {
504 pdev = mxsercfg[i].pciInfo.pdev;
505 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700506 if (pdev != NULL) { /* PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 release_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2));
508 release_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3));
Alan Cox1187ece2006-12-08 02:38:10 -0800509 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 } else {
511 release_region(mxsercfg[i].ioaddr[0], 8 * mxsercfg[i].ports);
512 release_region(mxsercfg[i].vector, 1);
513 }
514 }
515 }
516 if (verbose)
517 printk(KERN_DEBUG "Done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520static void process_txrx_fifo(struct mxser_struct *info)
521{
522 int i;
523
524 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
525 info->rx_trigger = 1;
526 info->rx_high_water = 1;
527 info->rx_low_water = 1;
528 info->xmit_fifo_size = 1;
529 } else {
530 for (i = 0; i < UART_INFO_NUM; i++) {
531 if (info->IsMoxaMustChipFlag == Gpci_uart_info[i].type) {
532 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
533 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
534 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
535 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
536 break;
537 }
538 }
539 }
540}
541
542static int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
543{
544 struct mxser_struct *info;
545 int retval;
546 int i, n;
547
548 n = board * MXSER_PORTS_PER_BOARD;
549 info = &mxvar_table[n];
550 /*if (verbose) */ {
Jiri Slabyae25f8e2006-12-08 02:38:09 -0800551 printk(KERN_DEBUG " ttyMI%d - ttyMI%d ",
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700552 n, n + hwconf->ports - 1);
553 printk(" max. baud rate = %d bps.\n",
554 hwconf->MaxCanSetBaudRate[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556
557 for (i = 0; i < hwconf->ports; i++, n++, info++) {
558 info->port = n;
559 info->base = hwconf->ioaddr[i];
560 info->irq = hwconf->irq;
561 info->vector = hwconf->vector;
562 info->vectormask = hwconf->vector_mask;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700563 info->opmode_ioaddr = hwconf->opmode_ioaddr[i]; /* add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 info->stop_rx = 0;
565 info->ldisc_stop_rx = 0;
566
567 info->IsMoxaMustChipFlag = hwconf->IsMoxaMustChipFlag;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700568 /* Enhance mode enabled here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
570 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->base);
571 }
572
573 info->flags = ASYNC_SHARE_IRQ;
574 info->type = hwconf->uart_type;
575 info->baud_base = hwconf->baud_base[i];
576
577 info->MaxCanSetBaudRate = hwconf->MaxCanSetBaudRate[i];
578
579 process_txrx_fifo(info);
580
581
582 info->custom_divisor = hwconf->baud_base[i] * 16;
583 info->close_delay = 5 * HZ / 10;
584 info->closing_wait = 30 * HZ;
David Howellsc4028952006-11-22 14:57:56 +0000585 INIT_WORK(&info->tqueue, mxser_do_softint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 info->normal_termios = mxvar_sdriver->init_termios;
587 init_waitqueue_head(&info->open_wait);
588 init_waitqueue_head(&info->close_wait);
589 init_waitqueue_head(&info->delta_msr_wait);
590 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
591 info->err_shadow = 0;
592 spin_lock_init(&info->slock);
593 }
594 /*
595 * Allocate the IRQ if necessary
596 */
597
598
599 /* before set INT ISR, disable all int */
600 for (i = 0; i < hwconf->ports; i++) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700601 outb(inb(hwconf->ioaddr[i] + UART_IER) & 0xf0,
602 hwconf->ioaddr[i] + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
605 n = board * MXSER_PORTS_PER_BOARD;
606 info = &mxvar_table[n];
607
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700608 retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
609 "mxser", info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (retval) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700611 printk(KERN_ERR "Board %d: %s",
612 board, mxser_brdname[hwconf->board_type - 1]);
613 printk(" Request irq failed, IRQ (%d) may conflict with"
614 " another device.\n", info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return retval;
616 }
617 return 0;
618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
621{
622 mxsercfg[board] = *hwconf;
623}
624
625#ifdef CONFIG_PCI
626static int mxser_get_PCI_conf(int busnum, int devnum, int board_type, struct mxser_hwconf *hwconf)
627{
628 int i, j;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700629 /* unsigned int val; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 unsigned int ioaddress;
631 struct pci_dev *pdev = hwconf->pciInfo.pdev;
632
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700633 /* io address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 hwconf->board_type = board_type;
635 hwconf->ports = mxser_numports[board_type - 1];
636 ioaddress = pci_resource_start(pdev, 2);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700637 request_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2),
638 "mxser(IO)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700640 for (i = 0; i < hwconf->ports; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 hwconf->ioaddr[i] = ioaddress + 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700643 /* vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ioaddress = pci_resource_start(pdev, 3);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700645 request_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3),
646 "mxser(vector)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 hwconf->vector = ioaddress;
648
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700649 /* irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 hwconf->irq = hwconf->pciInfo.pdev->irq;
651
652 hwconf->IsMoxaMustChipFlag = CheckIsMoxaMust(hwconf->ioaddr[0]);
653 hwconf->uart_type = PORT_16550A;
654 hwconf->vector_mask = 0;
655
656
657 for (i = 0; i < hwconf->ports; i++) {
658 for (j = 0; j < UART_INFO_NUM; j++) {
659 if (Gpci_uart_info[j].type == hwconf->IsMoxaMustChipFlag) {
660 hwconf->MaxCanSetBaudRate[i] = Gpci_uart_info[j].max_baud;
661
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700662 /* exception....CP-102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (board_type == MXSER_BOARD_CP102)
664 hwconf->MaxCanSetBaudRate[i] = 921600;
665 break;
666 }
667 }
668 }
669
670 if (hwconf->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID) {
671 for (i = 0; i < hwconf->ports; i++) {
672 if (i < 4)
673 hwconf->opmode_ioaddr[i] = ioaddress + 4;
674 else
675 hwconf->opmode_ioaddr[i] = ioaddress + 0x0c;
676 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700677 outb(0, ioaddress + 4); /* default set to RS232 mode */
678 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
681 for (i = 0; i < hwconf->ports; i++) {
682 hwconf->vector_mask |= (1 << i);
683 hwconf->baud_base[i] = 921600;
684 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687#endif
688
689static int mxser_init(void)
690{
691 int i, m, retval, b, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct pci_dev *pdev = NULL;
693 int index;
694 unsigned char busnum, devnum;
695 struct mxser_hwconf hwconf;
696
697 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
698 if (!mxvar_sdriver)
699 return -ENOMEM;
700 spin_lock_init(&gm_lock);
701
702 for (i = 0; i < MXSER_BOARDS; i++) {
703 mxsercfg[i].board_type = -1;
704 }
705
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700706 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
707 MXSER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /* Initialize the tty_driver structure */
710 memset(mxvar_sdriver, 0, sizeof(struct tty_driver));
Jiri Slaby31f87cf2006-12-29 16:47:34 -0800711 mxvar_sdriver->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
Jiri Slabyae25f8e2006-12-08 02:38:09 -0800713 mxvar_sdriver->name = "ttyMI";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 mxvar_sdriver->major = ttymajor;
715 mxvar_sdriver->minor_start = 0;
716 mxvar_sdriver->num = MXSER_PORTS + 1;
717 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
718 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
719 mxvar_sdriver->init_termios = tty_std_termios;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700720 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -0800721 mxvar_sdriver->init_termios.c_ispeed = 9600;
722 mxvar_sdriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
724 tty_set_operations(mxvar_sdriver, &mxser_ops);
725 mxvar_sdriver->ttys = mxvar_tty;
726 mxvar_sdriver->termios = mxvar_termios;
727 mxvar_sdriver->termios_locked = mxvar_termios_locked;
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 mxvar_diagflag = 0;
730 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
731 memset(&mxvar_log, 0, sizeof(struct mxser_log));
732
733 memset(&mxser_msr, 0, sizeof(unsigned char) * (MXSER_PORTS + 1));
734 memset(&mon_data_ext, 0, sizeof(struct mxser_mon_ext));
735 memset(&mxser_set_baud_method, 0, sizeof(int) * (MXSER_PORTS + 1));
736 memset(&hwconf, 0, sizeof(struct mxser_hwconf));
737
738 m = 0;
739 /* Start finding ISA boards here */
740 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
741 int cap;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (!(cap = mxserBoardCAP[b]))
744 continue;
745
746 retval = mxser_get_ISA_conf(cap, &hwconf);
747
748 if (retval != 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700749 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
750 mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 if (retval <= 0) {
753 if (retval == MXSER_ERR_IRQ)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700754 printk(KERN_ERR "Invalid interrupt number, "
755 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 else if (retval == MXSER_ERR_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700757 printk(KERN_ERR "Invalid interrupt number, "
758 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700760 printk(KERN_ERR "Invalid interrupt vector, "
761 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700763 printk(KERN_ERR "Invalid I/O address, "
764 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 continue;
767 }
768
769 hwconf.pciInfo.busNum = 0;
770 hwconf.pciInfo.devNum = 0;
771 hwconf.pciInfo.pdev = NULL;
772
773 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700774 /*
775 * init mxsercfg first,
776 * or mxsercfg data is not correct on ISR.
777 */
778 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (mxser_initbrd(m, &hwconf) < 0)
780 continue;
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 m++;
783 }
784
785 /* Start finding ISA boards from module arg */
786 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
787 int cap;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (!(cap = ioaddr[b]))
790 continue;
791
792 retval = mxser_get_ISA_conf(cap, &hwconf);
793
794 if (retval != 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700795 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
796 mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if (retval <= 0) {
799 if (retval == MXSER_ERR_IRQ)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700800 printk(KERN_ERR "Invalid interrupt number, "
801 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 else if (retval == MXSER_ERR_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700803 printk(KERN_ERR "Invalid interrupt number, "
804 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700806 printk(KERN_ERR "Invalid interrupt vector, "
807 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700809 printk(KERN_ERR "Invalid I/O address, "
810 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 continue;
813 }
814
815 hwconf.pciInfo.busNum = 0;
816 hwconf.pciInfo.devNum = 0;
817 hwconf.pciInfo.pdev = NULL;
818
819 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700820 /*
821 * init mxsercfg first,
822 * or mxsercfg data is not correct on ISR.
823 */
824 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (mxser_initbrd(m, &hwconf) < 0)
826 continue;
827
828 m++;
829 }
830
831 /* start finding PCI board here */
832#ifdef CONFIG_PCI
Tobias Klauserfe971072006-01-09 20:54:02 -0800833 n = ARRAY_SIZE(mxser_pcibrds) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 index = 0;
835 b = 0;
836 while (b < n) {
Alan Cox1187ece2006-12-08 02:38:10 -0800837 pdev = pci_get_device(mxser_pcibrds[b].vendor,
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700838 mxser_pcibrds[b].device, pdev);
Alan Cox1187ece2006-12-08 02:38:10 -0800839 if (pdev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 b++;
841 continue;
842 }
843 hwconf.pciInfo.busNum = busnum = pdev->bus->number;
844 hwconf.pciInfo.devNum = devnum = PCI_SLOT(pdev->devfn) << 3;
845 hwconf.pciInfo.pdev = pdev;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700846 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
847 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
848 busnum, devnum >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 index++;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700850 if (m >= MXSER_BOARDS)
851 printk(KERN_ERR
852 "Too many Smartio/Industio family boards find "
853 "(maximum %d), board not configured\n",
854 MXSER_BOARDS);
855 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (pci_enable_device(pdev)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700857 printk(KERN_ERR "Moxa SmartI/O PCI enable "
858 "fail !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 continue;
860 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700861 retval = mxser_get_PCI_conf(busnum, devnum,
862 (int)mxser_pcibrds[b].driver_data,
863 &hwconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (retval < 0) {
865 if (retval == MXSER_ERR_IRQ)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700866 printk(KERN_ERR
867 "Invalid interrupt number, "
868 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 else if (retval == MXSER_ERR_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700870 printk(KERN_ERR
871 "Invalid interrupt number, "
872 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700874 printk(KERN_ERR
875 "Invalid interrupt vector, "
876 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700878 printk(KERN_ERR
879 "Invalid I/O address, "
880 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 continue;
882 }
883 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700884 /* init mxsercfg first,
885 * or mxsercfg data is not correct on ISR.
886 */
887 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (mxser_initbrd(m, &hwconf) < 0)
889 continue;
890 m++;
Alan Cox1187ece2006-12-08 02:38:10 -0800891 /* Keep an extra reference if we succeeded. It will
892 be returned at unload time */
893 pci_dev_get(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 }
896#endif
897
Kirill Smelkov64698b62005-11-07 00:59:22 -0800898 retval = tty_register_driver(mxvar_sdriver);
899 if (retval) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700900 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
901 " driver !\n");
Kirill Smelkov64698b62005-11-07 00:59:22 -0800902 put_tty_driver(mxvar_sdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 for (i = 0; i < MXSER_BOARDS; i++) {
905 if (mxsercfg[i].board_type == -1)
906 continue;
907 else {
908 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700909 /* todo: release io, vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911 }
Kirill Smelkov64698b62005-11-07 00:59:22 -0800912 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914
Kirill Smelkov64698b62005-11-07 00:59:22 -0800915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
David Howellsc4028952006-11-22 14:57:56 +0000918static void mxser_do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
David Howellsc4028952006-11-22 14:57:56 +0000920 struct mxser_struct *info =
921 container_of(work, struct mxser_struct, tqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 struct tty_struct *tty;
923
924 tty = info->tty;
925
926 if (tty) {
927 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
928 tty_wakeup(tty);
929 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
930 tty_hangup(tty);
931 }
932}
933
934static unsigned char mxser_get_msr(int baseaddr, int mode, int port, struct mxser_struct *info)
935{
936 unsigned char status = 0;
937
938 status = inb(baseaddr + UART_MSR);
939
940 mxser_msr[port] &= 0x0F;
941 mxser_msr[port] |= status;
942 status = mxser_msr[port];
943 if (mode)
944 mxser_msr[port] = 0;
945
946 return status;
947}
948
949/*
950 * This routine is called whenever a serial port is opened. It
951 * enables interrupts for a serial port, linking in its async structure into
952 * the IRQ chain. It also performs the serial-specific
953 * initialization for the tty structure.
954 */
955static int mxser_open(struct tty_struct *tty, struct file *filp)
956{
957 struct mxser_struct *info;
958 int retval, line;
959
Kirill Smelkov6f08b722005-11-07 00:59:23 -0800960 /* initialize driver_data in case something fails */
961 tty->driver_data = NULL;
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 line = tty->index;
964 if (line == MXSER_PORTS)
965 return 0;
966 if (line < 0 || line > MXSER_PORTS)
967 return -ENODEV;
968 info = mxvar_table + line;
969 if (!info->base)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700970 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 tty->driver_data = info;
973 info->tty = tty;
974 /*
975 * Start up serial port
976 */
977 retval = mxser_startup(info);
978 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700979 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 retval = mxser_block_til_ready(tty, filp, info);
982 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700983 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 info->count++;
986
987 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
988 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
989 *tty->termios = info->normal_termios;
990 else
991 *tty->termios = info->callout_termios;
992 mxser_change_speed(info, NULL);
993 }
994
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700995 /*
996 status = mxser_get_msr(info->base, 0, info->port);
997 mxser_check_modem_status(info, status);
998 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Cedric Le Goater8cddd702007-02-10 01:46:33 -08001000 /* unmark here for very high baud rate (ex. 921600 bps) used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 tty->low_latency = 1;
1002 return 0;
1003}
1004
1005/*
1006 * This routine is called when the serial port gets closed. First, we
1007 * wait for the last remaining data to be sent. Then, we unlink its
1008 * async structure from the interrupt chain if necessary, and we free
1009 * that IRQ if nothing is left in the chain.
1010 */
1011static void mxser_close(struct tty_struct *tty, struct file *filp)
1012{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001013 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 unsigned long timeout;
1016 unsigned long flags;
1017 struct tty_ldisc *ld;
1018
1019 if (tty->index == MXSER_PORTS)
1020 return;
1021 if (!info)
Kirill Smelkov6f08b722005-11-07 00:59:23 -08001022 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 spin_lock_irqsave(&info->slock, flags);
1025
1026 if (tty_hung_up_p(filp)) {
1027 spin_unlock_irqrestore(&info->slock, flags);
1028 return;
1029 }
1030 if ((tty->count == 1) && (info->count != 1)) {
1031 /*
1032 * Uh, oh. tty->count is 1, which means that the tty
1033 * structure will be freed. Info->count should always
1034 * be one in these conditions. If it's greater than
1035 * one, we've got real problems, since it means the
1036 * serial port won't be shutdown.
1037 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001038 printk(KERN_ERR "mxser_close: bad serial port count; "
1039 "tty->count is 1, info->count is %d\n", info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 info->count = 1;
1041 }
1042 if (--info->count < 0) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001043 printk(KERN_ERR "mxser_close: bad serial port count for "
1044 "ttys%d: %d\n", info->port, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 info->count = 0;
1046 }
1047 if (info->count) {
1048 spin_unlock_irqrestore(&info->slock, flags);
1049 return;
1050 }
1051 info->flags |= ASYNC_CLOSING;
1052 spin_unlock_irqrestore(&info->slock, flags);
1053 /*
1054 * Save the termios structure, since this port may have
1055 * separate termios for callout and dialin.
1056 */
1057 if (info->flags & ASYNC_NORMAL_ACTIVE)
1058 info->normal_termios = *tty->termios;
1059 /*
1060 * Now we wait for the transmit buffer to clear; and we notify
1061 * the line discipline to only process XON/XOFF characters.
1062 */
1063 tty->closing = 1;
1064 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1065 tty_wait_until_sent(tty, info->closing_wait);
1066 /*
1067 * At this point we stop accepting input. To do this, we
1068 * disable the receive line status interrupts, and tell the
1069 * interrupt driver to stop checking the data ready bit in the
1070 * line status register.
1071 */
1072 info->IER &= ~UART_IER_RLSI;
1073 if (info->IsMoxaMustChipFlag)
1074 info->IER &= ~MOXA_MUST_RECV_ISR;
1075/* by William
1076 info->read_status_mask &= ~UART_LSR_DR;
1077*/
1078 if (info->flags & ASYNC_INITIALIZED) {
1079 outb(info->IER, info->base + UART_IER);
1080 /*
1081 * Before we drop DTR, make sure the UART transmitter
1082 * has completely drained; this is especially
1083 * important if there is a transmit FIFO!
1084 */
1085 timeout = jiffies + HZ;
1086 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001087 schedule_timeout_interruptible(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (time_after(jiffies, timeout))
1089 break;
1090 }
1091 }
1092 mxser_shutdown(info);
1093
1094 if (tty->driver->flush_buffer)
1095 tty->driver->flush_buffer(tty);
1096
1097 ld = tty_ldisc_ref(tty);
1098 if (ld) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001099 if (ld->flush_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 ld->flush_buffer(tty);
1101 tty_ldisc_deref(ld);
1102 }
1103
1104 tty->closing = 0;
1105 info->event = 0;
1106 info->tty = NULL;
1107 if (info->blocked_open) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001108 if (info->close_delay)
1109 schedule_timeout_interruptible(info->close_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 wake_up_interruptible(&info->open_wait);
1111 }
1112
1113 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1114 wake_up_interruptible(&info->close_wait);
1115
1116}
1117
1118static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1119{
1120 int c, total = 0;
Jesper Juhl56e139f2006-06-25 05:47:52 -07001121 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 unsigned long flags;
1123
Jesper Juhl8a7f7c92006-06-25 05:47:53 -07001124 if (!info->xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001128 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1129 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (c <= 0)
1131 break;
1132
1133 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1134 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001135 info->xmit_head = (info->xmit_head + c) &
1136 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 info->xmit_cnt += c;
1138 spin_unlock_irqrestore(&info->slock, flags);
1139
1140 buf += c;
1141 count -= c;
1142 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144
1145 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001146 if (!tty->hw_stopped ||
1147 (info->type == PORT_16550A) ||
1148 (info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 spin_lock_irqsave(&info->slock, flags);
1150 info->IER |= UART_IER_THRI;
1151 outb(info->IER, info->base + UART_IER);
1152 spin_unlock_irqrestore(&info->slock, flags);
1153 }
1154 }
1155 return total;
1156}
1157
1158static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1159{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001160 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 unsigned long flags;
1162
Jesper Juhl8a7f7c92006-06-25 05:47:53 -07001163 if (!info->xmit_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return;
1165
1166 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1167 return;
1168
1169 spin_lock_irqsave(&info->slock, flags);
1170 info->xmit_buf[info->xmit_head++] = ch;
1171 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1172 info->xmit_cnt++;
1173 spin_unlock_irqrestore(&info->slock, flags);
1174 if (!tty->stopped && !(info->IER & UART_IER_THRI)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001175 if (!tty->hw_stopped ||
1176 (info->type == PORT_16550A) ||
1177 info->IsMoxaMustChipFlag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 spin_lock_irqsave(&info->slock, flags);
1179 info->IER |= UART_IER_THRI;
1180 outb(info->IER, info->base + UART_IER);
1181 spin_unlock_irqrestore(&info->slock, flags);
1182 }
1183 }
1184}
1185
1186
1187static void mxser_flush_chars(struct tty_struct *tty)
1188{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001189 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 unsigned long flags;
1191
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001192 if (info->xmit_cnt <= 0 ||
1193 tty->stopped ||
1194 !info->xmit_buf ||
1195 (tty->hw_stopped &&
1196 (info->type != PORT_16550A) &&
1197 (!info->IsMoxaMustChipFlag)
1198 ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return;
1200
1201 spin_lock_irqsave(&info->slock, flags);
1202
1203 info->IER |= UART_IER_THRI;
1204 outb(info->IER, info->base + UART_IER);
1205
1206 spin_unlock_irqrestore(&info->slock, flags);
1207}
1208
1209static int mxser_write_room(struct tty_struct *tty)
1210{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001211 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 int ret;
1213
1214 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1215 if (ret < 0)
1216 ret = 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001217 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
1220static int mxser_chars_in_buffer(struct tty_struct *tty)
1221{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001222 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return info->xmit_cnt;
1224}
1225
1226static void mxser_flush_buffer(struct tty_struct *tty)
1227{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001228 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 char fcr;
1230 unsigned long flags;
1231
1232
1233 spin_lock_irqsave(&info->slock, flags);
1234 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1235
1236 /* below added by shinhay */
1237 fcr = inb(info->base + UART_FCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001238 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1239 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 outb(fcr, info->base + UART_FCR);
1241
1242 spin_unlock_irqrestore(&info->slock, flags);
1243 /* above added by shinhay */
1244
Jiri Slabyb963a842007-02-10 01:44:55 -08001245 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
1248static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1249{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001250 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 int retval;
1252 struct async_icount cprev, cnow; /* kernel counter temps */
1253 struct serial_icounter_struct __user *p_cuser;
1254 unsigned long templ;
1255 unsigned long flags;
1256 void __user *argp = (void __user *)arg;
1257
1258 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001259 return mxser_ioctl_special(cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001261 /* following add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1263 int opmode, p;
1264 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1265 int shiftbit;
1266 unsigned char val, mask;
1267
1268 p = info->port % 4;
1269 if (cmd == MOXA_SET_OP_MODE) {
1270 if (get_user(opmode, (int __user *) argp))
1271 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001272 if (opmode != RS232_MODE &&
1273 opmode != RS485_2WIRE_MODE &&
1274 opmode != RS422_MODE &&
1275 opmode != RS485_4WIRE_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return -EFAULT;
1277 mask = ModeMask[p];
1278 shiftbit = p * 2;
1279 val = inb(info->opmode_ioaddr);
1280 val &= mask;
1281 val |= (opmode << shiftbit);
1282 outb(val, info->opmode_ioaddr);
1283 } else {
1284 shiftbit = p * 2;
1285 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1286 opmode &= OP_MODE_MASK;
1287 if (copy_to_user(argp, &opmode, sizeof(int)))
1288 return -EFAULT;
1289 }
1290 return 0;
1291 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001292 /* above add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1295 if (tty->flags & (1 << TTY_IO_ERROR))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001296 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298 switch (cmd) {
1299 case TCSBRK: /* SVID version: non-zero arg --> no break */
1300 retval = tty_check_change(tty);
1301 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001302 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 tty_wait_until_sent(tty, 0);
1304 if (!arg)
1305 mxser_send_break(info, HZ / 4); /* 1/4 second */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 case TCSBRKP: /* support for POSIX tcsendbreak() */
1308 retval = tty_check_change(tty);
1309 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001310 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 tty_wait_until_sent(tty, 0);
1312 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 case TIOCGSOFTCAR:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001315 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 case TIOCSSOFTCAR:
1317 if (get_user(templ, (unsigned long __user *) argp))
1318 return -EFAULT;
1319 arg = templ;
1320 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 case TIOCGSERIAL:
1323 return mxser_get_serial_info(info, argp);
1324 case TIOCSSERIAL:
1325 return mxser_set_serial_info(info, argp);
1326 case TIOCSERGETLSR: /* Get line status register */
1327 return mxser_get_lsr_info(info, argp);
1328 /*
1329 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1330 * - mask passed in arg for lines of interest
1331 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1332 * Caller should use TIOCGICOUNT to see which one it was
1333 */
Jiri Slabyfc838152007-04-23 14:41:04 -07001334 case TIOCMIWAIT:
1335 spin_lock_irqsave(&info->slock, flags);
1336 cnow = info->icount; /* note the counters on entry */
1337 spin_unlock_irqrestore(&info->slock, flags);
1338
1339 wait_event_interruptible(info->delta_msr_wait, ({
1340 cprev = cnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 spin_lock_irqsave(&info->slock, flags);
Jiri Slabyfc838152007-04-23 14:41:04 -07001342 cnow = info->icount; /* atomic copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 spin_unlock_irqrestore(&info->slock, flags);
1344
Jiri Slabyfc838152007-04-23 14:41:04 -07001345 ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1346 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1347 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1348 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
1349 }));
1350 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /*
1352 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1353 * Return: write counters to the user passed counter struct
1354 * NB: both 1->0 and 0->1 transitions are counted except for
1355 * RI where only 0->1 is counted.
1356 */
1357 case TIOCGICOUNT:
1358 spin_lock_irqsave(&info->slock, flags);
1359 cnow = info->icount;
1360 spin_unlock_irqrestore(&info->slock, flags);
1361 p_cuser = argp;
1362 /* modified by casper 1/11/2000 */
1363 if (put_user(cnow.frame, &p_cuser->frame))
1364 return -EFAULT;
1365 if (put_user(cnow.brk, &p_cuser->brk))
1366 return -EFAULT;
1367 if (put_user(cnow.overrun, &p_cuser->overrun))
1368 return -EFAULT;
1369 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1370 return -EFAULT;
1371 if (put_user(cnow.parity, &p_cuser->parity))
1372 return -EFAULT;
1373 if (put_user(cnow.rx, &p_cuser->rx))
1374 return -EFAULT;
1375 if (put_user(cnow.tx, &p_cuser->tx))
1376 return -EFAULT;
1377 put_user(cnow.cts, &p_cuser->cts);
1378 put_user(cnow.dsr, &p_cuser->dsr);
1379 put_user(cnow.rng, &p_cuser->rng);
1380 put_user(cnow.dcd, &p_cuser->dcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return 0;
1382 case MOXA_HighSpeedOn:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001383 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1384 case MOXA_SDS_RSTICOUNTER: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 info->mon_data.rxcnt = 0;
1386 info->mon_data.txcnt = 0;
1387 return 0;
1388 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001389/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 case MOXA_ASPP_SETBAUD:{
1391 long baud;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001392 if (get_user(baud, (long __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return -EFAULT;
1394 mxser_set_baud(info, baud);
1395 return 0;
1396 }
1397 case MOXA_ASPP_GETBAUD:
1398 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1399 return -EFAULT;
1400
1401 return 0;
1402
1403 case MOXA_ASPP_OQUEUE:{
1404 int len, lsr;
1405
1406 len = mxser_chars_in_buffer(tty);
1407
1408 lsr = inb(info->base + UART_LSR) & UART_LSR_TEMT;
1409
1410 len += (lsr ? 0 : 1);
1411
1412 if (copy_to_user(argp, &len, sizeof(int)))
1413 return -EFAULT;
1414
1415 return 0;
1416 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001417 case MOXA_ASPP_MON: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int mcr, status;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001419
1420 /* info->mon_data.ser_param = tty->termios->c_cflag; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 status = mxser_get_msr(info->base, 1, info->port, info);
1423 mxser_check_modem_status(info, status);
1424
1425 mcr = inb(info->base + UART_MCR);
1426 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1427 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1428 else
1429 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1430
1431 if (mcr & MOXA_MUST_MCR_TX_XON)
1432 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1433 else
1434 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1435
1436 if (info->tty->hw_stopped)
1437 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1438 else
1439 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1440
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001441 if (copy_to_user(argp, &info->mon_data,
1442 sizeof(struct mxser_mon)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return -EFAULT;
1444
1445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001448 case MOXA_ASPP_LSTATUS: {
1449 if (copy_to_user(argp, &info->err_shadow,
1450 sizeof(unsigned char)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return -EFAULT;
1452
1453 info->err_shadow = 0;
1454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001456 case MOXA_SET_BAUD_METHOD: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 int method;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001458
1459 if (get_user(method, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return -EFAULT;
1461 mxser_set_baud_method[info->port] = method;
1462 if (copy_to_user(argp, &method, sizeof(int)))
1463 return -EFAULT;
1464
1465 return 0;
1466 }
1467 default:
1468 return -ENOIOCTLCMD;
1469 }
1470 return 0;
1471}
1472
1473#ifndef CMSPAR
1474#define CMSPAR 010000000000
1475#endif
1476
1477static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1478{
1479 int i, result, status;
1480
1481 switch (cmd) {
1482 case MOXA_GET_CONF:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001483 if (copy_to_user(argp, mxsercfg,
1484 sizeof(struct mxser_hwconf) * 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return -EFAULT;
1486 return 0;
1487 case MOXA_GET_MAJOR:
1488 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1489 return -EFAULT;
1490 return 0;
1491
1492 case MOXA_GET_CUMAJOR:
1493 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1494 return -EFAULT;
1495 return 0;
1496
1497 case MOXA_CHKPORTENABLE:
1498 result = 0;
1499 for (i = 0; i < MXSER_PORTS; i++) {
1500 if (mxvar_table[i].base)
1501 result |= (1 << i);
1502 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001503 return put_user(result, (unsigned long __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 case MOXA_GETDATACOUNT:
1505 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1506 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 case MOXA_GETMSTATUS:
1509 for (i = 0; i < MXSER_PORTS; i++) {
1510 GMStatus[i].ri = 0;
1511 if (!mxvar_table[i].base) {
1512 GMStatus[i].dcd = 0;
1513 GMStatus[i].dsr = 0;
1514 GMStatus[i].cts = 0;
1515 continue;
1516 }
1517
1518 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1519 GMStatus[i].cflag = mxvar_table[i].normal_termios.c_cflag;
1520 else
1521 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1522
1523 status = inb(mxvar_table[i].base + UART_MSR);
1524 if (status & 0x80 /*UART_MSR_DCD */ )
1525 GMStatus[i].dcd = 1;
1526 else
1527 GMStatus[i].dcd = 0;
1528
1529 if (status & 0x20 /*UART_MSR_DSR */ )
1530 GMStatus[i].dsr = 1;
1531 else
1532 GMStatus[i].dsr = 0;
1533
1534
1535 if (status & 0x10 /*UART_MSR_CTS */ )
1536 GMStatus[i].cts = 1;
1537 else
1538 GMStatus[i].cts = 0;
1539 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001540 if (copy_to_user(argp, GMStatus,
1541 sizeof(struct mxser_mstatus) * MXSER_PORTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return -EFAULT;
1543 return 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001544 case MOXA_ASPP_MON_EXT: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 int status;
1546 int opmode, p;
1547 int shiftbit;
1548 unsigned cflag, iflag;
1549
1550 for (i = 0; i < MXSER_PORTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (!mxvar_table[i].base)
1552 continue;
1553
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001554 status = mxser_get_msr(mxvar_table[i].base, 0,
1555 i, &(mxvar_table[i]));
1556 /*
1557 mxser_check_modem_status(&mxvar_table[i],
1558 status);
1559 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (status & UART_MSR_TERI)
1561 mxvar_table[i].icount.rng++;
1562 if (status & UART_MSR_DDSR)
1563 mxvar_table[i].icount.dsr++;
1564 if (status & UART_MSR_DDCD)
1565 mxvar_table[i].icount.dcd++;
1566 if (status & UART_MSR_DCTS)
1567 mxvar_table[i].icount.cts++;
1568
1569 mxvar_table[i].mon_data.modem_status = status;
1570 mon_data_ext.rx_cnt[i] = mxvar_table[i].mon_data.rxcnt;
1571 mon_data_ext.tx_cnt[i] = mxvar_table[i].mon_data.txcnt;
1572 mon_data_ext.up_rxcnt[i] = mxvar_table[i].mon_data.up_rxcnt;
1573 mon_data_ext.up_txcnt[i] = mxvar_table[i].mon_data.up_txcnt;
1574 mon_data_ext.modem_status[i] = mxvar_table[i].mon_data.modem_status;
1575 mon_data_ext.baudrate[i] = mxvar_table[i].realbaud;
1576
1577 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios) {
1578 cflag = mxvar_table[i].normal_termios.c_cflag;
1579 iflag = mxvar_table[i].normal_termios.c_iflag;
1580 } else {
1581 cflag = mxvar_table[i].tty->termios->c_cflag;
1582 iflag = mxvar_table[i].tty->termios->c_iflag;
1583 }
1584
1585 mon_data_ext.databits[i] = cflag & CSIZE;
1586
1587 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1588
1589 mon_data_ext.parity[i] = cflag & (PARENB | PARODD | CMSPAR);
1590
1591 mon_data_ext.flowctrl[i] = 0x00;
1592
1593 if (cflag & CRTSCTS)
1594 mon_data_ext.flowctrl[i] |= 0x03;
1595
1596 if (iflag & (IXON | IXOFF))
1597 mon_data_ext.flowctrl[i] |= 0x0C;
1598
1599 if (mxvar_table[i].type == PORT_16550A)
1600 mon_data_ext.fifo[i] = 1;
1601 else
1602 mon_data_ext.fifo[i] = 0;
1603
1604 p = i % 4;
1605 shiftbit = p * 2;
1606 opmode = inb(mxvar_table[i].opmode_ioaddr) >> shiftbit;
1607 opmode &= OP_MODE_MASK;
1608
1609 mon_data_ext.iftype[i] = opmode;
1610
1611 }
1612 if (copy_to_user(argp, &mon_data_ext, sizeof(struct mxser_mon_ext)))
1613 return -EFAULT;
1614
1615 return 0;
1616
1617 }
1618 default:
1619 return -ENOIOCTLCMD;
1620 }
1621 return 0;
1622}
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624static void mxser_stoprx(struct tty_struct *tty)
1625{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001626 struct mxser_struct *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001627 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 info->ldisc_stop_rx = 1;
1630 if (I_IXOFF(tty)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001631 /* MX_LOCK(&info->slock); */
1632 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (info->IsMoxaMustChipFlag) {
1634 info->IER &= ~MOXA_MUST_RECV_ISR;
1635 outb(info->IER, info->base + UART_IER);
1636 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001637 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 info->x_char = STOP_CHAR(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001639 /* mask by Victor Yu. 09-02-2002 */
1640 /* outb(info->IER, 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 outb(0, info->base + UART_IER);
1642 info->IER |= UART_IER_THRI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001643 /* force Tx interrupt */
1644 outb(info->IER, info->base + UART_IER);
1645 } /* add by Victor Yu. 09-02-2002 */
1646 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
1648
1649 if (info->tty->termios->c_cflag & CRTSCTS) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001650 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 info->MCR &= ~UART_MCR_RTS;
1652 outb(info->MCR, info->base + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001653 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655}
1656
1657static void mxser_startrx(struct tty_struct *tty)
1658{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001659 struct mxser_struct *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001660 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 info->ldisc_stop_rx = 0;
1663 if (I_IXOFF(tty)) {
1664 if (info->x_char)
1665 info->x_char = 0;
1666 else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001667 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001669 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (info->IsMoxaMustChipFlag) {
1671 info->IER |= MOXA_MUST_RECV_ISR;
1672 outb(info->IER, info->base + UART_IER);
1673 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001674 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 info->x_char = START_CHAR(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001677 /* mask by Victor Yu. 09-02-2002 */
1678 /* outb(info->IER, 0); */
1679 /* add by Victor Yu. 09-02-2002 */
1680 outb(0, info->base + UART_IER);
1681 /* force Tx interrupt */
1682 info->IER |= UART_IER_THRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 outb(info->IER, info->base + UART_IER);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001684 } /* add by Victor Yu. 09-02-2002 */
1685 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
1687 }
1688
1689 if (info->tty->termios->c_cflag & CRTSCTS) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001690 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 info->MCR |= UART_MCR_RTS;
1692 outb(info->MCR, info->base + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001693 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695}
1696
1697/*
1698 * This routine is called by the upper-layer tty layer to signal that
1699 * incoming characters should be throttled.
1700 */
1701static void mxser_throttle(struct tty_struct *tty)
1702{
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001703 /* struct mxser_struct *info = tty->driver_data; */
1704 /* unsigned long flags; */
1705
1706 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001708 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
1711static void mxser_unthrottle(struct tty_struct *tty)
1712{
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001713 /* struct mxser_struct *info = tty->driver_data; */
1714 /* unsigned long flags; */
1715
1716 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 mxser_startrx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001718 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
Alan Cox606d0992006-12-08 02:38:45 -08001721static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001723 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 unsigned long flags;
1725
Alan Cox7e7d1362007-07-15 23:41:49 -07001726 mxser_change_speed(info, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Alan Cox7e7d1362007-07-15 23:41:49 -07001728 if ((old_termios->c_cflag & CRTSCTS) &&
1729 !(tty->termios->c_cflag & CRTSCTS)) {
1730 tty->hw_stopped = 0;
1731 mxser_start(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733
1734/* Handle sw stopped */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001735 if ((old_termios->c_iflag & IXON) &&
1736 !(tty->termios->c_iflag & IXON)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 tty->stopped = 0;
1738
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001739 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (info->IsMoxaMustChipFlag) {
1741 spin_lock_irqsave(&info->slock, flags);
1742 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
1743 spin_unlock_irqrestore(&info->slock, flags);
1744 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001745 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 mxser_start(tty);
1748 }
1749}
1750
1751/*
1752 * mxser_stop() and mxser_start()
1753 *
1754 * This routines are called before setting or resetting tty->stopped.
1755 * They enable or disable transmitter interrupts, as necessary.
1756 */
1757static void mxser_stop(struct tty_struct *tty)
1758{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001759 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 unsigned long flags;
1761
1762 spin_lock_irqsave(&info->slock, flags);
1763 if (info->IER & UART_IER_THRI) {
1764 info->IER &= ~UART_IER_THRI;
1765 outb(info->IER, info->base + UART_IER);
1766 }
1767 spin_unlock_irqrestore(&info->slock, flags);
1768}
1769
1770static void mxser_start(struct tty_struct *tty)
1771{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001772 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 unsigned long flags;
1774
1775 spin_lock_irqsave(&info->slock, flags);
1776 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
1777 info->IER |= UART_IER_THRI;
1778 outb(info->IER, info->base + UART_IER);
1779 }
1780 spin_unlock_irqrestore(&info->slock, flags);
1781}
1782
1783/*
1784 * mxser_wait_until_sent() --- wait until the transmitter is empty
1785 */
1786static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1787{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001788 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 unsigned long orig_jiffies, char_time;
1790 int lsr;
1791
1792 if (info->type == PORT_UNKNOWN)
1793 return;
1794
1795 if (info->xmit_fifo_size == 0)
1796 return; /* Just in case.... */
1797
1798 orig_jiffies = jiffies;
1799 /*
1800 * Set the check interval to be 1/5 of the estimated time to
1801 * send a single character, and make it at least 1. The check
1802 * interval should also be less than the timeout.
1803 *
1804 * Note: we have to use pretty tight timings here to satisfy
1805 * the NIST-PCTS.
1806 */
1807 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1808 char_time = char_time / 5;
1809 if (char_time == 0)
1810 char_time = 1;
1811 if (timeout && timeout < char_time)
1812 char_time = timeout;
1813 /*
1814 * If the transmitter hasn't cleared in twice the approximate
1815 * amount of time to send the entire FIFO, it probably won't
1816 * ever clear. This assumes the UART isn't doing flow
1817 * control, which is currently the case. Hence, if it ever
1818 * takes longer than info->timeout, this is probably due to a
1819 * UART bug of some kind. So, we clamp the timeout parameter at
1820 * 2*info->timeout.
1821 */
1822 if (!timeout || timeout > 2 * info->timeout)
1823 timeout = 2 * info->timeout;
1824#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001825 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1826 timeout, char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 printk("jiff=%lu...", jiffies);
1828#endif
1829 while (!((lsr = inb(info->base + UART_LSR)) & UART_LSR_TEMT)) {
1830#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1831 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1832#endif
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001833 schedule_timeout_interruptible(char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (signal_pending(current))
1835 break;
1836 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1837 break;
1838 }
1839 set_current_state(TASK_RUNNING);
1840
1841#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1842 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1843#endif
1844}
1845
1846
1847/*
1848 * This routine is called by tty_hangup() when a hangup is signaled.
1849 */
1850void mxser_hangup(struct tty_struct *tty)
1851{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001852 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 mxser_flush_buffer(tty);
1855 mxser_shutdown(info);
1856 info->event = 0;
1857 info->count = 0;
1858 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1859 info->tty = NULL;
1860 wake_up_interruptible(&info->open_wait);
1861}
1862
1863
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001864/* added by James 03-12-2004. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865/*
1866 * mxser_rs_break() --- routine which turns the break handling on or off
1867 */
1868static void mxser_rs_break(struct tty_struct *tty, int break_state)
1869{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001870 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 unsigned long flags;
1872
1873 spin_lock_irqsave(&info->slock, flags);
1874 if (break_state == -1)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001875 outb(inb(info->base + UART_LCR) | UART_LCR_SBC,
1876 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001878 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC,
1879 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 spin_unlock_irqrestore(&info->slock, flags);
1881}
1882
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001883/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885
1886/*
1887 * This is the serial driver's generic interrupt routine
1888 */
David Howells7d12e782006-10-05 14:55:46 +01001889static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
1891 int status, iir, i;
1892 struct mxser_struct *info;
1893 struct mxser_struct *port;
1894 int max, irqbits, bits, msr;
1895 int pass_counter = 0;
1896 int handled = IRQ_NONE;
1897
1898 port = NULL;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001899 /* spin_lock(&gm_lock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 for (i = 0; i < MXSER_BOARDS; i++) {
1902 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1903 port = dev_id;
1904 break;
1905 }
1906 }
1907
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001908 if (i == MXSER_BOARDS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 goto irq_stop;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001910 if (port == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 goto irq_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 max = mxser_numports[mxsercfg[i].board_type - 1];
1913 while (1) {
1914 irqbits = inb(port->vector) & port->vectormask;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001915 if (irqbits == port->vectormask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 handled = IRQ_HANDLED;
1919 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001920 if (irqbits == port->vectormask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (bits & irqbits)
1923 continue;
1924 info = port + i;
1925
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001926 /* following add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 iir = inb(info->base + UART_IIR);
1928 if (iir & UART_IIR_NO_INT)
1929 continue;
1930 iir &= MOXA_MUST_IIR_MASK;
1931 if (!info->tty) {
1932 status = inb(info->base + UART_LSR);
1933 outb(0x27, info->base + UART_FCR);
1934 inb(info->base + UART_MSR);
1935 continue;
1936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 /* mask by Victor Yu. 09-13-2002
1939 if ( !info->tty ||
1940 (inb(info->base + UART_IIR) & UART_IIR_NO_INT) )
1941 continue;
1942 */
1943 /* mask by Victor Yu. 09-02-2002
1944 status = inb(info->base + UART_LSR) & info->read_status_mask;
1945 */
1946
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001947 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 status = inb(info->base + UART_LSR);
1949
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001950 if (status & UART_LSR_PE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 info->err_shadow |= NPPI_NOTIFY_PARITY;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001952 if (status & UART_LSR_FE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 info->err_shadow |= NPPI_NOTIFY_FRAMING;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001954 if (status & UART_LSR_OE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 info->err_shadow |= NPPI_NOTIFY_HW_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (status & UART_LSR_BI)
1957 info->err_shadow |= NPPI_NOTIFY_BREAK;
1958
1959 if (info->IsMoxaMustChipFlag) {
1960 /*
1961 if ( (status & 0x02) && !(status & 0x01) ) {
1962 outb(info->base+UART_FCR, 0x23);
1963 continue;
1964 }
1965 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001966 if (iir == MOXA_MUST_IIR_GDA ||
1967 iir == MOXA_MUST_IIR_RDA ||
1968 iir == MOXA_MUST_IIR_RTO ||
1969 iir == MOXA_MUST_IIR_LSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 mxser_receive_chars(info, &status);
1971
1972 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001973 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 status &= info->read_status_mask;
1976 if (status & UART_LSR_DR)
1977 mxser_receive_chars(info, &status);
1978 }
1979 msr = inb(info->base + UART_MSR);
1980 if (msr & UART_MSR_ANY_DELTA) {
1981 mxser_check_modem_status(info, msr);
1982 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001983 /* following add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (info->IsMoxaMustChipFlag) {
1985 if ((iir == 0x02) && (status & UART_LSR_THRE)) {
1986 mxser_transmit_chars(info);
1987 }
1988 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001989 /* above add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 if (status & UART_LSR_THRE) {
1992/* 8-2-99 by William
1993 if ( info->x_char || (info->xmit_cnt > 0) )
1994*/
1995 mxser_transmit_chars(info);
1996 }
1997 }
1998 }
1999 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
2000 break; /* Prevent infinite loops */
2001 }
2002 }
2003
2004 irq_stop:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002005 /* spin_unlock(&gm_lock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return handled;
2007}
2008
2009static void mxser_receive_chars(struct mxser_struct *info, int *status)
2010{
2011 struct tty_struct *tty = info->tty;
2012 unsigned char ch, gdl;
2013 int ignored = 0;
2014 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 int recv_room;
2016 int max = 256;
2017 unsigned long flags;
2018
2019 spin_lock_irqsave(&info->slock, flags);
2020
Alan Cox33f0f882006-01-09 20:54:13 -08002021 recv_room = tty->receive_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if ((recv_room == 0) && (!info->ldisc_stop_rx)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002023 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002025 /* return; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002028 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
2030
2031 if (*status & UART_LSR_SPECIAL) {
2032 goto intr_old;
2033 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002034 /* following add by Victor Yu. 02-11-2004 */
2035 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID &&
2036 (*status & MOXA_MUST_LSR_RERR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 goto intr_old;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002038 /* above add by Victor Yu. 02-14-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (*status & MOXA_MUST_LSR_RERR)
2040 goto intr_old;
2041
2042 gdl = inb(info->base + MOXA_MUST_GDL_REGISTER);
2043
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002044 /* add by Victor Yu. 02-11-2004 */
2045 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU150_HWID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 gdl &= MOXA_MUST_GDL_MASK;
2047 if (gdl >= recv_room) {
2048 if (!info->ldisc_stop_rx) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002049 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 mxser_stoprx(tty);
2051 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002052 /* return; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054 while (gdl--) {
2055 ch = inb(info->base + UART_RX);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002056 tty_insert_flip_char(tty, ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 cnt++;
2058 /*
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002059 if ((cnt >= HI_WATER) && (info->stop_rx == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002061 info->stop_rx = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 break;
2063 } */
2064 }
2065 goto end_intr;
2066 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002067 intr_old:
2068 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 do {
2071 if (max-- < 0)
2072 break;
2073 /*
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002074 if ((cnt >= HI_WATER) && (info->stop_rx == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 mxser_stoprx(tty);
2076 info->stop_rx=1;
2077 break;
2078 }
2079 */
2080
2081 ch = inb(info->base + UART_RX);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002082 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (info->IsMoxaMustChipFlag && (*status & UART_LSR_OE) /*&& !(*status&UART_LSR_DR) */ )
2084 outb(0x23, info->base + UART_FCR);
2085 *status &= info->read_status_mask;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002086 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (*status & info->ignore_status_mask) {
2088 if (++ignored > 100)
2089 break;
2090 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002091 char flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (*status & UART_LSR_SPECIAL) {
2093 if (*status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002094 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095/* added by casper 1/11/2000 */
2096 info->icount.brk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097/* */
2098 if (info->flags & ASYNC_SAK)
2099 do_SAK(tty);
2100 } else if (*status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002101 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102/* added by casper 1/11/2000 */
2103 info->icount.parity++;
2104/* */
2105 } else if (*status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002106 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107/* added by casper 1/11/2000 */
2108 info->icount.frame++;
2109/* */
2110 } else if (*status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002111 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112/* added by casper 1/11/2000 */
2113 info->icount.overrun++;
2114/* */
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002115 }
2116 }
2117 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 cnt++;
2119 if (cnt >= recv_room) {
2120 if (!info->ldisc_stop_rx) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002121 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 mxser_stoprx(tty);
2123 }
2124 break;
2125 }
2126
2127 }
2128
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002129 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (info->IsMoxaMustChipFlag)
2131 break;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002132 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 /* mask by Victor Yu. 09-02-2002
2135 *status = inb(info->base + UART_LSR) & info->read_status_mask;
2136 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002137 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 *status = inb(info->base + UART_LSR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002139 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 } while (*status & UART_LSR_DR);
2141
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002142end_intr: /* add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 mxvar_log.rxcnt[info->port] += cnt;
2144 info->mon_data.rxcnt += cnt;
2145 info->mon_data.up_rxcnt += cnt;
2146 spin_unlock_irqrestore(&info->slock, flags);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 tty_flip_buffer_push(tty);
2149}
2150
2151static void mxser_transmit_chars(struct mxser_struct *info)
2152{
2153 int count, cnt;
2154 unsigned long flags;
2155
2156 spin_lock_irqsave(&info->slock, flags);
2157
2158 if (info->x_char) {
2159 outb(info->x_char, info->base + UART_TX);
2160 info->x_char = 0;
2161 mxvar_log.txcnt[info->port]++;
2162 info->mon_data.txcnt++;
2163 info->mon_data.up_txcnt++;
2164
2165/* added by casper 1/11/2000 */
2166 info->icount.tx++;
2167/* */
2168 spin_unlock_irqrestore(&info->slock, flags);
2169 return;
2170 }
2171
2172 if (info->xmit_buf == 0) {
2173 spin_unlock_irqrestore(&info->slock, flags);
2174 return;
2175 }
2176
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002177 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
2178 (info->tty->hw_stopped &&
2179 (info->type != PORT_16550A) &&
2180 (!info->IsMoxaMustChipFlag))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 info->IER &= ~UART_IER_THRI;
2182 outb(info->IER, info->base + UART_IER);
2183 spin_unlock_irqrestore(&info->slock, flags);
2184 return;
2185 }
2186
2187 cnt = info->xmit_cnt;
2188 count = info->xmit_fifo_size;
2189 do {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002190 outb(info->xmit_buf[info->xmit_tail++],
2191 info->base + UART_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2193 if (--info->xmit_cnt <= 0)
2194 break;
2195 } while (--count > 0);
2196 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
2197
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002198/* added by James 03-12-2004. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 info->mon_data.txcnt += (cnt - info->xmit_cnt);
2200 info->mon_data.up_txcnt += (cnt - info->xmit_cnt);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002201/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203/* added by casper 1/11/2000 */
2204 info->icount.tx += (cnt - info->xmit_cnt);
2205/* */
2206
2207 if (info->xmit_cnt < WAKEUP_CHARS) {
2208 set_bit(MXSER_EVENT_TXLOW, &info->event);
2209 schedule_work(&info->tqueue);
2210 }
2211 if (info->xmit_cnt <= 0) {
2212 info->IER &= ~UART_IER_THRI;
2213 outb(info->IER, info->base + UART_IER);
2214 }
2215 spin_unlock_irqrestore(&info->slock, flags);
2216}
2217
2218static void mxser_check_modem_status(struct mxser_struct *info, int status)
2219{
2220 /* update input line counters */
2221 if (status & UART_MSR_TERI)
2222 info->icount.rng++;
2223 if (status & UART_MSR_DDSR)
2224 info->icount.dsr++;
2225 if (status & UART_MSR_DDCD)
2226 info->icount.dcd++;
2227 if (status & UART_MSR_DCTS)
2228 info->icount.cts++;
2229 info->mon_data.modem_status = status;
2230 wake_up_interruptible(&info->delta_msr_wait);
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
2233 if (status & UART_MSR_DCD)
2234 wake_up_interruptible(&info->open_wait);
2235 schedule_work(&info->tqueue);
2236 }
2237
2238 if (info->flags & ASYNC_CTS_FLOW) {
2239 if (info->tty->hw_stopped) {
2240 if (status & UART_MSR_CTS) {
2241 info->tty->hw_stopped = 0;
2242
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002243 if ((info->type != PORT_16550A) &&
2244 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 info->IER |= UART_IER_THRI;
2246 outb(info->IER, info->base + UART_IER);
2247 }
2248 set_bit(MXSER_EVENT_TXLOW, &info->event);
2249 schedule_work(&info->tqueue); }
2250 } else {
2251 if (!(status & UART_MSR_CTS)) {
2252 info->tty->hw_stopped = 1;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002253 if ((info->type != PORT_16550A) &&
2254 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 info->IER &= ~UART_IER_THRI;
2256 outb(info->IER, info->base + UART_IER);
2257 }
2258 }
2259 }
2260 }
2261}
2262
2263static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_struct *info)
2264{
2265 DECLARE_WAITQUEUE(wait, current);
2266 int retval;
2267 int do_clocal = 0;
2268 unsigned long flags;
2269
2270 /*
2271 * If non-blocking mode is set, or the port is not enabled,
2272 * then make the check up front and then exit.
2273 */
2274 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
2275 info->flags |= ASYNC_NORMAL_ACTIVE;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
2278
2279 if (tty->termios->c_cflag & CLOCAL)
2280 do_clocal = 1;
2281
2282 /*
2283 * Block waiting for the carrier detect and the line to become
2284 * free (i.e., not in use by the callout). While we are in
2285 * this loop, info->count is dropped by one, so that
2286 * mxser_close() knows when to free things. We restore it upon
2287 * exit, either normal or abnormal.
2288 */
2289 retval = 0;
2290 add_wait_queue(&info->open_wait, &wait);
2291
2292 spin_lock_irqsave(&info->slock, flags);
2293 if (!tty_hung_up_p(filp))
2294 info->count--;
2295 spin_unlock_irqrestore(&info->slock, flags);
2296 info->blocked_open++;
2297 while (1) {
2298 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002299 outb(inb(info->base + UART_MCR) |
2300 UART_MCR_DTR | UART_MCR_RTS, info->base + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 spin_unlock_irqrestore(&info->slock, flags);
2302 set_current_state(TASK_INTERRUPTIBLE);
2303 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
2304 if (info->flags & ASYNC_HUP_NOTIFY)
2305 retval = -EAGAIN;
2306 else
2307 retval = -ERESTARTSYS;
2308 break;
2309 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002310 if (!(info->flags & ASYNC_CLOSING) &&
2311 (do_clocal ||
2312 (inb(info->base + UART_MSR) & UART_MSR_DCD)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 break;
2314 if (signal_pending(current)) {
2315 retval = -ERESTARTSYS;
2316 break;
2317 }
2318 schedule();
2319 }
2320 set_current_state(TASK_RUNNING);
2321 remove_wait_queue(&info->open_wait, &wait);
2322 if (!tty_hung_up_p(filp))
2323 info->count++;
2324 info->blocked_open--;
2325 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002326 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 info->flags |= ASYNC_NORMAL_ACTIVE;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
2331static int mxser_startup(struct mxser_struct *info)
2332{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 unsigned long page;
2334 unsigned long flags;
2335
2336 page = __get_free_page(GFP_KERNEL);
2337 if (!page)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002338 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340 spin_lock_irqsave(&info->slock, flags);
2341
2342 if (info->flags & ASYNC_INITIALIZED) {
2343 free_page(page);
2344 spin_unlock_irqrestore(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
2347
2348 if (!info->base || !info->type) {
2349 if (info->tty)
2350 set_bit(TTY_IO_ERROR, &info->tty->flags);
2351 free_page(page);
2352 spin_unlock_irqrestore(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355 if (info->xmit_buf)
2356 free_page(page);
2357 else
2358 info->xmit_buf = (unsigned char *) page;
2359
2360 /*
2361 * Clear the FIFO buffers and disable them
2362 * (they will be reenabled in mxser_change_speed())
2363 */
2364 if (info->IsMoxaMustChipFlag)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002365 outb((UART_FCR_CLEAR_RCVR |
2366 UART_FCR_CLEAR_XMIT |
2367 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002369 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2370 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 /*
2373 * At this point there's no way the LSR could still be 0xFF;
2374 * if it is, then bail out, because there's likely no UART
2375 * here.
2376 */
2377 if (inb(info->base + UART_LSR) == 0xff) {
2378 spin_unlock_irqrestore(&info->slock, flags);
2379 if (capable(CAP_SYS_ADMIN)) {
2380 if (info->tty)
2381 set_bit(TTY_IO_ERROR, &info->tty->flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 } else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002384 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
2386
2387 /*
2388 * Clear the interrupt registers.
2389 */
2390 (void) inb(info->base + UART_LSR);
2391 (void) inb(info->base + UART_RX);
2392 (void) inb(info->base + UART_IIR);
2393 (void) inb(info->base + UART_MSR);
2394
2395 /*
2396 * Now, initialize the UART
2397 */
2398 outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
2399 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
2400 outb(info->MCR, info->base + UART_MCR);
2401
2402 /*
2403 * Finally, enable interrupts
2404 */
2405 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002406 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002408 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (info->IsMoxaMustChipFlag)
2410 info->IER |= MOXA_MUST_IER_EGDAI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002411 /* above add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 outb(info->IER, info->base + UART_IER); /* enable interrupts */
2413
2414 /*
2415 * And clear the interrupt registers again for luck.
2416 */
2417 (void) inb(info->base + UART_LSR);
2418 (void) inb(info->base + UART_RX);
2419 (void) inb(info->base + UART_IIR);
2420 (void) inb(info->base + UART_MSR);
2421
2422 if (info->tty)
2423 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2424 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2425
2426 /*
2427 * and set the speed of the serial port
2428 */
2429 spin_unlock_irqrestore(&info->slock, flags);
2430 mxser_change_speed(info, NULL);
2431
2432 info->flags |= ASYNC_INITIALIZED;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
2436/*
2437 * This routine will shutdown a serial port; interrupts maybe disabled, and
2438 * DTR is dropped if the hangup on close termio flag is on.
2439 */
2440static void mxser_shutdown(struct mxser_struct *info)
2441{
2442 unsigned long flags;
2443
2444 if (!(info->flags & ASYNC_INITIALIZED))
2445 return;
2446
2447 spin_lock_irqsave(&info->slock, flags);
2448
2449 /*
2450 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2451 * here so the queue might never be waken up
2452 */
2453 wake_up_interruptible(&info->delta_msr_wait);
2454
2455 /*
2456 * Free the IRQ, if necessary
2457 */
2458 if (info->xmit_buf) {
2459 free_page((unsigned long) info->xmit_buf);
2460 info->xmit_buf = NULL;
2461 }
2462
2463 info->IER = 0;
2464 outb(0x00, info->base + UART_IER);
2465
2466 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
2467 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
2468 outb(info->MCR, info->base + UART_MCR);
2469
2470 /* clear Rx/Tx FIFO's */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002471 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 if (info->IsMoxaMustChipFlag)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002473 outb((UART_FCR_CLEAR_RCVR |
2474 UART_FCR_CLEAR_XMIT |
2475 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002477 /* above add by Victor Yu. 08-30-2002 */
2478 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2479 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 /* read data port to reset things */
2482 (void) inb(info->base + UART_RX);
2483
2484 if (info->tty)
2485 set_bit(TTY_IO_ERROR, &info->tty->flags);
2486
2487 info->flags &= ~ASYNC_INITIALIZED;
2488
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002489 /* following add by Victor Yu. 09-23-2002 */
2490 if (info->IsMoxaMustChipFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->base);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002492 /* above add by Victor Yu. 09-23-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 spin_unlock_irqrestore(&info->slock, flags);
2495}
2496
2497/*
2498 * This routine is called to set the UART divisor registers to match
2499 * the specified baud rate for a serial port.
2500 */
Alan Cox606d0992006-12-08 02:38:45 -08002501static int mxser_change_speed(struct mxser_struct *info, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
2503 unsigned cflag, cval, fcr;
2504 int ret = 0;
2505 unsigned char status;
2506 long baud;
2507 unsigned long flags;
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 if (!info->tty || !info->tty->termios)
2510 return ret;
2511 cflag = info->tty->termios->c_cflag;
2512 if (!(info->base))
2513 return ret;
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515#ifndef B921600
2516#define B921600 (B460800 +1)
2517#endif
2518 if (mxser_set_baud_method[info->port] == 0) {
Alan Coxc7bce302006-09-30 23:27:24 -07002519 baud = tty_get_baud_rate(info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 mxser_set_baud(info, baud);
2521 }
2522
2523 /* byte size and parity */
2524 switch (cflag & CSIZE) {
2525 case CS5:
2526 cval = 0x00;
2527 break;
2528 case CS6:
2529 cval = 0x01;
2530 break;
2531 case CS7:
2532 cval = 0x02;
2533 break;
2534 case CS8:
2535 cval = 0x03;
2536 break;
2537 default:
2538 cval = 0x00;
2539 break; /* too keep GCC shut... */
2540 }
2541 if (cflag & CSTOPB)
2542 cval |= 0x04;
2543 if (cflag & PARENB)
2544 cval |= UART_LCR_PARITY;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002545 if (!(cflag & PARODD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 cval |= UART_LCR_EPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (cflag & CMSPAR)
2548 cval |= UART_LCR_SPAR;
2549
2550 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
2551 if (info->IsMoxaMustChipFlag) {
2552 fcr = UART_FCR_ENABLE_FIFO;
2553 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2554 SET_MOXA_MUST_FIFO_VALUE(info);
2555 } else
2556 fcr = 0;
2557 } else {
2558 fcr = UART_FCR_ENABLE_FIFO;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002559 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 if (info->IsMoxaMustChipFlag) {
2561 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2562 SET_MOXA_MUST_FIFO_VALUE(info);
2563 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002564 /* above add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 switch (info->rx_trigger) {
2566 case 1:
2567 fcr |= UART_FCR_TRIGGER_1;
2568 break;
2569 case 4:
2570 fcr |= UART_FCR_TRIGGER_4;
2571 break;
2572 case 8:
2573 fcr |= UART_FCR_TRIGGER_8;
2574 break;
2575 default:
2576 fcr |= UART_FCR_TRIGGER_14;
2577 break;
2578 }
2579 }
2580 }
2581
2582 /* CTS flow control flag and modem status interrupts */
2583 info->IER &= ~UART_IER_MSI;
2584 info->MCR &= ~UART_MCR_AFE;
2585 if (cflag & CRTSCTS) {
2586 info->flags |= ASYNC_CTS_FLOW;
2587 info->IER |= UART_IER_MSI;
2588 if ((info->type == PORT_16550A) || (info->IsMoxaMustChipFlag)) {
2589 info->MCR |= UART_MCR_AFE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 status = inb(info->base + UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 if (info->tty->hw_stopped) {
2593 if (status & UART_MSR_CTS) {
2594 info->tty->hw_stopped = 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002595 if ((info->type != PORT_16550A) &&
2596 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 info->IER |= UART_IER_THRI;
2598 outb(info->IER, info->base + UART_IER);
2599 }
2600 set_bit(MXSER_EVENT_TXLOW, &info->event);
2601 schedule_work(&info->tqueue); }
2602 } else {
2603 if (!(status & UART_MSR_CTS)) {
2604 info->tty->hw_stopped = 1;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002605 if ((info->type != PORT_16550A) &&
2606 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 info->IER &= ~UART_IER_THRI;
2608 outb(info->IER, info->base + UART_IER);
2609 }
2610 }
2611 }
2612 }
2613 } else {
2614 info->flags &= ~ASYNC_CTS_FLOW;
2615 }
2616 outb(info->MCR, info->base + UART_MCR);
2617 if (cflag & CLOCAL) {
2618 info->flags &= ~ASYNC_CHECK_CD;
2619 } else {
2620 info->flags |= ASYNC_CHECK_CD;
2621 info->IER |= UART_IER_MSI;
2622 }
2623 outb(info->IER, info->base + UART_IER);
2624
2625 /*
2626 * Set up parity check flag
2627 */
2628 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2629 if (I_INPCK(info->tty))
2630 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2631 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2632 info->read_status_mask |= UART_LSR_BI;
2633
2634 info->ignore_status_mask = 0;
2635
2636 if (I_IGNBRK(info->tty)) {
2637 info->ignore_status_mask |= UART_LSR_BI;
2638 info->read_status_mask |= UART_LSR_BI;
2639 /*
2640 * If we're ignore parity and break indicators, ignore
2641 * overruns too. (For real raw support).
2642 */
2643 if (I_IGNPAR(info->tty)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002644 info->ignore_status_mask |=
2645 UART_LSR_OE |
2646 UART_LSR_PE |
2647 UART_LSR_FE;
2648 info->read_status_mask |=
2649 UART_LSR_OE |
2650 UART_LSR_PE |
2651 UART_LSR_FE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 }
2653 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002654 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 if (info->IsMoxaMustChipFlag) {
2656 spin_lock_irqsave(&info->slock, flags);
2657 SET_MOXA_MUST_XON1_VALUE(info->base, START_CHAR(info->tty));
2658 SET_MOXA_MUST_XOFF1_VALUE(info->base, STOP_CHAR(info->tty));
2659 if (I_IXON(info->tty)) {
2660 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2661 } else {
2662 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2663 }
2664 if (I_IXOFF(info->tty)) {
2665 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2666 } else {
2667 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2668 }
2669 /*
2670 if ( I_IXANY(info->tty) ) {
2671 info->MCR |= MOXA_MUST_MCR_XON_ANY;
2672 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2673 } else {
2674 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
2675 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2676 }
2677 */
2678 spin_unlock_irqrestore(&info->slock, flags);
2679 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002680 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682
2683 outb(fcr, info->base + UART_FCR); /* set fcr */
2684 outb(cval, info->base + UART_LCR);
2685
2686 return ret;
2687}
2688
2689
2690static int mxser_set_baud(struct mxser_struct *info, long newspd)
2691{
2692 int quot = 0;
2693 unsigned char cval;
2694 int ret = 0;
2695 unsigned long flags;
2696
2697 if (!info->tty || !info->tty->termios)
2698 return ret;
2699
2700 if (!(info->base))
2701 return ret;
2702
2703 if (newspd > info->MaxCanSetBaudRate)
2704 return 0;
2705
2706 info->realbaud = newspd;
2707 if (newspd == 134) {
2708 quot = (2 * info->baud_base / 269);
2709 } else if (newspd) {
2710 quot = info->baud_base / newspd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (quot == 0)
2712 quot = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 } else {
2714 quot = 0;
2715 }
2716
2717 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
2718 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2719
2720 if (quot) {
2721 spin_lock_irqsave(&info->slock, flags);
2722 info->MCR |= UART_MCR_DTR;
2723 outb(info->MCR, info->base + UART_MCR);
2724 spin_unlock_irqrestore(&info->slock, flags);
2725 } else {
2726 spin_lock_irqsave(&info->slock, flags);
2727 info->MCR &= ~UART_MCR_DTR;
2728 outb(info->MCR, info->base + UART_MCR);
2729 spin_unlock_irqrestore(&info->slock, flags);
2730 return ret;
2731 }
2732
2733 cval = inb(info->base + UART_LCR);
2734
2735 outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2736
2737 outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2738 outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2739 outb(cval, info->base + UART_LCR); /* reset DLAB */
2740
2741
2742 return ret;
2743}
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745/*
2746 * ------------------------------------------------------------
2747 * friends of mxser_ioctl()
2748 * ------------------------------------------------------------
2749 */
2750static int mxser_get_serial_info(struct mxser_struct *info, struct serial_struct __user *retinfo)
2751{
2752 struct serial_struct tmp;
2753
2754 if (!retinfo)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002755 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 memset(&tmp, 0, sizeof(tmp));
2757 tmp.type = info->type;
2758 tmp.line = info->port;
2759 tmp.port = info->base;
2760 tmp.irq = info->irq;
2761 tmp.flags = info->flags;
2762 tmp.baud_base = info->baud_base;
2763 tmp.close_delay = info->close_delay;
2764 tmp.closing_wait = info->closing_wait;
2765 tmp.custom_divisor = info->custom_divisor;
2766 tmp.hub6 = 0;
2767 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2768 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770}
2771
2772static int mxser_set_serial_info(struct mxser_struct *info, struct serial_struct __user *new_info)
2773{
2774 struct serial_struct new_serial;
2775 unsigned int flags;
2776 int retval = 0;
2777
2778 if (!new_info || !info->base)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002779 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2781 return -EFAULT;
2782
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002783 if ((new_serial.irq != info->irq) ||
2784 (new_serial.port != info->base) ||
2785 (new_serial.custom_divisor != info->custom_divisor) ||
2786 (new_serial.baud_base != info->baud_base))
2787 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 flags = info->flags & ASYNC_SPD_MASK;
2790
2791 if (!capable(CAP_SYS_ADMIN)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002792 if ((new_serial.baud_base != info->baud_base) ||
2793 (new_serial.close_delay != info->close_delay) ||
2794 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
2795 return -EPERM;
2796 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2797 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 } else {
2799 /*
2800 * OK, past this point, all the error checking has been done.
2801 * At this point, we start making changes.....
2802 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002803 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2804 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 info->close_delay = new_serial.close_delay * HZ / 100;
2806 info->closing_wait = new_serial.closing_wait * HZ / 100;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002807 info->tty->low_latency =
2808 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2809 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
2811
2812 /* added by casper, 3/17/2000, for mouse */
2813 info->type = new_serial.type;
2814
2815 process_txrx_fifo(info);
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if (info->flags & ASYNC_INITIALIZED) {
2818 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2819 mxser_change_speed(info, NULL);
2820 }
2821 } else {
2822 retval = mxser_startup(info);
2823 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002824 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825}
2826
2827/*
2828 * mxser_get_lsr_info - get line status register info
2829 *
2830 * Purpose: Let user call ioctl() to get info when the UART physically
2831 * is emptied. On bus types like RS485, the transmitter must
2832 * release the bus after transmitting. This must be done when
2833 * the transmit shift register is empty, not be done when the
2834 * transmit holding register is empty. This functionality
2835 * allows an RS485 driver to be written in user space.
2836 */
2837static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int __user *value)
2838{
2839 unsigned char status;
2840 unsigned int result;
2841 unsigned long flags;
2842
2843 spin_lock_irqsave(&info->slock, flags);
2844 status = inb(info->base + UART_LSR);
2845 spin_unlock_irqrestore(&info->slock, flags);
2846 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2847 return put_user(result, value);
2848}
2849
2850/*
2851 * This routine sends a break character out the serial port.
2852 */
2853static void mxser_send_break(struct mxser_struct *info, int duration)
2854{
2855 unsigned long flags;
2856
2857 if (!info->base)
2858 return;
2859 set_current_state(TASK_INTERRUPTIBLE);
2860 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002861 outb(inb(info->base + UART_LCR) | UART_LCR_SBC,
2862 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 spin_unlock_irqrestore(&info->slock, flags);
2864 schedule_timeout(duration);
2865 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002866 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC,
2867 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 spin_unlock_irqrestore(&info->slock, flags);
2869}
2870
2871static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2872{
Jesper Juhl56e139f2006-06-25 05:47:52 -07002873 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 unsigned char control, status;
2875 unsigned long flags;
2876
2877
2878 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002879 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (tty->flags & (1 << TTY_IO_ERROR))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002881 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 control = info->MCR;
2884
2885 spin_lock_irqsave(&info->slock, flags);
2886 status = inb(info->base + UART_MSR);
2887 if (status & UART_MSR_ANY_DELTA)
2888 mxser_check_modem_status(info, status);
2889 spin_unlock_irqrestore(&info->slock, flags);
2890 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002891 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2892 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2893 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2894 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2895 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896}
2897
2898static int mxser_tiocmset(struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear)
2899{
Jesper Juhl56e139f2006-06-25 05:47:52 -07002900 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 unsigned long flags;
2902
2903
2904 if (tty->index == MXSER_PORTS)
2905 return -ENOIOCTLCMD;
2906 if (tty->flags & (1 << TTY_IO_ERROR))
2907 return -EIO;
2908
2909 spin_lock_irqsave(&info->slock, flags);
2910
2911 if (set & TIOCM_RTS)
2912 info->MCR |= UART_MCR_RTS;
2913 if (set & TIOCM_DTR)
2914 info->MCR |= UART_MCR_DTR;
2915
2916 if (clear & TIOCM_RTS)
2917 info->MCR &= ~UART_MCR_RTS;
2918 if (clear & TIOCM_DTR)
2919 info->MCR &= ~UART_MCR_DTR;
2920
2921 outb(info->MCR, info->base + UART_MCR);
2922 spin_unlock_irqrestore(&info->slock, flags);
2923 return 0;
2924}
2925
2926
2927static int mxser_read_register(int, unsigned short *);
2928static int mxser_program_mode(int);
2929static void mxser_normal_mode(int);
2930
2931static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2932{
2933 int id, i, bits;
2934 unsigned short regs[16], irq;
2935 unsigned char scratch, scratch2;
2936
2937 hwconf->IsMoxaMustChipFlag = MOXA_OTHER_UART;
2938
2939 id = mxser_read_register(cap, regs);
2940 if (id == C168_ASIC_ID) {
2941 hwconf->board_type = MXSER_BOARD_C168_ISA;
2942 hwconf->ports = 8;
2943 } else if (id == C104_ASIC_ID) {
2944 hwconf->board_type = MXSER_BOARD_C104_ISA;
2945 hwconf->ports = 4;
2946 } else if (id == C102_ASIC_ID) {
2947 hwconf->board_type = MXSER_BOARD_C102_ISA;
2948 hwconf->ports = 2;
2949 } else if (id == CI132_ASIC_ID) {
2950 hwconf->board_type = MXSER_BOARD_CI132;
2951 hwconf->ports = 2;
2952 } else if (id == CI134_ASIC_ID) {
2953 hwconf->board_type = MXSER_BOARD_CI134;
2954 hwconf->ports = 4;
2955 } else if (id == CI104J_ASIC_ID) {
2956 hwconf->board_type = MXSER_BOARD_CI104J;
2957 hwconf->ports = 4;
2958 } else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
2961 irq = 0;
2962 if (hwconf->ports == 2) {
2963 irq = regs[9] & 0xF000;
2964 irq = irq | (irq >> 4);
2965 if (irq != (regs[9] & 0xFF00))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002966 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 } else if (hwconf->ports == 4) {
2968 irq = regs[9] & 0xF000;
2969 irq = irq | (irq >> 4);
2970 irq = irq | (irq >> 8);
2971 if (irq != regs[9])
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002972 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 } else if (hwconf->ports == 8) {
2974 irq = regs[9] & 0xF000;
2975 irq = irq | (irq >> 4);
2976 irq = irq | (irq >> 8);
2977 if ((irq != regs[9]) || (irq != regs[10]))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002978 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
2980
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002981 if (!irq)
2982 return MXSER_ERR_IRQ;
2983 hwconf->irq = ((int)(irq & 0xF000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 for (i = 0; i < 8; i++)
2985 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002986 if ((regs[12] & 0x80) == 0)
2987 return MXSER_ERR_VECTOR;
2988 hwconf->vector = (int)regs[11]; /* interrupt vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 if (id == 1)
2990 hwconf->vector_mask = 0x00FF;
2991 else
2992 hwconf->vector_mask = 0x000F;
2993 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2994 if (regs[12] & bits) {
2995 hwconf->baud_base[i] = 921600;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002996 hwconf->MaxCanSetBaudRate[i] = 921600; /* add by Victor Yu. 09-04-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 } else {
2998 hwconf->baud_base[i] = 115200;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002999 hwconf->MaxCanSetBaudRate[i] = 115200; /* add by Victor Yu. 09-04-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
3001 }
3002 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
3003 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
3004 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
3005 outb(scratch2, cap + UART_LCR);
3006 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
3007 scratch = inb(cap + UART_IIR);
3008
3009 if (scratch & 0xC0)
3010 hwconf->uart_type = PORT_16550A;
3011 else
3012 hwconf->uart_type = PORT_16450;
3013 if (id == 1)
3014 hwconf->ports = 8;
3015 else
3016 hwconf->ports = 4;
3017 request_region(hwconf->ioaddr[0], 8 * hwconf->ports, "mxser(IO)");
3018 request_region(hwconf->vector, 1, "mxser(vector)");
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003019 return hwconf->ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021
3022#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
3023#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
3024#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
3025#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
3026#define EN_CCMD 0x000 /* Chip's command register */
3027#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
3028#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
3029#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
3030#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
3031#define EN0_DCFG 0x00E /* Data configuration reg WR */
3032#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
3033#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
3034#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
3035static int mxser_read_register(int port, unsigned short *regs)
3036{
3037 int i, k, value, id;
3038 unsigned int j;
3039
3040 id = mxser_program_mode(port);
3041 if (id < 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003042 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 for (i = 0; i < 14; i++) {
3044 k = (i & 0x3F) | 0x180;
3045 for (j = 0x100; j > 0; j >>= 1) {
3046 outb(CHIP_CS, port);
3047 if (k & j) {
3048 outb(CHIP_CS | CHIP_DO, port);
3049 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
3050 } else {
3051 outb(CHIP_CS, port);
3052 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
3053 }
3054 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003055 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 value = 0;
3057 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
3058 outb(CHIP_CS, port);
3059 outb(CHIP_CS | CHIP_SK, port);
3060 if (inb(port) & CHIP_DI)
3061 value |= j;
3062 }
3063 regs[i] = value;
3064 outb(0, port);
3065 }
3066 mxser_normal_mode(port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003067 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
3070static int mxser_program_mode(int port)
3071{
3072 int id, i, j, n;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003073 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
3075 spin_lock(&gm_lock);
3076 outb(0, port);
3077 outb(0, port);
3078 outb(0, port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003079 (void)inb(port);
3080 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 outb(0, port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003082 (void)inb(port);
3083 /* restore_flags(flags); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 spin_unlock(&gm_lock);
3085
3086 id = inb(port + 1) & 0x1F;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003087 if ((id != C168_ASIC_ID) &&
3088 (id != C104_ASIC_ID) &&
3089 (id != C102_ASIC_ID) &&
3090 (id != CI132_ASIC_ID) &&
3091 (id != CI134_ASIC_ID) &&
3092 (id != CI104J_ASIC_ID))
3093 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 for (i = 0, j = 0; i < 4; i++) {
3095 n = inb(port + 2);
3096 if (n == 'M') {
3097 j = 1;
3098 } else if ((j == 1) && (n == 1)) {
3099 j = 2;
3100 break;
3101 } else
3102 j = 0;
3103 }
3104 if (j != 2)
3105 id = -2;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003106 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
3109static void mxser_normal_mode(int port)
3110{
3111 int i, n;
3112
3113 outb(0xA5, port + 1);
3114 outb(0x80, port + 3);
3115 outb(12, port + 0); /* 9600 bps */
3116 outb(0, port + 1);
3117 outb(0x03, port + 3); /* 8 data bits */
3118 outb(0x13, port + 4); /* loop back mode */
3119 for (i = 0; i < 16; i++) {
3120 n = inb(port + 5);
3121 if ((n & 0x61) == 0x60)
3122 break;
3123 if ((n & 1) == 1)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003124 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 }
3126 outb(0x00, port + 4);
3127}
3128
3129module_init(mxser_module_init);
3130module_exit(mxser_module_exit);