blob: a61fb6da5d03f0b757b93c45893f01a8d031ddc8 [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/autoconf.h>
41#include <linux/errno.h>
42#include <linux/signal.h>
43#include <linux/sched.h>
44#include <linux/timer.h>
45#include <linux/interrupt.h>
46#include <linux/tty.h>
47#include <linux/tty_flip.h>
48#include <linux/serial.h>
49#include <linux/serial_reg.h>
50#include <linux/major.h>
51#include <linux/string.h>
52#include <linux/fcntl.h>
53#include <linux/ptrace.h>
54#include <linux/gfp.h>
55#include <linux/ioport.h>
56#include <linux/mm.h>
57#include <linux/smp_lock.h>
58#include <linux/delay.h>
59#include <linux/pci.h>
60
61#include <asm/system.h>
62#include <asm/io.h>
63#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <asm/bitops.h>
65#include <asm/uaccess.h>
66
67#include "mxser.h"
68
69#define MXSER_VERSION "1.8"
70#define MXSERMAJOR 174
71#define MXSERCUMAJOR 175
72
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070073#define MXSER_EVENT_TXLOW 1
74#define MXSER_EVENT_HANGUP 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76#define MXSER_BOARDS 4 /* Max. boards */
77#define MXSER_PORTS 32 /* Max. ports */
78#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
79#define MXSER_ISR_PASS_LIMIT 256
80
81#define MXSER_ERR_IOADDR -1
82#define MXSER_ERR_IRQ -2
83#define MXSER_ERR_IRQ_CONFLIT -3
84#define MXSER_ERR_VECTOR -4
85
86#define SERIAL_TYPE_NORMAL 1
87#define SERIAL_TYPE_CALLOUT 2
88
89#define WAKEUP_CHARS 256
90
91#define UART_MCR_AFE 0x20
92#define UART_LSR_SPECIAL 0x1E
93
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070094#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
95 IXON|IXOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -070097#define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED : IRQF_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99#define C168_ASIC_ID 1
100#define C104_ASIC_ID 2
101#define C102_ASIC_ID 0xB
102#define CI132_ASIC_ID 4
103#define CI134_ASIC_ID 3
104#define CI104J_ASIC_ID 5
105
106enum {
107 MXSER_BOARD_C168_ISA = 1,
108 MXSER_BOARD_C104_ISA,
109 MXSER_BOARD_CI104J,
110 MXSER_BOARD_C168_PCI,
111 MXSER_BOARD_C104_PCI,
112 MXSER_BOARD_C102_ISA,
113 MXSER_BOARD_CI132,
114 MXSER_BOARD_CI134,
115 MXSER_BOARD_CP132,
116 MXSER_BOARD_CP114,
117 MXSER_BOARD_CT114,
118 MXSER_BOARD_CP102,
119 MXSER_BOARD_CP104U,
120 MXSER_BOARD_CP168U,
121 MXSER_BOARD_CP132U,
122 MXSER_BOARD_CP134U,
123 MXSER_BOARD_CP104JU,
124 MXSER_BOARD_RC7000,
125 MXSER_BOARD_CP118U,
126 MXSER_BOARD_CP102UL,
127 MXSER_BOARD_CP102U,
128};
129
130static char *mxser_brdname[] = {
131 "C168 series",
132 "C104 series",
133 "CI-104J series",
134 "C168H/PCI series",
135 "C104H/PCI series",
136 "C102 series",
137 "CI-132 series",
138 "CI-134 series",
139 "CP-132 series",
140 "CP-114 series",
141 "CT-114 series",
142 "CP-102 series",
143 "CP-104U series",
144 "CP-168U series",
145 "CP-132U series",
146 "CP-134U series",
147 "CP-104JU series",
148 "Moxa UC7000 Serial",
149 "CP-118U series",
150 "CP-102UL series",
151 "CP-102U series",
152};
153
154static int mxser_numports[] = {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700155 8, /* C168-ISA */
156 4, /* C104-ISA */
157 4, /* CI104J */
158 8, /* C168-PCI */
159 4, /* C104-PCI */
160 2, /* C102-ISA */
161 2, /* CI132 */
162 4, /* CI134 */
163 2, /* CP132 */
164 4, /* CP114 */
165 4, /* CT114 */
166 2, /* CP102 */
167 4, /* CP104U */
168 8, /* CP168U */
169 2, /* CP132U */
170 4, /* CP134U */
171 4, /* CP104JU */
172 8, /* RC7000 */
173 8, /* CP118U */
174 2, /* CP102UL */
175 2, /* CP102U */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
178#define UART_TYPE_NUM 2
179
180static const unsigned int Gmoxa_uart_id[UART_TYPE_NUM] = {
181 MOXA_MUST_MU150_HWID,
182 MOXA_MUST_MU860_HWID
183};
184
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700185/* This is only for PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#define UART_INFO_NUM 3
187struct mxpciuart_info {
188 int type;
189 int tx_fifo;
190 int rx_fifo;
191 int xmit_fifo_size;
192 int rx_high_water;
193 int rx_trigger;
194 int rx_low_water;
195 long max_baud;
196};
197
198static const struct mxpciuart_info Gpci_uart_info[UART_INFO_NUM] = {
199 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
200 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
201 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
202};
203
204
205#ifdef CONFIG_PCI
206
207static struct pci_device_id mxser_pcibrds[] = {
208 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_C168_PCI},
209 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_C104_PCI},
210 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP132},
211 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP114},
212 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CT114},
213 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102},
214 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP104U},
215 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP168U},
216 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP132U},
217 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP134U},
218 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP104JU},
219 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_RC7000},
220 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP118U},
221 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102UL},
222 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102U},
223 {0}
224};
225
226MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
227
228
229#endif
230
231typedef struct _moxa_pci_info {
232 unsigned short busNum;
233 unsigned short devNum;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700234 struct pci_dev *pdev; /* add by Victor Yu. 06-23-2003 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235} moxa_pci_info;
236
237static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
238static int ttymajor = MXSERMAJOR;
239static int calloutmajor = MXSERCUMAJOR;
240static int verbose = 0;
241
242/* Variables for insmod */
243
244MODULE_AUTHOR("Casper Yang");
245MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800246module_param_array(ioaddr, int, NULL, 0);
247module_param(ttymajor, int, 0);
248module_param(calloutmajor, int, 0);
249module_param(verbose, bool, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250MODULE_LICENSE("GPL");
251
252struct mxser_log {
253 int tick;
254 unsigned long rxcnt[MXSER_PORTS];
255 unsigned long txcnt[MXSER_PORTS];
256};
257
258
259struct mxser_mon {
260 unsigned long rxcnt;
261 unsigned long txcnt;
262 unsigned long up_rxcnt;
263 unsigned long up_txcnt;
264 int modem_status;
265 unsigned char hold_reason;
266};
267
268struct mxser_mon_ext {
269 unsigned long rx_cnt[32];
270 unsigned long tx_cnt[32];
271 unsigned long up_rxcnt[32];
272 unsigned long up_txcnt[32];
273 int modem_status[32];
274
275 long baudrate[32];
276 int databits[32];
277 int stopbits[32];
278 int parity[32];
279 int flowctrl[32];
280 int fifo[32];
281 int iftype[32];
282};
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284struct mxser_hwconf {
285 int board_type;
286 int ports;
287 int irq;
288 int vector;
289 int vector_mask;
290 int uart_type;
291 int ioaddr[MXSER_PORTS_PER_BOARD];
292 int baud_base[MXSER_PORTS_PER_BOARD];
293 moxa_pci_info pciInfo;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700294 int IsMoxaMustChipFlag; /* add by Victor Yu. 08-30-2002 */
295 int MaxCanSetBaudRate[MXSER_PORTS_PER_BOARD]; /* add by Victor Yu. 09-04-2002 */
296 int opmode_ioaddr[MXSER_PORTS_PER_BOARD]; /* add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297};
298
299struct mxser_struct {
300 int port;
301 int base; /* port base address */
302 int irq; /* port using irq no. */
303 int vector; /* port irq vector */
304 int vectormask; /* port vector mask */
305 int rx_high_water;
306 int rx_trigger; /* Rx fifo trigger level */
307 int rx_low_water;
308 int baud_base; /* max. speed */
309 int flags; /* defined in tty.h */
310 int type; /* UART type */
311 struct tty_struct *tty;
312 int read_status_mask;
313 int ignore_status_mask;
314 int xmit_fifo_size;
315 int custom_divisor;
316 int x_char; /* xon/xoff character */
317 int close_delay;
318 unsigned short closing_wait;
319 int IER; /* Interrupt Enable Register */
320 int MCR; /* Modem control register */
321 unsigned long event;
322 int count; /* # of fd on device */
323 int blocked_open; /* # of blocked opens */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 unsigned char *xmit_buf;
325 int xmit_head;
326 int xmit_tail;
327 int xmit_cnt;
328 struct work_struct tqueue;
Alan Cox606d0992006-12-08 02:38:45 -0800329 struct ktermios normal_termios;
330 struct ktermios callout_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 wait_queue_head_t open_wait;
332 wait_queue_head_t close_wait;
333 wait_queue_head_t delta_msr_wait;
334 struct async_icount icount; /* kernel counters for the 4 input interrupts */
335 int timeout;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700336 int IsMoxaMustChipFlag; /* add by Victor Yu. 08-30-2002 */
337 int MaxCanSetBaudRate; /* add by Victor Yu. 09-04-2002 */
338 int opmode_ioaddr; /* add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 unsigned char stop_rx;
340 unsigned char ldisc_stop_rx;
341 long realbaud;
342 struct mxser_mon mon_data;
343 unsigned char err_shadow;
344 spinlock_t slock;
345};
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347struct mxser_mstatus {
348 tcflag_t cflag;
349 int cts;
350 int dsr;
351 int ri;
352 int dcd;
353};
354
355static struct mxser_mstatus GMStatus[MXSER_PORTS];
356
357static int mxserBoardCAP[MXSER_BOARDS] = {
358 0, 0, 0, 0
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700359 /* 0x180, 0x280, 0x200, 0x320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360};
361
362static struct tty_driver *mxvar_sdriver;
363static struct mxser_struct mxvar_table[MXSER_PORTS];
364static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
Alan Cox606d0992006-12-08 02:38:45 -0800365static struct ktermios *mxvar_termios[MXSER_PORTS + 1];
366static struct ktermios *mxvar_termios_locked[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static struct mxser_log mxvar_log;
368static int mxvar_diagflag;
369static unsigned char mxser_msr[MXSER_PORTS + 1];
370static struct mxser_mon_ext mon_data_ext;
371static int mxser_set_baud_method[MXSER_PORTS + 1];
372static spinlock_t gm_lock;
373
374/*
375 * This is used to figure out the divisor speeds and the timeouts
376 */
377
378static struct mxser_hwconf mxsercfg[MXSER_BOARDS];
379
380/*
381 * static functions:
382 */
383
384static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
385static int mxser_init(void);
386
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700387/* static void mxser_poll(unsigned long); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
389static int mxser_get_PCI_conf(int, int, int, struct mxser_hwconf *);
David Howellsc4028952006-11-22 14:57:56 +0000390static void mxser_do_softint(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391static int mxser_open(struct tty_struct *, struct file *);
392static void mxser_close(struct tty_struct *, struct file *);
393static int mxser_write(struct tty_struct *, const unsigned char *, int);
394static int mxser_write_room(struct tty_struct *);
395static void mxser_flush_buffer(struct tty_struct *);
396static int mxser_chars_in_buffer(struct tty_struct *);
397static void mxser_flush_chars(struct tty_struct *);
398static void mxser_put_char(struct tty_struct *, unsigned char);
399static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
400static int mxser_ioctl_special(unsigned int, void __user *);
401static void mxser_throttle(struct tty_struct *);
402static void mxser_unthrottle(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800403static void mxser_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404static void mxser_stop(struct tty_struct *);
405static void mxser_start(struct tty_struct *);
406static void mxser_hangup(struct tty_struct *);
407static void mxser_rs_break(struct tty_struct *, int);
David Howells7d12e782006-10-05 14:55:46 +0100408static irqreturn_t mxser_interrupt(int, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409static void mxser_receive_chars(struct mxser_struct *, int *);
410static void mxser_transmit_chars(struct mxser_struct *);
411static void mxser_check_modem_status(struct mxser_struct *, int);
412static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
413static int mxser_startup(struct mxser_struct *);
414static void mxser_shutdown(struct mxser_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800415static int mxser_change_speed(struct mxser_struct *, struct ktermios *old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct __user *);
417static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct __user *);
418static int mxser_get_lsr_info(struct mxser_struct *, unsigned int __user *);
419static void mxser_send_break(struct mxser_struct *, int);
420static int mxser_tiocmget(struct tty_struct *, struct file *);
421static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
422static int mxser_set_baud(struct mxser_struct *info, long newspd);
423static void mxser_wait_until_sent(struct tty_struct *tty, int timeout);
424
425static void mxser_startrx(struct tty_struct *tty);
426static void mxser_stoprx(struct tty_struct *tty);
427
428
429static int CheckIsMoxaMust(int io)
430{
431 u8 oldmcr, hwid;
432 int i;
433
434 outb(0, io + UART_LCR);
435 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
436 oldmcr = inb(io + UART_MCR);
437 outb(0, io + UART_MCR);
438 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
439 if ((hwid = inb(io + UART_MCR)) != 0) {
440 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700441 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443
444 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
445 for (i = 0; i < UART_TYPE_NUM; i++) {
446 if (hwid == Gmoxa_uart_id[i])
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700447 return (int)hwid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449 return MOXA_OTHER_UART;
450}
451
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700452/* above is modified by Victor Yu. 08-15-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700454static const struct tty_operations mxser_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 .open = mxser_open,
456 .close = mxser_close,
457 .write = mxser_write,
458 .put_char = mxser_put_char,
459 .flush_chars = mxser_flush_chars,
460 .write_room = mxser_write_room,
461 .chars_in_buffer = mxser_chars_in_buffer,
462 .flush_buffer = mxser_flush_buffer,
463 .ioctl = mxser_ioctl,
464 .throttle = mxser_throttle,
465 .unthrottle = mxser_unthrottle,
466 .set_termios = mxser_set_termios,
467 .stop = mxser_stop,
468 .start = mxser_start,
469 .hangup = mxser_hangup,
Kirill Smelkov57432342005-11-07 00:59:20 -0800470 .break_ctl = mxser_rs_break,
471 .wait_until_sent = mxser_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 .tiocmget = mxser_tiocmget,
473 .tiocmset = mxser_tiocmset,
474};
475
476/*
477 * The MOXA Smartio/Industio serial driver boot-time initialization code!
478 */
479
480static int __init mxser_module_init(void)
481{
482 int ret;
483
484 if (verbose)
485 printk(KERN_DEBUG "Loading module mxser ...\n");
486 ret = mxser_init();
487 if (verbose)
488 printk(KERN_DEBUG "Done.\n");
489 return ret;
490}
491
492static void __exit mxser_module_exit(void)
493{
Kirill Smelkov64698b62005-11-07 00:59:22 -0800494 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 if (verbose)
497 printk(KERN_DEBUG "Unloading module mxser ...\n");
498
Kirill Smelkov64698b62005-11-07 00:59:22 -0800499 err = tty_unregister_driver(mxvar_sdriver);
500 if (!err)
501 put_tty_driver(mxvar_sdriver);
502 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
504
505 for (i = 0; i < MXSER_BOARDS; i++) {
506 struct pci_dev *pdev;
507
508 if (mxsercfg[i].board_type == -1)
509 continue;
510 else {
511 pdev = mxsercfg[i].pciInfo.pdev;
512 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700513 if (pdev != NULL) { /* PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 release_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2));
515 release_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3));
Alan Cox1187ece2006-12-08 02:38:10 -0800516 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 } else {
518 release_region(mxsercfg[i].ioaddr[0], 8 * mxsercfg[i].ports);
519 release_region(mxsercfg[i].vector, 1);
520 }
521 }
522 }
523 if (verbose)
524 printk(KERN_DEBUG "Done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527static void process_txrx_fifo(struct mxser_struct *info)
528{
529 int i;
530
531 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
532 info->rx_trigger = 1;
533 info->rx_high_water = 1;
534 info->rx_low_water = 1;
535 info->xmit_fifo_size = 1;
536 } else {
537 for (i = 0; i < UART_INFO_NUM; i++) {
538 if (info->IsMoxaMustChipFlag == Gpci_uart_info[i].type) {
539 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
540 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
541 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
542 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
543 break;
544 }
545 }
546 }
547}
548
549static int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
550{
551 struct mxser_struct *info;
552 int retval;
553 int i, n;
554
555 n = board * MXSER_PORTS_PER_BOARD;
556 info = &mxvar_table[n];
557 /*if (verbose) */ {
Jiri Slabyae25f8e2006-12-08 02:38:09 -0800558 printk(KERN_DEBUG " ttyMI%d - ttyMI%d ",
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700559 n, n + hwconf->ports - 1);
560 printk(" max. baud rate = %d bps.\n",
561 hwconf->MaxCanSetBaudRate[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
564 for (i = 0; i < hwconf->ports; i++, n++, info++) {
565 info->port = n;
566 info->base = hwconf->ioaddr[i];
567 info->irq = hwconf->irq;
568 info->vector = hwconf->vector;
569 info->vectormask = hwconf->vector_mask;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700570 info->opmode_ioaddr = hwconf->opmode_ioaddr[i]; /* add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 info->stop_rx = 0;
572 info->ldisc_stop_rx = 0;
573
574 info->IsMoxaMustChipFlag = hwconf->IsMoxaMustChipFlag;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700575 /* Enhance mode enabled here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
577 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->base);
578 }
579
580 info->flags = ASYNC_SHARE_IRQ;
581 info->type = hwconf->uart_type;
582 info->baud_base = hwconf->baud_base[i];
583
584 info->MaxCanSetBaudRate = hwconf->MaxCanSetBaudRate[i];
585
586 process_txrx_fifo(info);
587
588
589 info->custom_divisor = hwconf->baud_base[i] * 16;
590 info->close_delay = 5 * HZ / 10;
591 info->closing_wait = 30 * HZ;
David Howellsc4028952006-11-22 14:57:56 +0000592 INIT_WORK(&info->tqueue, mxser_do_softint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 info->normal_termios = mxvar_sdriver->init_termios;
594 init_waitqueue_head(&info->open_wait);
595 init_waitqueue_head(&info->close_wait);
596 init_waitqueue_head(&info->delta_msr_wait);
597 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
598 info->err_shadow = 0;
599 spin_lock_init(&info->slock);
600 }
601 /*
602 * Allocate the IRQ if necessary
603 */
604
605
606 /* before set INT ISR, disable all int */
607 for (i = 0; i < hwconf->ports; i++) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700608 outb(inb(hwconf->ioaddr[i] + UART_IER) & 0xf0,
609 hwconf->ioaddr[i] + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
612 n = board * MXSER_PORTS_PER_BOARD;
613 info = &mxvar_table[n];
614
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700615 retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
616 "mxser", info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (retval) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700618 printk(KERN_ERR "Board %d: %s",
619 board, mxser_brdname[hwconf->board_type - 1]);
620 printk(" Request irq failed, IRQ (%d) may conflict with"
621 " another device.\n", info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return retval;
623 }
624 return 0;
625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
628{
629 mxsercfg[board] = *hwconf;
630}
631
632#ifdef CONFIG_PCI
633static int mxser_get_PCI_conf(int busnum, int devnum, int board_type, struct mxser_hwconf *hwconf)
634{
635 int i, j;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700636 /* unsigned int val; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 unsigned int ioaddress;
638 struct pci_dev *pdev = hwconf->pciInfo.pdev;
639
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700640 /* io address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 hwconf->board_type = board_type;
642 hwconf->ports = mxser_numports[board_type - 1];
643 ioaddress = pci_resource_start(pdev, 2);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700644 request_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2),
645 "mxser(IO)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700647 for (i = 0; i < hwconf->ports; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 hwconf->ioaddr[i] = ioaddress + 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700650 /* vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 ioaddress = pci_resource_start(pdev, 3);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700652 request_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3),
653 "mxser(vector)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 hwconf->vector = ioaddress;
655
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700656 /* irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 hwconf->irq = hwconf->pciInfo.pdev->irq;
658
659 hwconf->IsMoxaMustChipFlag = CheckIsMoxaMust(hwconf->ioaddr[0]);
660 hwconf->uart_type = PORT_16550A;
661 hwconf->vector_mask = 0;
662
663
664 for (i = 0; i < hwconf->ports; i++) {
665 for (j = 0; j < UART_INFO_NUM; j++) {
666 if (Gpci_uart_info[j].type == hwconf->IsMoxaMustChipFlag) {
667 hwconf->MaxCanSetBaudRate[i] = Gpci_uart_info[j].max_baud;
668
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700669 /* exception....CP-102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (board_type == MXSER_BOARD_CP102)
671 hwconf->MaxCanSetBaudRate[i] = 921600;
672 break;
673 }
674 }
675 }
676
677 if (hwconf->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID) {
678 for (i = 0; i < hwconf->ports; i++) {
679 if (i < 4)
680 hwconf->opmode_ioaddr[i] = ioaddress + 4;
681 else
682 hwconf->opmode_ioaddr[i] = ioaddress + 0x0c;
683 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700684 outb(0, ioaddress + 4); /* default set to RS232 mode */
685 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
688 for (i = 0; i < hwconf->ports; i++) {
689 hwconf->vector_mask |= (1 << i);
690 hwconf->baud_base[i] = 921600;
691 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694#endif
695
696static int mxser_init(void)
697{
698 int i, m, retval, b, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 struct pci_dev *pdev = NULL;
700 int index;
701 unsigned char busnum, devnum;
702 struct mxser_hwconf hwconf;
703
704 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
705 if (!mxvar_sdriver)
706 return -ENOMEM;
707 spin_lock_init(&gm_lock);
708
709 for (i = 0; i < MXSER_BOARDS; i++) {
710 mxsercfg[i].board_type = -1;
711 }
712
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700713 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
714 MXSER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 /* Initialize the tty_driver structure */
717 memset(mxvar_sdriver, 0, sizeof(struct tty_driver));
Jiri Slaby31f87cf2006-12-29 16:47:34 -0800718 mxvar_sdriver->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
Jiri Slabyae25f8e2006-12-08 02:38:09 -0800720 mxvar_sdriver->name = "ttyMI";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 mxvar_sdriver->major = ttymajor;
722 mxvar_sdriver->minor_start = 0;
723 mxvar_sdriver->num = MXSER_PORTS + 1;
724 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
725 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
726 mxvar_sdriver->init_termios = tty_std_termios;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700727 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -0800728 mxvar_sdriver->init_termios.c_ispeed = 9600;
729 mxvar_sdriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
731 tty_set_operations(mxvar_sdriver, &mxser_ops);
732 mxvar_sdriver->ttys = mxvar_tty;
733 mxvar_sdriver->termios = mxvar_termios;
734 mxvar_sdriver->termios_locked = mxvar_termios_locked;
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 mxvar_diagflag = 0;
737 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
738 memset(&mxvar_log, 0, sizeof(struct mxser_log));
739
740 memset(&mxser_msr, 0, sizeof(unsigned char) * (MXSER_PORTS + 1));
741 memset(&mon_data_ext, 0, sizeof(struct mxser_mon_ext));
742 memset(&mxser_set_baud_method, 0, sizeof(int) * (MXSER_PORTS + 1));
743 memset(&hwconf, 0, sizeof(struct mxser_hwconf));
744
745 m = 0;
746 /* Start finding ISA boards here */
747 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
748 int cap;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (!(cap = mxserBoardCAP[b]))
751 continue;
752
753 retval = mxser_get_ISA_conf(cap, &hwconf);
754
755 if (retval != 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700756 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
757 mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if (retval <= 0) {
760 if (retval == MXSER_ERR_IRQ)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700761 printk(KERN_ERR "Invalid interrupt number, "
762 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 else if (retval == MXSER_ERR_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700764 printk(KERN_ERR "Invalid interrupt number, "
765 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700767 printk(KERN_ERR "Invalid interrupt vector, "
768 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700770 printk(KERN_ERR "Invalid I/O address, "
771 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 continue;
774 }
775
776 hwconf.pciInfo.busNum = 0;
777 hwconf.pciInfo.devNum = 0;
778 hwconf.pciInfo.pdev = NULL;
779
780 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700781 /*
782 * init mxsercfg first,
783 * or mxsercfg data is not correct on ISR.
784 */
785 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (mxser_initbrd(m, &hwconf) < 0)
787 continue;
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 m++;
790 }
791
792 /* Start finding ISA boards from module arg */
793 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
794 int cap;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (!(cap = ioaddr[b]))
797 continue;
798
799 retval = mxser_get_ISA_conf(cap, &hwconf);
800
801 if (retval != 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700802 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
803 mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 if (retval <= 0) {
806 if (retval == MXSER_ERR_IRQ)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700807 printk(KERN_ERR "Invalid interrupt number, "
808 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 else if (retval == MXSER_ERR_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700810 printk(KERN_ERR "Invalid interrupt number, "
811 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700813 printk(KERN_ERR "Invalid interrupt vector, "
814 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700816 printk(KERN_ERR "Invalid I/O address, "
817 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 continue;
820 }
821
822 hwconf.pciInfo.busNum = 0;
823 hwconf.pciInfo.devNum = 0;
824 hwconf.pciInfo.pdev = NULL;
825
826 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700827 /*
828 * init mxsercfg first,
829 * or mxsercfg data is not correct on ISR.
830 */
831 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (mxser_initbrd(m, &hwconf) < 0)
833 continue;
834
835 m++;
836 }
837
838 /* start finding PCI board here */
839#ifdef CONFIG_PCI
Tobias Klauserfe971072006-01-09 20:54:02 -0800840 n = ARRAY_SIZE(mxser_pcibrds) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 index = 0;
842 b = 0;
843 while (b < n) {
Alan Cox1187ece2006-12-08 02:38:10 -0800844 pdev = pci_get_device(mxser_pcibrds[b].vendor,
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700845 mxser_pcibrds[b].device, pdev);
Alan Cox1187ece2006-12-08 02:38:10 -0800846 if (pdev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 b++;
848 continue;
849 }
850 hwconf.pciInfo.busNum = busnum = pdev->bus->number;
851 hwconf.pciInfo.devNum = devnum = PCI_SLOT(pdev->devfn) << 3;
852 hwconf.pciInfo.pdev = pdev;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700853 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
854 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
855 busnum, devnum >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 index++;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700857 if (m >= MXSER_BOARDS)
858 printk(KERN_ERR
859 "Too many Smartio/Industio family boards find "
860 "(maximum %d), board not configured\n",
861 MXSER_BOARDS);
862 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (pci_enable_device(pdev)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700864 printk(KERN_ERR "Moxa SmartI/O PCI enable "
865 "fail !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 continue;
867 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700868 retval = mxser_get_PCI_conf(busnum, devnum,
869 (int)mxser_pcibrds[b].driver_data,
870 &hwconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (retval < 0) {
872 if (retval == MXSER_ERR_IRQ)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700873 printk(KERN_ERR
874 "Invalid interrupt number, "
875 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 else if (retval == MXSER_ERR_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700877 printk(KERN_ERR
878 "Invalid interrupt number, "
879 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700881 printk(KERN_ERR
882 "Invalid interrupt vector, "
883 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700885 printk(KERN_ERR
886 "Invalid I/O address, "
887 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 continue;
889 }
890 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700891 /* init mxsercfg first,
892 * or mxsercfg data is not correct on ISR.
893 */
894 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (mxser_initbrd(m, &hwconf) < 0)
896 continue;
897 m++;
Alan Cox1187ece2006-12-08 02:38:10 -0800898 /* Keep an extra reference if we succeeded. It will
899 be returned at unload time */
900 pci_dev_get(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902 }
903#endif
904
Kirill Smelkov64698b62005-11-07 00:59:22 -0800905 retval = tty_register_driver(mxvar_sdriver);
906 if (retval) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700907 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
908 " driver !\n");
Kirill Smelkov64698b62005-11-07 00:59:22 -0800909 put_tty_driver(mxvar_sdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 for (i = 0; i < MXSER_BOARDS; i++) {
912 if (mxsercfg[i].board_type == -1)
913 continue;
914 else {
915 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700916 /* todo: release io, vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918 }
Kirill Smelkov64698b62005-11-07 00:59:22 -0800919 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
Kirill Smelkov64698b62005-11-07 00:59:22 -0800922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
David Howellsc4028952006-11-22 14:57:56 +0000925static void mxser_do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
David Howellsc4028952006-11-22 14:57:56 +0000927 struct mxser_struct *info =
928 container_of(work, struct mxser_struct, tqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 struct tty_struct *tty;
930
931 tty = info->tty;
932
933 if (tty) {
934 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
935 tty_wakeup(tty);
936 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
937 tty_hangup(tty);
938 }
939}
940
941static unsigned char mxser_get_msr(int baseaddr, int mode, int port, struct mxser_struct *info)
942{
943 unsigned char status = 0;
944
945 status = inb(baseaddr + UART_MSR);
946
947 mxser_msr[port] &= 0x0F;
948 mxser_msr[port] |= status;
949 status = mxser_msr[port];
950 if (mode)
951 mxser_msr[port] = 0;
952
953 return status;
954}
955
956/*
957 * This routine is called whenever a serial port is opened. It
958 * enables interrupts for a serial port, linking in its async structure into
959 * the IRQ chain. It also performs the serial-specific
960 * initialization for the tty structure.
961 */
962static int mxser_open(struct tty_struct *tty, struct file *filp)
963{
964 struct mxser_struct *info;
965 int retval, line;
966
Kirill Smelkov6f08b722005-11-07 00:59:23 -0800967 /* initialize driver_data in case something fails */
968 tty->driver_data = NULL;
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 line = tty->index;
971 if (line == MXSER_PORTS)
972 return 0;
973 if (line < 0 || line > MXSER_PORTS)
974 return -ENODEV;
975 info = mxvar_table + line;
976 if (!info->base)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700977 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 tty->driver_data = info;
980 info->tty = tty;
981 /*
982 * Start up serial port
983 */
984 retval = mxser_startup(info);
985 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700986 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 retval = mxser_block_til_ready(tty, filp, info);
989 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700990 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 info->count++;
993
994 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
995 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
996 *tty->termios = info->normal_termios;
997 else
998 *tty->termios = info->callout_termios;
999 mxser_change_speed(info, NULL);
1000 }
1001
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001002 /*
1003 status = mxser_get_msr(info->base, 0, info->port);
1004 mxser_check_modem_status(info, status);
1005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Cedric Le Goater8cddd702007-02-10 01:46:33 -08001007 /* unmark here for very high baud rate (ex. 921600 bps) used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 tty->low_latency = 1;
1009 return 0;
1010}
1011
1012/*
1013 * This routine is called when the serial port gets closed. First, we
1014 * wait for the last remaining data to be sent. Then, we unlink its
1015 * async structure from the interrupt chain if necessary, and we free
1016 * that IRQ if nothing is left in the chain.
1017 */
1018static void mxser_close(struct tty_struct *tty, struct file *filp)
1019{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001020 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 unsigned long timeout;
1023 unsigned long flags;
1024 struct tty_ldisc *ld;
1025
1026 if (tty->index == MXSER_PORTS)
1027 return;
1028 if (!info)
Kirill Smelkov6f08b722005-11-07 00:59:23 -08001029 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 spin_lock_irqsave(&info->slock, flags);
1032
1033 if (tty_hung_up_p(filp)) {
1034 spin_unlock_irqrestore(&info->slock, flags);
1035 return;
1036 }
1037 if ((tty->count == 1) && (info->count != 1)) {
1038 /*
1039 * Uh, oh. tty->count is 1, which means that the tty
1040 * structure will be freed. Info->count should always
1041 * be one in these conditions. If it's greater than
1042 * one, we've got real problems, since it means the
1043 * serial port won't be shutdown.
1044 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001045 printk(KERN_ERR "mxser_close: bad serial port count; "
1046 "tty->count is 1, info->count is %d\n", info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 info->count = 1;
1048 }
1049 if (--info->count < 0) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001050 printk(KERN_ERR "mxser_close: bad serial port count for "
1051 "ttys%d: %d\n", info->port, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 info->count = 0;
1053 }
1054 if (info->count) {
1055 spin_unlock_irqrestore(&info->slock, flags);
1056 return;
1057 }
1058 info->flags |= ASYNC_CLOSING;
1059 spin_unlock_irqrestore(&info->slock, flags);
1060 /*
1061 * Save the termios structure, since this port may have
1062 * separate termios for callout and dialin.
1063 */
1064 if (info->flags & ASYNC_NORMAL_ACTIVE)
1065 info->normal_termios = *tty->termios;
1066 /*
1067 * Now we wait for the transmit buffer to clear; and we notify
1068 * the line discipline to only process XON/XOFF characters.
1069 */
1070 tty->closing = 1;
1071 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1072 tty_wait_until_sent(tty, info->closing_wait);
1073 /*
1074 * At this point we stop accepting input. To do this, we
1075 * disable the receive line status interrupts, and tell the
1076 * interrupt driver to stop checking the data ready bit in the
1077 * line status register.
1078 */
1079 info->IER &= ~UART_IER_RLSI;
1080 if (info->IsMoxaMustChipFlag)
1081 info->IER &= ~MOXA_MUST_RECV_ISR;
1082/* by William
1083 info->read_status_mask &= ~UART_LSR_DR;
1084*/
1085 if (info->flags & ASYNC_INITIALIZED) {
1086 outb(info->IER, info->base + UART_IER);
1087 /*
1088 * Before we drop DTR, make sure the UART transmitter
1089 * has completely drained; this is especially
1090 * important if there is a transmit FIFO!
1091 */
1092 timeout = jiffies + HZ;
1093 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001094 schedule_timeout_interruptible(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (time_after(jiffies, timeout))
1096 break;
1097 }
1098 }
1099 mxser_shutdown(info);
1100
1101 if (tty->driver->flush_buffer)
1102 tty->driver->flush_buffer(tty);
1103
1104 ld = tty_ldisc_ref(tty);
1105 if (ld) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001106 if (ld->flush_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 ld->flush_buffer(tty);
1108 tty_ldisc_deref(ld);
1109 }
1110
1111 tty->closing = 0;
1112 info->event = 0;
1113 info->tty = NULL;
1114 if (info->blocked_open) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001115 if (info->close_delay)
1116 schedule_timeout_interruptible(info->close_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 wake_up_interruptible(&info->open_wait);
1118 }
1119
1120 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1121 wake_up_interruptible(&info->close_wait);
1122
1123}
1124
1125static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1126{
1127 int c, total = 0;
Jesper Juhl56e139f2006-06-25 05:47:52 -07001128 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 unsigned long flags;
1130
Jesper Juhl8a7f7c92006-06-25 05:47:53 -07001131 if (!info->xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001135 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1136 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (c <= 0)
1138 break;
1139
1140 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1141 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001142 info->xmit_head = (info->xmit_head + c) &
1143 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 info->xmit_cnt += c;
1145 spin_unlock_irqrestore(&info->slock, flags);
1146
1147 buf += c;
1148 count -= c;
1149 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
1152 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001153 if (!tty->hw_stopped ||
1154 (info->type == PORT_16550A) ||
1155 (info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 spin_lock_irqsave(&info->slock, flags);
1157 info->IER |= UART_IER_THRI;
1158 outb(info->IER, info->base + UART_IER);
1159 spin_unlock_irqrestore(&info->slock, flags);
1160 }
1161 }
1162 return total;
1163}
1164
1165static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1166{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001167 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 unsigned long flags;
1169
Jesper Juhl8a7f7c92006-06-25 05:47:53 -07001170 if (!info->xmit_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return;
1172
1173 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1174 return;
1175
1176 spin_lock_irqsave(&info->slock, flags);
1177 info->xmit_buf[info->xmit_head++] = ch;
1178 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1179 info->xmit_cnt++;
1180 spin_unlock_irqrestore(&info->slock, flags);
1181 if (!tty->stopped && !(info->IER & UART_IER_THRI)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001182 if (!tty->hw_stopped ||
1183 (info->type == PORT_16550A) ||
1184 info->IsMoxaMustChipFlag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 spin_lock_irqsave(&info->slock, flags);
1186 info->IER |= UART_IER_THRI;
1187 outb(info->IER, info->base + UART_IER);
1188 spin_unlock_irqrestore(&info->slock, flags);
1189 }
1190 }
1191}
1192
1193
1194static void mxser_flush_chars(struct tty_struct *tty)
1195{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001196 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 unsigned long flags;
1198
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001199 if (info->xmit_cnt <= 0 ||
1200 tty->stopped ||
1201 !info->xmit_buf ||
1202 (tty->hw_stopped &&
1203 (info->type != PORT_16550A) &&
1204 (!info->IsMoxaMustChipFlag)
1205 ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return;
1207
1208 spin_lock_irqsave(&info->slock, flags);
1209
1210 info->IER |= UART_IER_THRI;
1211 outb(info->IER, info->base + UART_IER);
1212
1213 spin_unlock_irqrestore(&info->slock, flags);
1214}
1215
1216static int mxser_write_room(struct tty_struct *tty)
1217{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001218 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 int ret;
1220
1221 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1222 if (ret < 0)
1223 ret = 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001224 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227static int mxser_chars_in_buffer(struct tty_struct *tty)
1228{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001229 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return info->xmit_cnt;
1231}
1232
1233static void mxser_flush_buffer(struct tty_struct *tty)
1234{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001235 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 char fcr;
1237 unsigned long flags;
1238
1239
1240 spin_lock_irqsave(&info->slock, flags);
1241 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1242
1243 /* below added by shinhay */
1244 fcr = inb(info->base + UART_FCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001245 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1246 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 outb(fcr, info->base + UART_FCR);
1248
1249 spin_unlock_irqrestore(&info->slock, flags);
1250 /* above added by shinhay */
1251
Jiri Slabyb963a842007-02-10 01:44:55 -08001252 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
1255static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1256{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001257 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 int retval;
1259 struct async_icount cprev, cnow; /* kernel counter temps */
1260 struct serial_icounter_struct __user *p_cuser;
1261 unsigned long templ;
1262 unsigned long flags;
1263 void __user *argp = (void __user *)arg;
1264
1265 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001266 return mxser_ioctl_special(cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001268 /* following add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1270 int opmode, p;
1271 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1272 int shiftbit;
1273 unsigned char val, mask;
1274
1275 p = info->port % 4;
1276 if (cmd == MOXA_SET_OP_MODE) {
1277 if (get_user(opmode, (int __user *) argp))
1278 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001279 if (opmode != RS232_MODE &&
1280 opmode != RS485_2WIRE_MODE &&
1281 opmode != RS422_MODE &&
1282 opmode != RS485_4WIRE_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return -EFAULT;
1284 mask = ModeMask[p];
1285 shiftbit = p * 2;
1286 val = inb(info->opmode_ioaddr);
1287 val &= mask;
1288 val |= (opmode << shiftbit);
1289 outb(val, info->opmode_ioaddr);
1290 } else {
1291 shiftbit = p * 2;
1292 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1293 opmode &= OP_MODE_MASK;
1294 if (copy_to_user(argp, &opmode, sizeof(int)))
1295 return -EFAULT;
1296 }
1297 return 0;
1298 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001299 /* above add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1302 if (tty->flags & (1 << TTY_IO_ERROR))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001303 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305 switch (cmd) {
1306 case TCSBRK: /* SVID version: non-zero arg --> no break */
1307 retval = tty_check_change(tty);
1308 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001309 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 tty_wait_until_sent(tty, 0);
1311 if (!arg)
1312 mxser_send_break(info, HZ / 4); /* 1/4 second */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 case TCSBRKP: /* support for POSIX tcsendbreak() */
1315 retval = tty_check_change(tty);
1316 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001317 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 tty_wait_until_sent(tty, 0);
1319 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 case TIOCGSOFTCAR:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001322 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 case TIOCSSOFTCAR:
1324 if (get_user(templ, (unsigned long __user *) argp))
1325 return -EFAULT;
1326 arg = templ;
1327 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 case TIOCGSERIAL:
1330 return mxser_get_serial_info(info, argp);
1331 case TIOCSSERIAL:
1332 return mxser_set_serial_info(info, argp);
1333 case TIOCSERGETLSR: /* Get line status register */
1334 return mxser_get_lsr_info(info, argp);
1335 /*
1336 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1337 * - mask passed in arg for lines of interest
1338 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1339 * Caller should use TIOCGICOUNT to see which one it was
1340 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001341 case TIOCMIWAIT: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 DECLARE_WAITQUEUE(wait, current);
1343 int ret;
1344 spin_lock_irqsave(&info->slock, flags);
1345 cprev = info->icount; /* note the counters on entry */
1346 spin_unlock_irqrestore(&info->slock, flags);
1347
1348 add_wait_queue(&info->delta_msr_wait, &wait);
1349 while (1) {
1350 spin_lock_irqsave(&info->slock, flags);
1351 cnow = info->icount; /* atomic copy */
1352 spin_unlock_irqrestore(&info->slock, flags);
1353
1354 set_current_state(TASK_INTERRUPTIBLE);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001355 if (((arg & TIOCM_RNG) &&
1356 (cnow.rng != cprev.rng)) ||
1357 ((arg & TIOCM_DSR) &&
1358 (cnow.dsr != cprev.dsr)) ||
1359 ((arg & TIOCM_CD) &&
1360 (cnow.dcd != cprev.dcd)) ||
1361 ((arg & TIOCM_CTS) &&
1362 (cnow.cts != cprev.cts))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 ret = 0;
1364 break;
1365 }
1366 /* see if a signal did it */
1367 if (signal_pending(current)) {
1368 ret = -ERESTARTSYS;
1369 break;
1370 }
1371 cprev = cnow;
1372 }
1373 current->state = TASK_RUNNING;
1374 remove_wait_queue(&info->delta_msr_wait, &wait);
1375 break;
1376 }
1377 /* NOTREACHED */
1378 /*
1379 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1380 * Return: write counters to the user passed counter struct
1381 * NB: both 1->0 and 0->1 transitions are counted except for
1382 * RI where only 0->1 is counted.
1383 */
1384 case TIOCGICOUNT:
1385 spin_lock_irqsave(&info->slock, flags);
1386 cnow = info->icount;
1387 spin_unlock_irqrestore(&info->slock, flags);
1388 p_cuser = argp;
1389 /* modified by casper 1/11/2000 */
1390 if (put_user(cnow.frame, &p_cuser->frame))
1391 return -EFAULT;
1392 if (put_user(cnow.brk, &p_cuser->brk))
1393 return -EFAULT;
1394 if (put_user(cnow.overrun, &p_cuser->overrun))
1395 return -EFAULT;
1396 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1397 return -EFAULT;
1398 if (put_user(cnow.parity, &p_cuser->parity))
1399 return -EFAULT;
1400 if (put_user(cnow.rx, &p_cuser->rx))
1401 return -EFAULT;
1402 if (put_user(cnow.tx, &p_cuser->tx))
1403 return -EFAULT;
1404 put_user(cnow.cts, &p_cuser->cts);
1405 put_user(cnow.dsr, &p_cuser->dsr);
1406 put_user(cnow.rng, &p_cuser->rng);
1407 put_user(cnow.dcd, &p_cuser->dcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return 0;
1409 case MOXA_HighSpeedOn:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001410 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1411 case MOXA_SDS_RSTICOUNTER: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 info->mon_data.rxcnt = 0;
1413 info->mon_data.txcnt = 0;
1414 return 0;
1415 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001416/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 case MOXA_ASPP_SETBAUD:{
1418 long baud;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001419 if (get_user(baud, (long __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return -EFAULT;
1421 mxser_set_baud(info, baud);
1422 return 0;
1423 }
1424 case MOXA_ASPP_GETBAUD:
1425 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1426 return -EFAULT;
1427
1428 return 0;
1429
1430 case MOXA_ASPP_OQUEUE:{
1431 int len, lsr;
1432
1433 len = mxser_chars_in_buffer(tty);
1434
1435 lsr = inb(info->base + UART_LSR) & UART_LSR_TEMT;
1436
1437 len += (lsr ? 0 : 1);
1438
1439 if (copy_to_user(argp, &len, sizeof(int)))
1440 return -EFAULT;
1441
1442 return 0;
1443 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001444 case MOXA_ASPP_MON: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 int mcr, status;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001446
1447 /* info->mon_data.ser_param = tty->termios->c_cflag; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 status = mxser_get_msr(info->base, 1, info->port, info);
1450 mxser_check_modem_status(info, status);
1451
1452 mcr = inb(info->base + UART_MCR);
1453 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1454 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1455 else
1456 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1457
1458 if (mcr & MOXA_MUST_MCR_TX_XON)
1459 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1460 else
1461 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1462
1463 if (info->tty->hw_stopped)
1464 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1465 else
1466 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1467
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001468 if (copy_to_user(argp, &info->mon_data,
1469 sizeof(struct mxser_mon)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return -EFAULT;
1471
1472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001475 case MOXA_ASPP_LSTATUS: {
1476 if (copy_to_user(argp, &info->err_shadow,
1477 sizeof(unsigned char)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return -EFAULT;
1479
1480 info->err_shadow = 0;
1481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001483 case MOXA_SET_BAUD_METHOD: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 int method;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001485
1486 if (get_user(method, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return -EFAULT;
1488 mxser_set_baud_method[info->port] = method;
1489 if (copy_to_user(argp, &method, sizeof(int)))
1490 return -EFAULT;
1491
1492 return 0;
1493 }
1494 default:
1495 return -ENOIOCTLCMD;
1496 }
1497 return 0;
1498}
1499
1500#ifndef CMSPAR
1501#define CMSPAR 010000000000
1502#endif
1503
1504static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1505{
1506 int i, result, status;
1507
1508 switch (cmd) {
1509 case MOXA_GET_CONF:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001510 if (copy_to_user(argp, mxsercfg,
1511 sizeof(struct mxser_hwconf) * 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return -EFAULT;
1513 return 0;
1514 case MOXA_GET_MAJOR:
1515 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1516 return -EFAULT;
1517 return 0;
1518
1519 case MOXA_GET_CUMAJOR:
1520 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1521 return -EFAULT;
1522 return 0;
1523
1524 case MOXA_CHKPORTENABLE:
1525 result = 0;
1526 for (i = 0; i < MXSER_PORTS; i++) {
1527 if (mxvar_table[i].base)
1528 result |= (1 << i);
1529 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001530 return put_user(result, (unsigned long __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 case MOXA_GETDATACOUNT:
1532 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1533 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 case MOXA_GETMSTATUS:
1536 for (i = 0; i < MXSER_PORTS; i++) {
1537 GMStatus[i].ri = 0;
1538 if (!mxvar_table[i].base) {
1539 GMStatus[i].dcd = 0;
1540 GMStatus[i].dsr = 0;
1541 GMStatus[i].cts = 0;
1542 continue;
1543 }
1544
1545 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1546 GMStatus[i].cflag = mxvar_table[i].normal_termios.c_cflag;
1547 else
1548 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1549
1550 status = inb(mxvar_table[i].base + UART_MSR);
1551 if (status & 0x80 /*UART_MSR_DCD */ )
1552 GMStatus[i].dcd = 1;
1553 else
1554 GMStatus[i].dcd = 0;
1555
1556 if (status & 0x20 /*UART_MSR_DSR */ )
1557 GMStatus[i].dsr = 1;
1558 else
1559 GMStatus[i].dsr = 0;
1560
1561
1562 if (status & 0x10 /*UART_MSR_CTS */ )
1563 GMStatus[i].cts = 1;
1564 else
1565 GMStatus[i].cts = 0;
1566 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001567 if (copy_to_user(argp, GMStatus,
1568 sizeof(struct mxser_mstatus) * MXSER_PORTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return -EFAULT;
1570 return 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001571 case MOXA_ASPP_MON_EXT: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 int status;
1573 int opmode, p;
1574 int shiftbit;
1575 unsigned cflag, iflag;
1576
1577 for (i = 0; i < MXSER_PORTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (!mxvar_table[i].base)
1579 continue;
1580
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001581 status = mxser_get_msr(mxvar_table[i].base, 0,
1582 i, &(mxvar_table[i]));
1583 /*
1584 mxser_check_modem_status(&mxvar_table[i],
1585 status);
1586 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (status & UART_MSR_TERI)
1588 mxvar_table[i].icount.rng++;
1589 if (status & UART_MSR_DDSR)
1590 mxvar_table[i].icount.dsr++;
1591 if (status & UART_MSR_DDCD)
1592 mxvar_table[i].icount.dcd++;
1593 if (status & UART_MSR_DCTS)
1594 mxvar_table[i].icount.cts++;
1595
1596 mxvar_table[i].mon_data.modem_status = status;
1597 mon_data_ext.rx_cnt[i] = mxvar_table[i].mon_data.rxcnt;
1598 mon_data_ext.tx_cnt[i] = mxvar_table[i].mon_data.txcnt;
1599 mon_data_ext.up_rxcnt[i] = mxvar_table[i].mon_data.up_rxcnt;
1600 mon_data_ext.up_txcnt[i] = mxvar_table[i].mon_data.up_txcnt;
1601 mon_data_ext.modem_status[i] = mxvar_table[i].mon_data.modem_status;
1602 mon_data_ext.baudrate[i] = mxvar_table[i].realbaud;
1603
1604 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios) {
1605 cflag = mxvar_table[i].normal_termios.c_cflag;
1606 iflag = mxvar_table[i].normal_termios.c_iflag;
1607 } else {
1608 cflag = mxvar_table[i].tty->termios->c_cflag;
1609 iflag = mxvar_table[i].tty->termios->c_iflag;
1610 }
1611
1612 mon_data_ext.databits[i] = cflag & CSIZE;
1613
1614 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1615
1616 mon_data_ext.parity[i] = cflag & (PARENB | PARODD | CMSPAR);
1617
1618 mon_data_ext.flowctrl[i] = 0x00;
1619
1620 if (cflag & CRTSCTS)
1621 mon_data_ext.flowctrl[i] |= 0x03;
1622
1623 if (iflag & (IXON | IXOFF))
1624 mon_data_ext.flowctrl[i] |= 0x0C;
1625
1626 if (mxvar_table[i].type == PORT_16550A)
1627 mon_data_ext.fifo[i] = 1;
1628 else
1629 mon_data_ext.fifo[i] = 0;
1630
1631 p = i % 4;
1632 shiftbit = p * 2;
1633 opmode = inb(mxvar_table[i].opmode_ioaddr) >> shiftbit;
1634 opmode &= OP_MODE_MASK;
1635
1636 mon_data_ext.iftype[i] = opmode;
1637
1638 }
1639 if (copy_to_user(argp, &mon_data_ext, sizeof(struct mxser_mon_ext)))
1640 return -EFAULT;
1641
1642 return 0;
1643
1644 }
1645 default:
1646 return -ENOIOCTLCMD;
1647 }
1648 return 0;
1649}
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651static void mxser_stoprx(struct tty_struct *tty)
1652{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001653 struct mxser_struct *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001654 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 info->ldisc_stop_rx = 1;
1657 if (I_IXOFF(tty)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001658 /* MX_LOCK(&info->slock); */
1659 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (info->IsMoxaMustChipFlag) {
1661 info->IER &= ~MOXA_MUST_RECV_ISR;
1662 outb(info->IER, info->base + UART_IER);
1663 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001664 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 info->x_char = STOP_CHAR(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001666 /* mask by Victor Yu. 09-02-2002 */
1667 /* outb(info->IER, 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 outb(0, info->base + UART_IER);
1669 info->IER |= UART_IER_THRI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001670 /* force Tx interrupt */
1671 outb(info->IER, info->base + UART_IER);
1672 } /* add by Victor Yu. 09-02-2002 */
1673 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
1675
1676 if (info->tty->termios->c_cflag & CRTSCTS) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001677 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 info->MCR &= ~UART_MCR_RTS;
1679 outb(info->MCR, info->base + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001680 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682}
1683
1684static void mxser_startrx(struct tty_struct *tty)
1685{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001686 struct mxser_struct *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001687 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 info->ldisc_stop_rx = 0;
1690 if (I_IXOFF(tty)) {
1691 if (info->x_char)
1692 info->x_char = 0;
1693 else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001694 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001696 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (info->IsMoxaMustChipFlag) {
1698 info->IER |= MOXA_MUST_RECV_ISR;
1699 outb(info->IER, info->base + UART_IER);
1700 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001701 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 info->x_char = START_CHAR(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001704 /* mask by Victor Yu. 09-02-2002 */
1705 /* outb(info->IER, 0); */
1706 /* add by Victor Yu. 09-02-2002 */
1707 outb(0, info->base + UART_IER);
1708 /* force Tx interrupt */
1709 info->IER |= UART_IER_THRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 outb(info->IER, info->base + UART_IER);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001711 } /* add by Victor Yu. 09-02-2002 */
1712 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714 }
1715
1716 if (info->tty->termios->c_cflag & CRTSCTS) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001717 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 info->MCR |= UART_MCR_RTS;
1719 outb(info->MCR, info->base + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001720 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722}
1723
1724/*
1725 * This routine is called by the upper-layer tty layer to signal that
1726 * incoming characters should be throttled.
1727 */
1728static void mxser_throttle(struct tty_struct *tty)
1729{
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001730 /* struct mxser_struct *info = tty->driver_data; */
1731 /* unsigned long flags; */
1732
1733 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001735 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
1738static void mxser_unthrottle(struct tty_struct *tty)
1739{
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001740 /* struct mxser_struct *info = tty->driver_data; */
1741 /* unsigned long flags; */
1742
1743 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 mxser_startrx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001745 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
Alan Cox606d0992006-12-08 02:38:45 -08001748static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001750 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 unsigned long flags;
1752
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001753 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1754 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 mxser_change_speed(info, old_termios);
1757
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001758 if ((old_termios->c_cflag & CRTSCTS) &&
1759 !(tty->termios->c_cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 tty->hw_stopped = 0;
1761 mxser_start(tty);
1762 }
1763 }
1764
1765/* Handle sw stopped */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001766 if ((old_termios->c_iflag & IXON) &&
1767 !(tty->termios->c_iflag & IXON)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 tty->stopped = 0;
1769
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001770 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (info->IsMoxaMustChipFlag) {
1772 spin_lock_irqsave(&info->slock, flags);
1773 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
1774 spin_unlock_irqrestore(&info->slock, flags);
1775 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001776 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 mxser_start(tty);
1779 }
1780}
1781
1782/*
1783 * mxser_stop() and mxser_start()
1784 *
1785 * This routines are called before setting or resetting tty->stopped.
1786 * They enable or disable transmitter interrupts, as necessary.
1787 */
1788static void mxser_stop(struct tty_struct *tty)
1789{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001790 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 unsigned long flags;
1792
1793 spin_lock_irqsave(&info->slock, flags);
1794 if (info->IER & UART_IER_THRI) {
1795 info->IER &= ~UART_IER_THRI;
1796 outb(info->IER, info->base + UART_IER);
1797 }
1798 spin_unlock_irqrestore(&info->slock, flags);
1799}
1800
1801static void mxser_start(struct tty_struct *tty)
1802{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001803 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 unsigned long flags;
1805
1806 spin_lock_irqsave(&info->slock, flags);
1807 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
1808 info->IER |= UART_IER_THRI;
1809 outb(info->IER, info->base + UART_IER);
1810 }
1811 spin_unlock_irqrestore(&info->slock, flags);
1812}
1813
1814/*
1815 * mxser_wait_until_sent() --- wait until the transmitter is empty
1816 */
1817static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1818{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001819 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 unsigned long orig_jiffies, char_time;
1821 int lsr;
1822
1823 if (info->type == PORT_UNKNOWN)
1824 return;
1825
1826 if (info->xmit_fifo_size == 0)
1827 return; /* Just in case.... */
1828
1829 orig_jiffies = jiffies;
1830 /*
1831 * Set the check interval to be 1/5 of the estimated time to
1832 * send a single character, and make it at least 1. The check
1833 * interval should also be less than the timeout.
1834 *
1835 * Note: we have to use pretty tight timings here to satisfy
1836 * the NIST-PCTS.
1837 */
1838 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1839 char_time = char_time / 5;
1840 if (char_time == 0)
1841 char_time = 1;
1842 if (timeout && timeout < char_time)
1843 char_time = timeout;
1844 /*
1845 * If the transmitter hasn't cleared in twice the approximate
1846 * amount of time to send the entire FIFO, it probably won't
1847 * ever clear. This assumes the UART isn't doing flow
1848 * control, which is currently the case. Hence, if it ever
1849 * takes longer than info->timeout, this is probably due to a
1850 * UART bug of some kind. So, we clamp the timeout parameter at
1851 * 2*info->timeout.
1852 */
1853 if (!timeout || timeout > 2 * info->timeout)
1854 timeout = 2 * info->timeout;
1855#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001856 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1857 timeout, char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 printk("jiff=%lu...", jiffies);
1859#endif
1860 while (!((lsr = inb(info->base + UART_LSR)) & UART_LSR_TEMT)) {
1861#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1862 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1863#endif
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001864 schedule_timeout_interruptible(char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (signal_pending(current))
1866 break;
1867 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1868 break;
1869 }
1870 set_current_state(TASK_RUNNING);
1871
1872#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1873 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1874#endif
1875}
1876
1877
1878/*
1879 * This routine is called by tty_hangup() when a hangup is signaled.
1880 */
1881void mxser_hangup(struct tty_struct *tty)
1882{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001883 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 mxser_flush_buffer(tty);
1886 mxser_shutdown(info);
1887 info->event = 0;
1888 info->count = 0;
1889 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1890 info->tty = NULL;
1891 wake_up_interruptible(&info->open_wait);
1892}
1893
1894
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001895/* added by James 03-12-2004. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896/*
1897 * mxser_rs_break() --- routine which turns the break handling on or off
1898 */
1899static void mxser_rs_break(struct tty_struct *tty, int break_state)
1900{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001901 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 unsigned long flags;
1903
1904 spin_lock_irqsave(&info->slock, flags);
1905 if (break_state == -1)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001906 outb(inb(info->base + UART_LCR) | UART_LCR_SBC,
1907 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001909 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC,
1910 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 spin_unlock_irqrestore(&info->slock, flags);
1912}
1913
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001914/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916
1917/*
1918 * This is the serial driver's generic interrupt routine
1919 */
David Howells7d12e782006-10-05 14:55:46 +01001920static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
1922 int status, iir, i;
1923 struct mxser_struct *info;
1924 struct mxser_struct *port;
1925 int max, irqbits, bits, msr;
1926 int pass_counter = 0;
1927 int handled = IRQ_NONE;
1928
1929 port = NULL;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001930 /* spin_lock(&gm_lock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 for (i = 0; i < MXSER_BOARDS; i++) {
1933 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1934 port = dev_id;
1935 break;
1936 }
1937 }
1938
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001939 if (i == MXSER_BOARDS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 goto irq_stop;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001941 if (port == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 goto irq_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 max = mxser_numports[mxsercfg[i].board_type - 1];
1944 while (1) {
1945 irqbits = inb(port->vector) & port->vectormask;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001946 if (irqbits == port->vectormask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 handled = IRQ_HANDLED;
1950 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001951 if (irqbits == port->vectormask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (bits & irqbits)
1954 continue;
1955 info = port + i;
1956
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001957 /* following add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 iir = inb(info->base + UART_IIR);
1959 if (iir & UART_IIR_NO_INT)
1960 continue;
1961 iir &= MOXA_MUST_IIR_MASK;
1962 if (!info->tty) {
1963 status = inb(info->base + UART_LSR);
1964 outb(0x27, info->base + UART_FCR);
1965 inb(info->base + UART_MSR);
1966 continue;
1967 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001968 /* above add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /*
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001970 if (info->tty->flip.count < TTY_FLIPBUF_SIZE / 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 info->IER |= MOXA_MUST_RECV_ISR;
1972 outb(info->IER, info->base + UART_IER);
1973 }
1974 */
1975
1976
1977 /* mask by Victor Yu. 09-13-2002
1978 if ( !info->tty ||
1979 (inb(info->base + UART_IIR) & UART_IIR_NO_INT) )
1980 continue;
1981 */
1982 /* mask by Victor Yu. 09-02-2002
1983 status = inb(info->base + UART_LSR) & info->read_status_mask;
1984 */
1985
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001986 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 status = inb(info->base + UART_LSR);
1988
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001989 if (status & UART_LSR_PE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 info->err_shadow |= NPPI_NOTIFY_PARITY;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001991 if (status & UART_LSR_FE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 info->err_shadow |= NPPI_NOTIFY_FRAMING;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001993 if (status & UART_LSR_OE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 info->err_shadow |= NPPI_NOTIFY_HW_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (status & UART_LSR_BI)
1996 info->err_shadow |= NPPI_NOTIFY_BREAK;
1997
1998 if (info->IsMoxaMustChipFlag) {
1999 /*
2000 if ( (status & 0x02) && !(status & 0x01) ) {
2001 outb(info->base+UART_FCR, 0x23);
2002 continue;
2003 }
2004 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002005 if (iir == MOXA_MUST_IIR_GDA ||
2006 iir == MOXA_MUST_IIR_RDA ||
2007 iir == MOXA_MUST_IIR_RTO ||
2008 iir == MOXA_MUST_IIR_LSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 mxser_receive_chars(info, &status);
2010
2011 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002012 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 status &= info->read_status_mask;
2015 if (status & UART_LSR_DR)
2016 mxser_receive_chars(info, &status);
2017 }
2018 msr = inb(info->base + UART_MSR);
2019 if (msr & UART_MSR_ANY_DELTA) {
2020 mxser_check_modem_status(info, msr);
2021 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002022 /* following add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (info->IsMoxaMustChipFlag) {
2024 if ((iir == 0x02) && (status & UART_LSR_THRE)) {
2025 mxser_transmit_chars(info);
2026 }
2027 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002028 /* above add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 if (status & UART_LSR_THRE) {
2031/* 8-2-99 by William
2032 if ( info->x_char || (info->xmit_cnt > 0) )
2033*/
2034 mxser_transmit_chars(info);
2035 }
2036 }
2037 }
2038 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
2039 break; /* Prevent infinite loops */
2040 }
2041 }
2042
2043 irq_stop:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002044 /* spin_unlock(&gm_lock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return handled;
2046}
2047
2048static void mxser_receive_chars(struct mxser_struct *info, int *status)
2049{
2050 struct tty_struct *tty = info->tty;
2051 unsigned char ch, gdl;
2052 int ignored = 0;
2053 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 int recv_room;
2055 int max = 256;
2056 unsigned long flags;
2057
2058 spin_lock_irqsave(&info->slock, flags);
2059
Alan Cox33f0f882006-01-09 20:54:13 -08002060 recv_room = tty->receive_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if ((recv_room == 0) && (!info->ldisc_stop_rx)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002062 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002064 /* return; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002067 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
2069
2070 if (*status & UART_LSR_SPECIAL) {
2071 goto intr_old;
2072 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002073 /* following add by Victor Yu. 02-11-2004 */
2074 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID &&
2075 (*status & MOXA_MUST_LSR_RERR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 goto intr_old;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002077 /* above add by Victor Yu. 02-14-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (*status & MOXA_MUST_LSR_RERR)
2079 goto intr_old;
2080
2081 gdl = inb(info->base + MOXA_MUST_GDL_REGISTER);
2082
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002083 /* add by Victor Yu. 02-11-2004 */
2084 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU150_HWID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 gdl &= MOXA_MUST_GDL_MASK;
2086 if (gdl >= recv_room) {
2087 if (!info->ldisc_stop_rx) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002088 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 mxser_stoprx(tty);
2090 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002091 /* return; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
2093 while (gdl--) {
2094 ch = inb(info->base + UART_RX);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002095 tty_insert_flip_char(tty, ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 cnt++;
2097 /*
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002098 if ((cnt >= HI_WATER) && (info->stop_rx == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002100 info->stop_rx = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 break;
2102 } */
2103 }
2104 goto end_intr;
2105 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002106 intr_old:
2107 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 do {
2110 if (max-- < 0)
2111 break;
2112 /*
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002113 if ((cnt >= HI_WATER) && (info->stop_rx == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 mxser_stoprx(tty);
2115 info->stop_rx=1;
2116 break;
2117 }
2118 */
2119
2120 ch = inb(info->base + UART_RX);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002121 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (info->IsMoxaMustChipFlag && (*status & UART_LSR_OE) /*&& !(*status&UART_LSR_DR) */ )
2123 outb(0x23, info->base + UART_FCR);
2124 *status &= info->read_status_mask;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002125 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (*status & info->ignore_status_mask) {
2127 if (++ignored > 100)
2128 break;
2129 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002130 char flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (*status & UART_LSR_SPECIAL) {
2132 if (*status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002133 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134/* added by casper 1/11/2000 */
2135 info->icount.brk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136/* */
2137 if (info->flags & ASYNC_SAK)
2138 do_SAK(tty);
2139 } else if (*status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002140 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141/* added by casper 1/11/2000 */
2142 info->icount.parity++;
2143/* */
2144 } else if (*status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002145 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146/* added by casper 1/11/2000 */
2147 info->icount.frame++;
2148/* */
2149 } else if (*status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002150 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151/* added by casper 1/11/2000 */
2152 info->icount.overrun++;
2153/* */
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002154 }
2155 }
2156 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 cnt++;
2158 if (cnt >= recv_room) {
2159 if (!info->ldisc_stop_rx) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002160 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 mxser_stoprx(tty);
2162 }
2163 break;
2164 }
2165
2166 }
2167
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002168 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (info->IsMoxaMustChipFlag)
2170 break;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002171 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 /* mask by Victor Yu. 09-02-2002
2174 *status = inb(info->base + UART_LSR) & info->read_status_mask;
2175 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002176 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 *status = inb(info->base + UART_LSR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002178 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 } while (*status & UART_LSR_DR);
2180
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002181end_intr: /* add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 mxvar_log.rxcnt[info->port] += cnt;
2183 info->mon_data.rxcnt += cnt;
2184 info->mon_data.up_rxcnt += cnt;
2185 spin_unlock_irqrestore(&info->slock, flags);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 tty_flip_buffer_push(tty);
2188}
2189
2190static void mxser_transmit_chars(struct mxser_struct *info)
2191{
2192 int count, cnt;
2193 unsigned long flags;
2194
2195 spin_lock_irqsave(&info->slock, flags);
2196
2197 if (info->x_char) {
2198 outb(info->x_char, info->base + UART_TX);
2199 info->x_char = 0;
2200 mxvar_log.txcnt[info->port]++;
2201 info->mon_data.txcnt++;
2202 info->mon_data.up_txcnt++;
2203
2204/* added by casper 1/11/2000 */
2205 info->icount.tx++;
2206/* */
2207 spin_unlock_irqrestore(&info->slock, flags);
2208 return;
2209 }
2210
2211 if (info->xmit_buf == 0) {
2212 spin_unlock_irqrestore(&info->slock, flags);
2213 return;
2214 }
2215
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002216 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
2217 (info->tty->hw_stopped &&
2218 (info->type != PORT_16550A) &&
2219 (!info->IsMoxaMustChipFlag))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 info->IER &= ~UART_IER_THRI;
2221 outb(info->IER, info->base + UART_IER);
2222 spin_unlock_irqrestore(&info->slock, flags);
2223 return;
2224 }
2225
2226 cnt = info->xmit_cnt;
2227 count = info->xmit_fifo_size;
2228 do {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002229 outb(info->xmit_buf[info->xmit_tail++],
2230 info->base + UART_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2232 if (--info->xmit_cnt <= 0)
2233 break;
2234 } while (--count > 0);
2235 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
2236
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002237/* added by James 03-12-2004. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 info->mon_data.txcnt += (cnt - info->xmit_cnt);
2239 info->mon_data.up_txcnt += (cnt - info->xmit_cnt);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002240/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242/* added by casper 1/11/2000 */
2243 info->icount.tx += (cnt - info->xmit_cnt);
2244/* */
2245
2246 if (info->xmit_cnt < WAKEUP_CHARS) {
2247 set_bit(MXSER_EVENT_TXLOW, &info->event);
2248 schedule_work(&info->tqueue);
2249 }
2250 if (info->xmit_cnt <= 0) {
2251 info->IER &= ~UART_IER_THRI;
2252 outb(info->IER, info->base + UART_IER);
2253 }
2254 spin_unlock_irqrestore(&info->slock, flags);
2255}
2256
2257static void mxser_check_modem_status(struct mxser_struct *info, int status)
2258{
2259 /* update input line counters */
2260 if (status & UART_MSR_TERI)
2261 info->icount.rng++;
2262 if (status & UART_MSR_DDSR)
2263 info->icount.dsr++;
2264 if (status & UART_MSR_DDCD)
2265 info->icount.dcd++;
2266 if (status & UART_MSR_DCTS)
2267 info->icount.cts++;
2268 info->mon_data.modem_status = status;
2269 wake_up_interruptible(&info->delta_msr_wait);
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
2272 if (status & UART_MSR_DCD)
2273 wake_up_interruptible(&info->open_wait);
2274 schedule_work(&info->tqueue);
2275 }
2276
2277 if (info->flags & ASYNC_CTS_FLOW) {
2278 if (info->tty->hw_stopped) {
2279 if (status & UART_MSR_CTS) {
2280 info->tty->hw_stopped = 0;
2281
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002282 if ((info->type != PORT_16550A) &&
2283 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 info->IER |= UART_IER_THRI;
2285 outb(info->IER, info->base + UART_IER);
2286 }
2287 set_bit(MXSER_EVENT_TXLOW, &info->event);
2288 schedule_work(&info->tqueue); }
2289 } else {
2290 if (!(status & UART_MSR_CTS)) {
2291 info->tty->hw_stopped = 1;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002292 if ((info->type != PORT_16550A) &&
2293 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 info->IER &= ~UART_IER_THRI;
2295 outb(info->IER, info->base + UART_IER);
2296 }
2297 }
2298 }
2299 }
2300}
2301
2302static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_struct *info)
2303{
2304 DECLARE_WAITQUEUE(wait, current);
2305 int retval;
2306 int do_clocal = 0;
2307 unsigned long flags;
2308
2309 /*
2310 * If non-blocking mode is set, or the port is not enabled,
2311 * then make the check up front and then exit.
2312 */
2313 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
2314 info->flags |= ASYNC_NORMAL_ACTIVE;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
2317
2318 if (tty->termios->c_cflag & CLOCAL)
2319 do_clocal = 1;
2320
2321 /*
2322 * Block waiting for the carrier detect and the line to become
2323 * free (i.e., not in use by the callout). While we are in
2324 * this loop, info->count is dropped by one, so that
2325 * mxser_close() knows when to free things. We restore it upon
2326 * exit, either normal or abnormal.
2327 */
2328 retval = 0;
2329 add_wait_queue(&info->open_wait, &wait);
2330
2331 spin_lock_irqsave(&info->slock, flags);
2332 if (!tty_hung_up_p(filp))
2333 info->count--;
2334 spin_unlock_irqrestore(&info->slock, flags);
2335 info->blocked_open++;
2336 while (1) {
2337 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002338 outb(inb(info->base + UART_MCR) |
2339 UART_MCR_DTR | UART_MCR_RTS, info->base + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 spin_unlock_irqrestore(&info->slock, flags);
2341 set_current_state(TASK_INTERRUPTIBLE);
2342 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
2343 if (info->flags & ASYNC_HUP_NOTIFY)
2344 retval = -EAGAIN;
2345 else
2346 retval = -ERESTARTSYS;
2347 break;
2348 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002349 if (!(info->flags & ASYNC_CLOSING) &&
2350 (do_clocal ||
2351 (inb(info->base + UART_MSR) & UART_MSR_DCD)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 break;
2353 if (signal_pending(current)) {
2354 retval = -ERESTARTSYS;
2355 break;
2356 }
2357 schedule();
2358 }
2359 set_current_state(TASK_RUNNING);
2360 remove_wait_queue(&info->open_wait, &wait);
2361 if (!tty_hung_up_p(filp))
2362 info->count++;
2363 info->blocked_open--;
2364 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002365 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 info->flags |= ASYNC_NORMAL_ACTIVE;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368}
2369
2370static int mxser_startup(struct mxser_struct *info)
2371{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 unsigned long page;
2373 unsigned long flags;
2374
2375 page = __get_free_page(GFP_KERNEL);
2376 if (!page)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002377 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 spin_lock_irqsave(&info->slock, flags);
2380
2381 if (info->flags & ASYNC_INITIALIZED) {
2382 free_page(page);
2383 spin_unlock_irqrestore(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
2386
2387 if (!info->base || !info->type) {
2388 if (info->tty)
2389 set_bit(TTY_IO_ERROR, &info->tty->flags);
2390 free_page(page);
2391 spin_unlock_irqrestore(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
2394 if (info->xmit_buf)
2395 free_page(page);
2396 else
2397 info->xmit_buf = (unsigned char *) page;
2398
2399 /*
2400 * Clear the FIFO buffers and disable them
2401 * (they will be reenabled in mxser_change_speed())
2402 */
2403 if (info->IsMoxaMustChipFlag)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002404 outb((UART_FCR_CLEAR_RCVR |
2405 UART_FCR_CLEAR_XMIT |
2406 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002408 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2409 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411 /*
2412 * At this point there's no way the LSR could still be 0xFF;
2413 * if it is, then bail out, because there's likely no UART
2414 * here.
2415 */
2416 if (inb(info->base + UART_LSR) == 0xff) {
2417 spin_unlock_irqrestore(&info->slock, flags);
2418 if (capable(CAP_SYS_ADMIN)) {
2419 if (info->tty)
2420 set_bit(TTY_IO_ERROR, &info->tty->flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 } else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002423 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 }
2425
2426 /*
2427 * Clear the interrupt registers.
2428 */
2429 (void) inb(info->base + UART_LSR);
2430 (void) inb(info->base + UART_RX);
2431 (void) inb(info->base + UART_IIR);
2432 (void) inb(info->base + UART_MSR);
2433
2434 /*
2435 * Now, initialize the UART
2436 */
2437 outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
2438 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
2439 outb(info->MCR, info->base + UART_MCR);
2440
2441 /*
2442 * Finally, enable interrupts
2443 */
2444 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002445 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002447 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (info->IsMoxaMustChipFlag)
2449 info->IER |= MOXA_MUST_IER_EGDAI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002450 /* above add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 outb(info->IER, info->base + UART_IER); /* enable interrupts */
2452
2453 /*
2454 * And clear the interrupt registers again for luck.
2455 */
2456 (void) inb(info->base + UART_LSR);
2457 (void) inb(info->base + UART_RX);
2458 (void) inb(info->base + UART_IIR);
2459 (void) inb(info->base + UART_MSR);
2460
2461 if (info->tty)
2462 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2463 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2464
2465 /*
2466 * and set the speed of the serial port
2467 */
2468 spin_unlock_irqrestore(&info->slock, flags);
2469 mxser_change_speed(info, NULL);
2470
2471 info->flags |= ASYNC_INITIALIZED;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
2475/*
2476 * This routine will shutdown a serial port; interrupts maybe disabled, and
2477 * DTR is dropped if the hangup on close termio flag is on.
2478 */
2479static void mxser_shutdown(struct mxser_struct *info)
2480{
2481 unsigned long flags;
2482
2483 if (!(info->flags & ASYNC_INITIALIZED))
2484 return;
2485
2486 spin_lock_irqsave(&info->slock, flags);
2487
2488 /*
2489 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2490 * here so the queue might never be waken up
2491 */
2492 wake_up_interruptible(&info->delta_msr_wait);
2493
2494 /*
2495 * Free the IRQ, if necessary
2496 */
2497 if (info->xmit_buf) {
2498 free_page((unsigned long) info->xmit_buf);
2499 info->xmit_buf = NULL;
2500 }
2501
2502 info->IER = 0;
2503 outb(0x00, info->base + UART_IER);
2504
2505 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
2506 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
2507 outb(info->MCR, info->base + UART_MCR);
2508
2509 /* clear Rx/Tx FIFO's */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002510 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (info->IsMoxaMustChipFlag)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002512 outb((UART_FCR_CLEAR_RCVR |
2513 UART_FCR_CLEAR_XMIT |
2514 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002516 /* above add by Victor Yu. 08-30-2002 */
2517 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2518 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 /* read data port to reset things */
2521 (void) inb(info->base + UART_RX);
2522
2523 if (info->tty)
2524 set_bit(TTY_IO_ERROR, &info->tty->flags);
2525
2526 info->flags &= ~ASYNC_INITIALIZED;
2527
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002528 /* following add by Victor Yu. 09-23-2002 */
2529 if (info->IsMoxaMustChipFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->base);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002531 /* above add by Victor Yu. 09-23-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 spin_unlock_irqrestore(&info->slock, flags);
2534}
2535
2536/*
2537 * This routine is called to set the UART divisor registers to match
2538 * the specified baud rate for a serial port.
2539 */
Alan Cox606d0992006-12-08 02:38:45 -08002540static int mxser_change_speed(struct mxser_struct *info, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
2542 unsigned cflag, cval, fcr;
2543 int ret = 0;
2544 unsigned char status;
2545 long baud;
2546 unsigned long flags;
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (!info->tty || !info->tty->termios)
2549 return ret;
2550 cflag = info->tty->termios->c_cflag;
2551 if (!(info->base))
2552 return ret;
2553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554#ifndef B921600
2555#define B921600 (B460800 +1)
2556#endif
2557 if (mxser_set_baud_method[info->port] == 0) {
Alan Coxc7bce302006-09-30 23:27:24 -07002558 baud = tty_get_baud_rate(info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 mxser_set_baud(info, baud);
2560 }
2561
2562 /* byte size and parity */
2563 switch (cflag & CSIZE) {
2564 case CS5:
2565 cval = 0x00;
2566 break;
2567 case CS6:
2568 cval = 0x01;
2569 break;
2570 case CS7:
2571 cval = 0x02;
2572 break;
2573 case CS8:
2574 cval = 0x03;
2575 break;
2576 default:
2577 cval = 0x00;
2578 break; /* too keep GCC shut... */
2579 }
2580 if (cflag & CSTOPB)
2581 cval |= 0x04;
2582 if (cflag & PARENB)
2583 cval |= UART_LCR_PARITY;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002584 if (!(cflag & PARODD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 cval |= UART_LCR_EPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 if (cflag & CMSPAR)
2587 cval |= UART_LCR_SPAR;
2588
2589 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
2590 if (info->IsMoxaMustChipFlag) {
2591 fcr = UART_FCR_ENABLE_FIFO;
2592 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2593 SET_MOXA_MUST_FIFO_VALUE(info);
2594 } else
2595 fcr = 0;
2596 } else {
2597 fcr = UART_FCR_ENABLE_FIFO;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002598 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 if (info->IsMoxaMustChipFlag) {
2600 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2601 SET_MOXA_MUST_FIFO_VALUE(info);
2602 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002603 /* above add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 switch (info->rx_trigger) {
2605 case 1:
2606 fcr |= UART_FCR_TRIGGER_1;
2607 break;
2608 case 4:
2609 fcr |= UART_FCR_TRIGGER_4;
2610 break;
2611 case 8:
2612 fcr |= UART_FCR_TRIGGER_8;
2613 break;
2614 default:
2615 fcr |= UART_FCR_TRIGGER_14;
2616 break;
2617 }
2618 }
2619 }
2620
2621 /* CTS flow control flag and modem status interrupts */
2622 info->IER &= ~UART_IER_MSI;
2623 info->MCR &= ~UART_MCR_AFE;
2624 if (cflag & CRTSCTS) {
2625 info->flags |= ASYNC_CTS_FLOW;
2626 info->IER |= UART_IER_MSI;
2627 if ((info->type == PORT_16550A) || (info->IsMoxaMustChipFlag)) {
2628 info->MCR |= UART_MCR_AFE;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002629 /* status = mxser_get_msr(info->base, 0, info->port); */
2630/*
2631 save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 cli();
2633 status = inb(baseaddr + UART_MSR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002634 restore_flags(flags);
2635*/
2636 /* mxser_check_modem_status(info, status); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002638 /* status = mxser_get_msr(info->base, 0, info->port); */
2639 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 status = inb(info->base + UART_MSR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002641 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (info->tty->hw_stopped) {
2643 if (status & UART_MSR_CTS) {
2644 info->tty->hw_stopped = 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002645 if ((info->type != PORT_16550A) &&
2646 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 info->IER |= UART_IER_THRI;
2648 outb(info->IER, info->base + UART_IER);
2649 }
2650 set_bit(MXSER_EVENT_TXLOW, &info->event);
2651 schedule_work(&info->tqueue); }
2652 } else {
2653 if (!(status & UART_MSR_CTS)) {
2654 info->tty->hw_stopped = 1;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002655 if ((info->type != PORT_16550A) &&
2656 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 info->IER &= ~UART_IER_THRI;
2658 outb(info->IER, info->base + UART_IER);
2659 }
2660 }
2661 }
2662 }
2663 } else {
2664 info->flags &= ~ASYNC_CTS_FLOW;
2665 }
2666 outb(info->MCR, info->base + UART_MCR);
2667 if (cflag & CLOCAL) {
2668 info->flags &= ~ASYNC_CHECK_CD;
2669 } else {
2670 info->flags |= ASYNC_CHECK_CD;
2671 info->IER |= UART_IER_MSI;
2672 }
2673 outb(info->IER, info->base + UART_IER);
2674
2675 /*
2676 * Set up parity check flag
2677 */
2678 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2679 if (I_INPCK(info->tty))
2680 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2681 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2682 info->read_status_mask |= UART_LSR_BI;
2683
2684 info->ignore_status_mask = 0;
2685
2686 if (I_IGNBRK(info->tty)) {
2687 info->ignore_status_mask |= UART_LSR_BI;
2688 info->read_status_mask |= UART_LSR_BI;
2689 /*
2690 * If we're ignore parity and break indicators, ignore
2691 * overruns too. (For real raw support).
2692 */
2693 if (I_IGNPAR(info->tty)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002694 info->ignore_status_mask |=
2695 UART_LSR_OE |
2696 UART_LSR_PE |
2697 UART_LSR_FE;
2698 info->read_status_mask |=
2699 UART_LSR_OE |
2700 UART_LSR_PE |
2701 UART_LSR_FE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
2703 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002704 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 if (info->IsMoxaMustChipFlag) {
2706 spin_lock_irqsave(&info->slock, flags);
2707 SET_MOXA_MUST_XON1_VALUE(info->base, START_CHAR(info->tty));
2708 SET_MOXA_MUST_XOFF1_VALUE(info->base, STOP_CHAR(info->tty));
2709 if (I_IXON(info->tty)) {
2710 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2711 } else {
2712 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2713 }
2714 if (I_IXOFF(info->tty)) {
2715 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2716 } else {
2717 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2718 }
2719 /*
2720 if ( I_IXANY(info->tty) ) {
2721 info->MCR |= MOXA_MUST_MCR_XON_ANY;
2722 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2723 } else {
2724 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
2725 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2726 }
2727 */
2728 spin_unlock_irqrestore(&info->slock, flags);
2729 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002730 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
2732
2733 outb(fcr, info->base + UART_FCR); /* set fcr */
2734 outb(cval, info->base + UART_LCR);
2735
2736 return ret;
2737}
2738
2739
2740static int mxser_set_baud(struct mxser_struct *info, long newspd)
2741{
2742 int quot = 0;
2743 unsigned char cval;
2744 int ret = 0;
2745 unsigned long flags;
2746
2747 if (!info->tty || !info->tty->termios)
2748 return ret;
2749
2750 if (!(info->base))
2751 return ret;
2752
2753 if (newspd > info->MaxCanSetBaudRate)
2754 return 0;
2755
2756 info->realbaud = newspd;
2757 if (newspd == 134) {
2758 quot = (2 * info->baud_base / 269);
2759 } else if (newspd) {
2760 quot = info->baud_base / newspd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if (quot == 0)
2762 quot = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 } else {
2764 quot = 0;
2765 }
2766
2767 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
2768 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2769
2770 if (quot) {
2771 spin_lock_irqsave(&info->slock, flags);
2772 info->MCR |= UART_MCR_DTR;
2773 outb(info->MCR, info->base + UART_MCR);
2774 spin_unlock_irqrestore(&info->slock, flags);
2775 } else {
2776 spin_lock_irqsave(&info->slock, flags);
2777 info->MCR &= ~UART_MCR_DTR;
2778 outb(info->MCR, info->base + UART_MCR);
2779 spin_unlock_irqrestore(&info->slock, flags);
2780 return ret;
2781 }
2782
2783 cval = inb(info->base + UART_LCR);
2784
2785 outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2786
2787 outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2788 outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2789 outb(cval, info->base + UART_LCR); /* reset DLAB */
2790
2791
2792 return ret;
2793}
2794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795/*
2796 * ------------------------------------------------------------
2797 * friends of mxser_ioctl()
2798 * ------------------------------------------------------------
2799 */
2800static int mxser_get_serial_info(struct mxser_struct *info, struct serial_struct __user *retinfo)
2801{
2802 struct serial_struct tmp;
2803
2804 if (!retinfo)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002805 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 memset(&tmp, 0, sizeof(tmp));
2807 tmp.type = info->type;
2808 tmp.line = info->port;
2809 tmp.port = info->base;
2810 tmp.irq = info->irq;
2811 tmp.flags = info->flags;
2812 tmp.baud_base = info->baud_base;
2813 tmp.close_delay = info->close_delay;
2814 tmp.closing_wait = info->closing_wait;
2815 tmp.custom_divisor = info->custom_divisor;
2816 tmp.hub6 = 0;
2817 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2818 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002819 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820}
2821
2822static int mxser_set_serial_info(struct mxser_struct *info, struct serial_struct __user *new_info)
2823{
2824 struct serial_struct new_serial;
2825 unsigned int flags;
2826 int retval = 0;
2827
2828 if (!new_info || !info->base)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002829 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2831 return -EFAULT;
2832
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002833 if ((new_serial.irq != info->irq) ||
2834 (new_serial.port != info->base) ||
2835 (new_serial.custom_divisor != info->custom_divisor) ||
2836 (new_serial.baud_base != info->baud_base))
2837 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
2839 flags = info->flags & ASYNC_SPD_MASK;
2840
2841 if (!capable(CAP_SYS_ADMIN)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002842 if ((new_serial.baud_base != info->baud_base) ||
2843 (new_serial.close_delay != info->close_delay) ||
2844 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
2845 return -EPERM;
2846 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2847 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 } else {
2849 /*
2850 * OK, past this point, all the error checking has been done.
2851 * At this point, we start making changes.....
2852 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002853 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2854 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 info->close_delay = new_serial.close_delay * HZ / 100;
2856 info->closing_wait = new_serial.closing_wait * HZ / 100;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002857 info->tty->low_latency =
2858 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2859 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 }
2861
2862 /* added by casper, 3/17/2000, for mouse */
2863 info->type = new_serial.type;
2864
2865 process_txrx_fifo(info);
2866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 if (info->flags & ASYNC_INITIALIZED) {
2868 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2869 mxser_change_speed(info, NULL);
2870 }
2871 } else {
2872 retval = mxser_startup(info);
2873 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002874 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
2877/*
2878 * mxser_get_lsr_info - get line status register info
2879 *
2880 * Purpose: Let user call ioctl() to get info when the UART physically
2881 * is emptied. On bus types like RS485, the transmitter must
2882 * release the bus after transmitting. This must be done when
2883 * the transmit shift register is empty, not be done when the
2884 * transmit holding register is empty. This functionality
2885 * allows an RS485 driver to be written in user space.
2886 */
2887static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int __user *value)
2888{
2889 unsigned char status;
2890 unsigned int result;
2891 unsigned long flags;
2892
2893 spin_lock_irqsave(&info->slock, flags);
2894 status = inb(info->base + UART_LSR);
2895 spin_unlock_irqrestore(&info->slock, flags);
2896 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2897 return put_user(result, value);
2898}
2899
2900/*
2901 * This routine sends a break character out the serial port.
2902 */
2903static void mxser_send_break(struct mxser_struct *info, int duration)
2904{
2905 unsigned long flags;
2906
2907 if (!info->base)
2908 return;
2909 set_current_state(TASK_INTERRUPTIBLE);
2910 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002911 outb(inb(info->base + UART_LCR) | UART_LCR_SBC,
2912 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 spin_unlock_irqrestore(&info->slock, flags);
2914 schedule_timeout(duration);
2915 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002916 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC,
2917 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 spin_unlock_irqrestore(&info->slock, flags);
2919}
2920
2921static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2922{
Jesper Juhl56e139f2006-06-25 05:47:52 -07002923 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 unsigned char control, status;
2925 unsigned long flags;
2926
2927
2928 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002929 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (tty->flags & (1 << TTY_IO_ERROR))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002931 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 control = info->MCR;
2934
2935 spin_lock_irqsave(&info->slock, flags);
2936 status = inb(info->base + UART_MSR);
2937 if (status & UART_MSR_ANY_DELTA)
2938 mxser_check_modem_status(info, status);
2939 spin_unlock_irqrestore(&info->slock, flags);
2940 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002941 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2942 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2943 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2944 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2945 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947
2948static int mxser_tiocmset(struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear)
2949{
Jesper Juhl56e139f2006-06-25 05:47:52 -07002950 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 unsigned long flags;
2952
2953
2954 if (tty->index == MXSER_PORTS)
2955 return -ENOIOCTLCMD;
2956 if (tty->flags & (1 << TTY_IO_ERROR))
2957 return -EIO;
2958
2959 spin_lock_irqsave(&info->slock, flags);
2960
2961 if (set & TIOCM_RTS)
2962 info->MCR |= UART_MCR_RTS;
2963 if (set & TIOCM_DTR)
2964 info->MCR |= UART_MCR_DTR;
2965
2966 if (clear & TIOCM_RTS)
2967 info->MCR &= ~UART_MCR_RTS;
2968 if (clear & TIOCM_DTR)
2969 info->MCR &= ~UART_MCR_DTR;
2970
2971 outb(info->MCR, info->base + UART_MCR);
2972 spin_unlock_irqrestore(&info->slock, flags);
2973 return 0;
2974}
2975
2976
2977static int mxser_read_register(int, unsigned short *);
2978static int mxser_program_mode(int);
2979static void mxser_normal_mode(int);
2980
2981static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2982{
2983 int id, i, bits;
2984 unsigned short regs[16], irq;
2985 unsigned char scratch, scratch2;
2986
2987 hwconf->IsMoxaMustChipFlag = MOXA_OTHER_UART;
2988
2989 id = mxser_read_register(cap, regs);
2990 if (id == C168_ASIC_ID) {
2991 hwconf->board_type = MXSER_BOARD_C168_ISA;
2992 hwconf->ports = 8;
2993 } else if (id == C104_ASIC_ID) {
2994 hwconf->board_type = MXSER_BOARD_C104_ISA;
2995 hwconf->ports = 4;
2996 } else if (id == C102_ASIC_ID) {
2997 hwconf->board_type = MXSER_BOARD_C102_ISA;
2998 hwconf->ports = 2;
2999 } else if (id == CI132_ASIC_ID) {
3000 hwconf->board_type = MXSER_BOARD_CI132;
3001 hwconf->ports = 2;
3002 } else if (id == CI134_ASIC_ID) {
3003 hwconf->board_type = MXSER_BOARD_CI134;
3004 hwconf->ports = 4;
3005 } else if (id == CI104J_ASIC_ID) {
3006 hwconf->board_type = MXSER_BOARD_CI104J;
3007 hwconf->ports = 4;
3008 } else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
3011 irq = 0;
3012 if (hwconf->ports == 2) {
3013 irq = regs[9] & 0xF000;
3014 irq = irq | (irq >> 4);
3015 if (irq != (regs[9] & 0xFF00))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003016 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 } else if (hwconf->ports == 4) {
3018 irq = regs[9] & 0xF000;
3019 irq = irq | (irq >> 4);
3020 irq = irq | (irq >> 8);
3021 if (irq != regs[9])
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003022 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 } else if (hwconf->ports == 8) {
3024 irq = regs[9] & 0xF000;
3025 irq = irq | (irq >> 4);
3026 irq = irq | (irq >> 8);
3027 if ((irq != regs[9]) || (irq != regs[10]))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003028 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
3030
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003031 if (!irq)
3032 return MXSER_ERR_IRQ;
3033 hwconf->irq = ((int)(irq & 0xF000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 for (i = 0; i < 8; i++)
3035 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003036 if ((regs[12] & 0x80) == 0)
3037 return MXSER_ERR_VECTOR;
3038 hwconf->vector = (int)regs[11]; /* interrupt vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 if (id == 1)
3040 hwconf->vector_mask = 0x00FF;
3041 else
3042 hwconf->vector_mask = 0x000F;
3043 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
3044 if (regs[12] & bits) {
3045 hwconf->baud_base[i] = 921600;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003046 hwconf->MaxCanSetBaudRate[i] = 921600; /* add by Victor Yu. 09-04-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 } else {
3048 hwconf->baud_base[i] = 115200;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003049 hwconf->MaxCanSetBaudRate[i] = 115200; /* add by Victor Yu. 09-04-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 }
3051 }
3052 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
3053 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
3054 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
3055 outb(scratch2, cap + UART_LCR);
3056 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
3057 scratch = inb(cap + UART_IIR);
3058
3059 if (scratch & 0xC0)
3060 hwconf->uart_type = PORT_16550A;
3061 else
3062 hwconf->uart_type = PORT_16450;
3063 if (id == 1)
3064 hwconf->ports = 8;
3065 else
3066 hwconf->ports = 4;
3067 request_region(hwconf->ioaddr[0], 8 * hwconf->ports, "mxser(IO)");
3068 request_region(hwconf->vector, 1, "mxser(vector)");
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003069 return hwconf->ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070}
3071
3072#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
3073#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
3074#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
3075#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
3076#define EN_CCMD 0x000 /* Chip's command register */
3077#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
3078#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
3079#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
3080#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
3081#define EN0_DCFG 0x00E /* Data configuration reg WR */
3082#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
3083#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
3084#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
3085static int mxser_read_register(int port, unsigned short *regs)
3086{
3087 int i, k, value, id;
3088 unsigned int j;
3089
3090 id = mxser_program_mode(port);
3091 if (id < 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003092 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 for (i = 0; i < 14; i++) {
3094 k = (i & 0x3F) | 0x180;
3095 for (j = 0x100; j > 0; j >>= 1) {
3096 outb(CHIP_CS, port);
3097 if (k & j) {
3098 outb(CHIP_CS | CHIP_DO, port);
3099 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
3100 } else {
3101 outb(CHIP_CS, port);
3102 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
3103 }
3104 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003105 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 value = 0;
3107 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
3108 outb(CHIP_CS, port);
3109 outb(CHIP_CS | CHIP_SK, port);
3110 if (inb(port) & CHIP_DI)
3111 value |= j;
3112 }
3113 regs[i] = value;
3114 outb(0, port);
3115 }
3116 mxser_normal_mode(port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003117 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
3119
3120static int mxser_program_mode(int port)
3121{
3122 int id, i, j, n;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003123 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
3125 spin_lock(&gm_lock);
3126 outb(0, port);
3127 outb(0, port);
3128 outb(0, port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003129 (void)inb(port);
3130 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 outb(0, port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003132 (void)inb(port);
3133 /* restore_flags(flags); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 spin_unlock(&gm_lock);
3135
3136 id = inb(port + 1) & 0x1F;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003137 if ((id != C168_ASIC_ID) &&
3138 (id != C104_ASIC_ID) &&
3139 (id != C102_ASIC_ID) &&
3140 (id != CI132_ASIC_ID) &&
3141 (id != CI134_ASIC_ID) &&
3142 (id != CI104J_ASIC_ID))
3143 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 for (i = 0, j = 0; i < 4; i++) {
3145 n = inb(port + 2);
3146 if (n == 'M') {
3147 j = 1;
3148 } else if ((j == 1) && (n == 1)) {
3149 j = 2;
3150 break;
3151 } else
3152 j = 0;
3153 }
3154 if (j != 2)
3155 id = -2;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003156 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157}
3158
3159static void mxser_normal_mode(int port)
3160{
3161 int i, n;
3162
3163 outb(0xA5, port + 1);
3164 outb(0x80, port + 3);
3165 outb(12, port + 0); /* 9600 bps */
3166 outb(0, port + 1);
3167 outb(0x03, port + 3); /* 8 data bits */
3168 outb(0x13, port + 4); /* loop back mode */
3169 for (i = 0; i < 16; i++) {
3170 n = inb(port + 5);
3171 if ((n & 0x61) == 0x60)
3172 break;
3173 if ((n & 1) == 1)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003174 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 }
3176 outb(0x00, port + 4);
3177}
3178
3179module_init(mxser_module_init);
3180module_exit(mxser_module_exit);