blob: 27a653772049ff6b78050c8f2ff86f2e327b7967 [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 */
324 long session; /* Session of opening process */
325 long pgrp; /* pgrp of opening process */
326 unsigned char *xmit_buf;
327 int xmit_head;
328 int xmit_tail;
329 int xmit_cnt;
330 struct work_struct tqueue;
331 struct termios normal_termios;
332 struct termios callout_termios;
333 wait_queue_head_t open_wait;
334 wait_queue_head_t close_wait;
335 wait_queue_head_t delta_msr_wait;
336 struct async_icount icount; /* kernel counters for the 4 input interrupts */
337 int timeout;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700338 int IsMoxaMustChipFlag; /* add by Victor Yu. 08-30-2002 */
339 int MaxCanSetBaudRate; /* add by Victor Yu. 09-04-2002 */
340 int opmode_ioaddr; /* add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 unsigned char stop_rx;
342 unsigned char ldisc_stop_rx;
343 long realbaud;
344 struct mxser_mon mon_data;
345 unsigned char err_shadow;
346 spinlock_t slock;
347};
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349struct mxser_mstatus {
350 tcflag_t cflag;
351 int cts;
352 int dsr;
353 int ri;
354 int dcd;
355};
356
357static struct mxser_mstatus GMStatus[MXSER_PORTS];
358
359static int mxserBoardCAP[MXSER_BOARDS] = {
360 0, 0, 0, 0
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700361 /* 0x180, 0x280, 0x200, 0x320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362};
363
364static struct tty_driver *mxvar_sdriver;
365static struct mxser_struct mxvar_table[MXSER_PORTS];
366static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
367static struct termios *mxvar_termios[MXSER_PORTS + 1];
368static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
369static struct mxser_log mxvar_log;
370static int mxvar_diagflag;
371static unsigned char mxser_msr[MXSER_PORTS + 1];
372static struct mxser_mon_ext mon_data_ext;
373static int mxser_set_baud_method[MXSER_PORTS + 1];
374static spinlock_t gm_lock;
375
376/*
377 * This is used to figure out the divisor speeds and the timeouts
378 */
379
380static struct mxser_hwconf mxsercfg[MXSER_BOARDS];
381
382/*
383 * static functions:
384 */
385
386static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
387static int mxser_init(void);
388
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700389/* static void mxser_poll(unsigned long); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
391static int mxser_get_PCI_conf(int, int, int, struct mxser_hwconf *);
392static void mxser_do_softint(void *);
393static int mxser_open(struct tty_struct *, struct file *);
394static void mxser_close(struct tty_struct *, struct file *);
395static int mxser_write(struct tty_struct *, const unsigned char *, int);
396static int mxser_write_room(struct tty_struct *);
397static void mxser_flush_buffer(struct tty_struct *);
398static int mxser_chars_in_buffer(struct tty_struct *);
399static void mxser_flush_chars(struct tty_struct *);
400static void mxser_put_char(struct tty_struct *, unsigned char);
401static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
402static int mxser_ioctl_special(unsigned int, void __user *);
403static void mxser_throttle(struct tty_struct *);
404static void mxser_unthrottle(struct tty_struct *);
405static void mxser_set_termios(struct tty_struct *, struct termios *);
406static void mxser_stop(struct tty_struct *);
407static void mxser_start(struct tty_struct *);
408static void mxser_hangup(struct tty_struct *);
409static void mxser_rs_break(struct tty_struct *, int);
410static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
411static void mxser_receive_chars(struct mxser_struct *, int *);
412static void mxser_transmit_chars(struct mxser_struct *);
413static void mxser_check_modem_status(struct mxser_struct *, int);
414static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
415static int mxser_startup(struct mxser_struct *);
416static void mxser_shutdown(struct mxser_struct *);
417static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
418static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct __user *);
419static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct __user *);
420static int mxser_get_lsr_info(struct mxser_struct *, unsigned int __user *);
421static void mxser_send_break(struct mxser_struct *, int);
422static int mxser_tiocmget(struct tty_struct *, struct file *);
423static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
424static int mxser_set_baud(struct mxser_struct *info, long newspd);
425static void mxser_wait_until_sent(struct tty_struct *tty, int timeout);
426
427static void mxser_startrx(struct tty_struct *tty);
428static void mxser_stoprx(struct tty_struct *tty);
429
430
431static int CheckIsMoxaMust(int io)
432{
433 u8 oldmcr, hwid;
434 int i;
435
436 outb(0, io + UART_LCR);
437 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
438 oldmcr = inb(io + UART_MCR);
439 outb(0, io + UART_MCR);
440 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
441 if ((hwid = inb(io + UART_MCR)) != 0) {
442 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700443 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
446 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
447 for (i = 0; i < UART_TYPE_NUM; i++) {
448 if (hwid == Gmoxa_uart_id[i])
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700449 return (int)hwid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451 return MOXA_OTHER_UART;
452}
453
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700454/* above is modified by Victor Yu. 08-15-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456static struct tty_operations mxser_ops = {
457 .open = mxser_open,
458 .close = mxser_close,
459 .write = mxser_write,
460 .put_char = mxser_put_char,
461 .flush_chars = mxser_flush_chars,
462 .write_room = mxser_write_room,
463 .chars_in_buffer = mxser_chars_in_buffer,
464 .flush_buffer = mxser_flush_buffer,
465 .ioctl = mxser_ioctl,
466 .throttle = mxser_throttle,
467 .unthrottle = mxser_unthrottle,
468 .set_termios = mxser_set_termios,
469 .stop = mxser_stop,
470 .start = mxser_start,
471 .hangup = mxser_hangup,
Kirill Smelkov57432342005-11-07 00:59:20 -0800472 .break_ctl = mxser_rs_break,
473 .wait_until_sent = mxser_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 .tiocmget = mxser_tiocmget,
475 .tiocmset = mxser_tiocmset,
476};
477
478/*
479 * The MOXA Smartio/Industio serial driver boot-time initialization code!
480 */
481
482static int __init mxser_module_init(void)
483{
484 int ret;
485
486 if (verbose)
487 printk(KERN_DEBUG "Loading module mxser ...\n");
488 ret = mxser_init();
489 if (verbose)
490 printk(KERN_DEBUG "Done.\n");
491 return ret;
492}
493
494static void __exit mxser_module_exit(void)
495{
Kirill Smelkov64698b62005-11-07 00:59:22 -0800496 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (verbose)
499 printk(KERN_DEBUG "Unloading module mxser ...\n");
500
Kirill Smelkov64698b62005-11-07 00:59:22 -0800501 err = tty_unregister_driver(mxvar_sdriver);
502 if (!err)
503 put_tty_driver(mxvar_sdriver);
504 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
506
507 for (i = 0; i < MXSER_BOARDS; i++) {
508 struct pci_dev *pdev;
509
510 if (mxsercfg[i].board_type == -1)
511 continue;
512 else {
513 pdev = mxsercfg[i].pciInfo.pdev;
514 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700515 if (pdev != NULL) { /* PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 release_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2));
517 release_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3));
518 } else {
519 release_region(mxsercfg[i].ioaddr[0], 8 * mxsercfg[i].ports);
520 release_region(mxsercfg[i].vector, 1);
521 }
522 }
523 }
524 if (verbose)
525 printk(KERN_DEBUG "Done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528static void process_txrx_fifo(struct mxser_struct *info)
529{
530 int i;
531
532 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
533 info->rx_trigger = 1;
534 info->rx_high_water = 1;
535 info->rx_low_water = 1;
536 info->xmit_fifo_size = 1;
537 } else {
538 for (i = 0; i < UART_INFO_NUM; i++) {
539 if (info->IsMoxaMustChipFlag == Gpci_uart_info[i].type) {
540 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
541 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
542 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
543 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
544 break;
545 }
546 }
547 }
548}
549
550static int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
551{
552 struct mxser_struct *info;
553 int retval;
554 int i, n;
555
556 n = board * MXSER_PORTS_PER_BOARD;
557 info = &mxvar_table[n];
558 /*if (verbose) */ {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700559 printk(KERN_DEBUG " ttyM%d - ttyM%d ",
560 n, n + hwconf->ports - 1);
561 printk(" max. baud rate = %d bps.\n",
562 hwconf->MaxCanSetBaudRate[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
565 for (i = 0; i < hwconf->ports; i++, n++, info++) {
566 info->port = n;
567 info->base = hwconf->ioaddr[i];
568 info->irq = hwconf->irq;
569 info->vector = hwconf->vector;
570 info->vectormask = hwconf->vector_mask;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700571 info->opmode_ioaddr = hwconf->opmode_ioaddr[i]; /* add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 info->stop_rx = 0;
573 info->ldisc_stop_rx = 0;
574
575 info->IsMoxaMustChipFlag = hwconf->IsMoxaMustChipFlag;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700576 /* Enhance mode enabled here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
578 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->base);
579 }
580
581 info->flags = ASYNC_SHARE_IRQ;
582 info->type = hwconf->uart_type;
583 info->baud_base = hwconf->baud_base[i];
584
585 info->MaxCanSetBaudRate = hwconf->MaxCanSetBaudRate[i];
586
587 process_txrx_fifo(info);
588
589
590 info->custom_divisor = hwconf->baud_base[i] * 16;
591 info->close_delay = 5 * HZ / 10;
592 info->closing_wait = 30 * HZ;
593 INIT_WORK(&info->tqueue, mxser_do_softint, info);
594 info->normal_termios = mxvar_sdriver->init_termios;
595 init_waitqueue_head(&info->open_wait);
596 init_waitqueue_head(&info->close_wait);
597 init_waitqueue_head(&info->delta_msr_wait);
598 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
599 info->err_shadow = 0;
600 spin_lock_init(&info->slock);
601 }
602 /*
603 * Allocate the IRQ if necessary
604 */
605
606
607 /* before set INT ISR, disable all int */
608 for (i = 0; i < hwconf->ports; i++) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700609 outb(inb(hwconf->ioaddr[i] + UART_IER) & 0xf0,
610 hwconf->ioaddr[i] + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
613 n = board * MXSER_PORTS_PER_BOARD;
614 info = &mxvar_table[n];
615
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700616 retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
617 "mxser", info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (retval) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700619 printk(KERN_ERR "Board %d: %s",
620 board, mxser_brdname[hwconf->board_type - 1]);
621 printk(" Request irq failed, IRQ (%d) may conflict with"
622 " another device.\n", info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return retval;
624 }
625 return 0;
626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
629{
630 mxsercfg[board] = *hwconf;
631}
632
633#ifdef CONFIG_PCI
634static int mxser_get_PCI_conf(int busnum, int devnum, int board_type, struct mxser_hwconf *hwconf)
635{
636 int i, j;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700637 /* unsigned int val; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 unsigned int ioaddress;
639 struct pci_dev *pdev = hwconf->pciInfo.pdev;
640
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700641 /* io address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 hwconf->board_type = board_type;
643 hwconf->ports = mxser_numports[board_type - 1];
644 ioaddress = pci_resource_start(pdev, 2);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700645 request_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2),
646 "mxser(IO)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700648 for (i = 0; i < hwconf->ports; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 hwconf->ioaddr[i] = ioaddress + 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700651 /* vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 ioaddress = pci_resource_start(pdev, 3);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700653 request_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3),
654 "mxser(vector)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 hwconf->vector = ioaddress;
656
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700657 /* irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 hwconf->irq = hwconf->pciInfo.pdev->irq;
659
660 hwconf->IsMoxaMustChipFlag = CheckIsMoxaMust(hwconf->ioaddr[0]);
661 hwconf->uart_type = PORT_16550A;
662 hwconf->vector_mask = 0;
663
664
665 for (i = 0; i < hwconf->ports; i++) {
666 for (j = 0; j < UART_INFO_NUM; j++) {
667 if (Gpci_uart_info[j].type == hwconf->IsMoxaMustChipFlag) {
668 hwconf->MaxCanSetBaudRate[i] = Gpci_uart_info[j].max_baud;
669
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700670 /* exception....CP-102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (board_type == MXSER_BOARD_CP102)
672 hwconf->MaxCanSetBaudRate[i] = 921600;
673 break;
674 }
675 }
676 }
677
678 if (hwconf->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID) {
679 for (i = 0; i < hwconf->ports; i++) {
680 if (i < 4)
681 hwconf->opmode_ioaddr[i] = ioaddress + 4;
682 else
683 hwconf->opmode_ioaddr[i] = ioaddress + 0x0c;
684 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700685 outb(0, ioaddress + 4); /* default set to RS232 mode */
686 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688
689 for (i = 0; i < hwconf->ports; i++) {
690 hwconf->vector_mask |= (1 << i);
691 hwconf->baud_base[i] = 921600;
692 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695#endif
696
697static int mxser_init(void)
698{
699 int i, m, retval, b, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct pci_dev *pdev = NULL;
701 int index;
702 unsigned char busnum, devnum;
703 struct mxser_hwconf hwconf;
704
705 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
706 if (!mxvar_sdriver)
707 return -ENOMEM;
708 spin_lock_init(&gm_lock);
709
710 for (i = 0; i < MXSER_BOARDS; i++) {
711 mxsercfg[i].board_type = -1;
712 }
713
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700714 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
715 MXSER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 /* Initialize the tty_driver structure */
718 memset(mxvar_sdriver, 0, sizeof(struct tty_driver));
719 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
720 mxvar_sdriver->name = "ttyM";
721 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
729 tty_set_operations(mxvar_sdriver, &mxser_ops);
730 mxvar_sdriver->ttys = mxvar_tty;
731 mxvar_sdriver->termios = mxvar_termios;
732 mxvar_sdriver->termios_locked = mxvar_termios_locked;
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 mxvar_diagflag = 0;
735 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
736 memset(&mxvar_log, 0, sizeof(struct mxser_log));
737
738 memset(&mxser_msr, 0, sizeof(unsigned char) * (MXSER_PORTS + 1));
739 memset(&mon_data_ext, 0, sizeof(struct mxser_mon_ext));
740 memset(&mxser_set_baud_method, 0, sizeof(int) * (MXSER_PORTS + 1));
741 memset(&hwconf, 0, sizeof(struct mxser_hwconf));
742
743 m = 0;
744 /* Start finding ISA boards here */
745 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
746 int cap;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (!(cap = mxserBoardCAP[b]))
749 continue;
750
751 retval = mxser_get_ISA_conf(cap, &hwconf);
752
753 if (retval != 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700754 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
755 mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (retval <= 0) {
758 if (retval == MXSER_ERR_IRQ)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700759 printk(KERN_ERR "Invalid interrupt number, "
760 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 else if (retval == MXSER_ERR_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700762 printk(KERN_ERR "Invalid interrupt number, "
763 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700765 printk(KERN_ERR "Invalid interrupt vector, "
766 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700768 printk(KERN_ERR "Invalid I/O address, "
769 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 continue;
772 }
773
774 hwconf.pciInfo.busNum = 0;
775 hwconf.pciInfo.devNum = 0;
776 hwconf.pciInfo.pdev = NULL;
777
778 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700779 /*
780 * init mxsercfg first,
781 * or mxsercfg data is not correct on ISR.
782 */
783 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (mxser_initbrd(m, &hwconf) < 0)
785 continue;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 m++;
788 }
789
790 /* Start finding ISA boards from module arg */
791 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
792 int cap;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (!(cap = ioaddr[b]))
795 continue;
796
797 retval = mxser_get_ISA_conf(cap, &hwconf);
798
799 if (retval != 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700800 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
801 mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 if (retval <= 0) {
804 if (retval == MXSER_ERR_IRQ)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700805 printk(KERN_ERR "Invalid interrupt number, "
806 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 else if (retval == MXSER_ERR_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700808 printk(KERN_ERR "Invalid interrupt number, "
809 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700811 printk(KERN_ERR "Invalid interrupt vector, "
812 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700814 printk(KERN_ERR "Invalid I/O address, "
815 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 continue;
818 }
819
820 hwconf.pciInfo.busNum = 0;
821 hwconf.pciInfo.devNum = 0;
822 hwconf.pciInfo.pdev = NULL;
823
824 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700825 /*
826 * init mxsercfg first,
827 * or mxsercfg data is not correct on ISR.
828 */
829 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (mxser_initbrd(m, &hwconf) < 0)
831 continue;
832
833 m++;
834 }
835
836 /* start finding PCI board here */
837#ifdef CONFIG_PCI
Tobias Klauserfe971072006-01-09 20:54:02 -0800838 n = ARRAY_SIZE(mxser_pcibrds) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 index = 0;
840 b = 0;
841 while (b < n) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700842 pdev = pci_find_device(mxser_pcibrds[b].vendor,
843 mxser_pcibrds[b].device, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (pdev == NULL) {
845 b++;
846 continue;
847 }
848 hwconf.pciInfo.busNum = busnum = pdev->bus->number;
849 hwconf.pciInfo.devNum = devnum = PCI_SLOT(pdev->devfn) << 3;
850 hwconf.pciInfo.pdev = pdev;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700851 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
852 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
853 busnum, devnum >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 index++;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700855 if (m >= MXSER_BOARDS)
856 printk(KERN_ERR
857 "Too many Smartio/Industio family boards find "
858 "(maximum %d), board not configured\n",
859 MXSER_BOARDS);
860 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (pci_enable_device(pdev)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700862 printk(KERN_ERR "Moxa SmartI/O PCI enable "
863 "fail !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 continue;
865 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700866 retval = mxser_get_PCI_conf(busnum, devnum,
867 (int)mxser_pcibrds[b].driver_data,
868 &hwconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (retval < 0) {
870 if (retval == MXSER_ERR_IRQ)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700871 printk(KERN_ERR
872 "Invalid interrupt number, "
873 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 else if (retval == MXSER_ERR_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700875 printk(KERN_ERR
876 "Invalid interrupt number, "
877 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700879 printk(KERN_ERR
880 "Invalid interrupt vector, "
881 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700883 printk(KERN_ERR
884 "Invalid I/O address, "
885 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 continue;
887 }
888 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700889 /* init mxsercfg first,
890 * or mxsercfg data is not correct on ISR.
891 */
892 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (mxser_initbrd(m, &hwconf) < 0)
894 continue;
895 m++;
896 }
897 }
898#endif
899
Kirill Smelkov64698b62005-11-07 00:59:22 -0800900 retval = tty_register_driver(mxvar_sdriver);
901 if (retval) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700902 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
903 " driver !\n");
Kirill Smelkov64698b62005-11-07 00:59:22 -0800904 put_tty_driver(mxvar_sdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 for (i = 0; i < MXSER_BOARDS; i++) {
907 if (mxsercfg[i].board_type == -1)
908 continue;
909 else {
910 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700911 /* todo: release io, vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913 }
Kirill Smelkov64698b62005-11-07 00:59:22 -0800914 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916
Kirill Smelkov64698b62005-11-07 00:59:22 -0800917 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920static void mxser_do_softint(void *private_)
921{
Jesper Juhl56e139f2006-06-25 05:47:52 -0700922 struct mxser_struct *info = private_;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct tty_struct *tty;
924
925 tty = info->tty;
926
927 if (tty) {
928 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
929 tty_wakeup(tty);
930 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
931 tty_hangup(tty);
932 }
933}
934
935static unsigned char mxser_get_msr(int baseaddr, int mode, int port, struct mxser_struct *info)
936{
937 unsigned char status = 0;
938
939 status = inb(baseaddr + UART_MSR);
940
941 mxser_msr[port] &= 0x0F;
942 mxser_msr[port] |= status;
943 status = mxser_msr[port];
944 if (mode)
945 mxser_msr[port] = 0;
946
947 return status;
948}
949
950/*
951 * This routine is called whenever a serial port is opened. It
952 * enables interrupts for a serial port, linking in its async structure into
953 * the IRQ chain. It also performs the serial-specific
954 * initialization for the tty structure.
955 */
956static int mxser_open(struct tty_struct *tty, struct file *filp)
957{
958 struct mxser_struct *info;
959 int retval, line;
960
Kirill Smelkov6f08b722005-11-07 00:59:23 -0800961 /* initialize driver_data in case something fails */
962 tty->driver_data = NULL;
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 line = tty->index;
965 if (line == MXSER_PORTS)
966 return 0;
967 if (line < 0 || line > MXSER_PORTS)
968 return -ENODEV;
969 info = mxvar_table + line;
970 if (!info->base)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700971 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 tty->driver_data = info;
974 info->tty = tty;
975 /*
976 * Start up serial port
977 */
978 retval = mxser_startup(info);
979 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700980 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 retval = mxser_block_til_ready(tty, filp, info);
983 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700984 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 info->count++;
987
988 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
989 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
990 *tty->termios = info->normal_termios;
991 else
992 *tty->termios = info->callout_termios;
993 mxser_change_speed(info, NULL);
994 }
995
996 info->session = current->signal->session;
997 info->pgrp = process_group(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700999 /*
1000 status = mxser_get_msr(info->base, 0, info->port);
1001 mxser_check_modem_status(info, status);
1002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001004/* unmark here for very high baud rate (ex. 921600 bps) used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 tty->low_latency = 1;
1006 return 0;
1007}
1008
1009/*
1010 * This routine is called when the serial port gets closed. First, we
1011 * wait for the last remaining data to be sent. Then, we unlink its
1012 * async structure from the interrupt chain if necessary, and we free
1013 * that IRQ if nothing is left in the chain.
1014 */
1015static void mxser_close(struct tty_struct *tty, struct file *filp)
1016{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001017 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 unsigned long timeout;
1020 unsigned long flags;
1021 struct tty_ldisc *ld;
1022
1023 if (tty->index == MXSER_PORTS)
1024 return;
1025 if (!info)
Kirill Smelkov6f08b722005-11-07 00:59:23 -08001026 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 spin_lock_irqsave(&info->slock, flags);
1029
1030 if (tty_hung_up_p(filp)) {
1031 spin_unlock_irqrestore(&info->slock, flags);
1032 return;
1033 }
1034 if ((tty->count == 1) && (info->count != 1)) {
1035 /*
1036 * Uh, oh. tty->count is 1, which means that the tty
1037 * structure will be freed. Info->count should always
1038 * be one in these conditions. If it's greater than
1039 * one, we've got real problems, since it means the
1040 * serial port won't be shutdown.
1041 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001042 printk(KERN_ERR "mxser_close: bad serial port count; "
1043 "tty->count is 1, info->count is %d\n", info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 info->count = 1;
1045 }
1046 if (--info->count < 0) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001047 printk(KERN_ERR "mxser_close: bad serial port count for "
1048 "ttys%d: %d\n", info->port, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 info->count = 0;
1050 }
1051 if (info->count) {
1052 spin_unlock_irqrestore(&info->slock, flags);
1053 return;
1054 }
1055 info->flags |= ASYNC_CLOSING;
1056 spin_unlock_irqrestore(&info->slock, flags);
1057 /*
1058 * Save the termios structure, since this port may have
1059 * separate termios for callout and dialin.
1060 */
1061 if (info->flags & ASYNC_NORMAL_ACTIVE)
1062 info->normal_termios = *tty->termios;
1063 /*
1064 * Now we wait for the transmit buffer to clear; and we notify
1065 * the line discipline to only process XON/XOFF characters.
1066 */
1067 tty->closing = 1;
1068 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1069 tty_wait_until_sent(tty, info->closing_wait);
1070 /*
1071 * At this point we stop accepting input. To do this, we
1072 * disable the receive line status interrupts, and tell the
1073 * interrupt driver to stop checking the data ready bit in the
1074 * line status register.
1075 */
1076 info->IER &= ~UART_IER_RLSI;
1077 if (info->IsMoxaMustChipFlag)
1078 info->IER &= ~MOXA_MUST_RECV_ISR;
1079/* by William
1080 info->read_status_mask &= ~UART_LSR_DR;
1081*/
1082 if (info->flags & ASYNC_INITIALIZED) {
1083 outb(info->IER, info->base + UART_IER);
1084 /*
1085 * Before we drop DTR, make sure the UART transmitter
1086 * has completely drained; this is especially
1087 * important if there is a transmit FIFO!
1088 */
1089 timeout = jiffies + HZ;
1090 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001091 schedule_timeout_interruptible(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (time_after(jiffies, timeout))
1093 break;
1094 }
1095 }
1096 mxser_shutdown(info);
1097
1098 if (tty->driver->flush_buffer)
1099 tty->driver->flush_buffer(tty);
1100
1101 ld = tty_ldisc_ref(tty);
1102 if (ld) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001103 if (ld->flush_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 ld->flush_buffer(tty);
1105 tty_ldisc_deref(ld);
1106 }
1107
1108 tty->closing = 0;
1109 info->event = 0;
1110 info->tty = NULL;
1111 if (info->blocked_open) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001112 if (info->close_delay)
1113 schedule_timeout_interruptible(info->close_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 wake_up_interruptible(&info->open_wait);
1115 }
1116
1117 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1118 wake_up_interruptible(&info->close_wait);
1119
1120}
1121
1122static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1123{
1124 int c, total = 0;
Jesper Juhl56e139f2006-06-25 05:47:52 -07001125 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 unsigned long flags;
1127
Jesper Juhl8a7f7c92006-06-25 05:47:53 -07001128 if (!info->xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001132 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1133 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (c <= 0)
1135 break;
1136
1137 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1138 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001139 info->xmit_head = (info->xmit_head + c) &
1140 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 info->xmit_cnt += c;
1142 spin_unlock_irqrestore(&info->slock, flags);
1143
1144 buf += c;
1145 count -= c;
1146 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148
1149 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001150 if (!tty->hw_stopped ||
1151 (info->type == PORT_16550A) ||
1152 (info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 spin_lock_irqsave(&info->slock, flags);
1154 info->IER |= UART_IER_THRI;
1155 outb(info->IER, info->base + UART_IER);
1156 spin_unlock_irqrestore(&info->slock, flags);
1157 }
1158 }
1159 return total;
1160}
1161
1162static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1163{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001164 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 unsigned long flags;
1166
Jesper Juhl8a7f7c92006-06-25 05:47:53 -07001167 if (!info->xmit_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return;
1169
1170 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1171 return;
1172
1173 spin_lock_irqsave(&info->slock, flags);
1174 info->xmit_buf[info->xmit_head++] = ch;
1175 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1176 info->xmit_cnt++;
1177 spin_unlock_irqrestore(&info->slock, flags);
1178 if (!tty->stopped && !(info->IER & UART_IER_THRI)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001179 if (!tty->hw_stopped ||
1180 (info->type == PORT_16550A) ||
1181 info->IsMoxaMustChipFlag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 spin_lock_irqsave(&info->slock, flags);
1183 info->IER |= UART_IER_THRI;
1184 outb(info->IER, info->base + UART_IER);
1185 spin_unlock_irqrestore(&info->slock, flags);
1186 }
1187 }
1188}
1189
1190
1191static void mxser_flush_chars(struct tty_struct *tty)
1192{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001193 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 unsigned long flags;
1195
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001196 if (info->xmit_cnt <= 0 ||
1197 tty->stopped ||
1198 !info->xmit_buf ||
1199 (tty->hw_stopped &&
1200 (info->type != PORT_16550A) &&
1201 (!info->IsMoxaMustChipFlag)
1202 ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return;
1204
1205 spin_lock_irqsave(&info->slock, flags);
1206
1207 info->IER |= UART_IER_THRI;
1208 outb(info->IER, info->base + UART_IER);
1209
1210 spin_unlock_irqrestore(&info->slock, flags);
1211}
1212
1213static int mxser_write_room(struct tty_struct *tty)
1214{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001215 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 int ret;
1217
1218 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1219 if (ret < 0)
1220 ret = 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001221 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
1224static int mxser_chars_in_buffer(struct tty_struct *tty)
1225{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001226 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return info->xmit_cnt;
1228}
1229
1230static void mxser_flush_buffer(struct tty_struct *tty)
1231{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001232 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 char fcr;
1234 unsigned long flags;
1235
1236
1237 spin_lock_irqsave(&info->slock, flags);
1238 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1239
1240 /* below added by shinhay */
1241 fcr = inb(info->base + UART_FCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001242 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1243 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 outb(fcr, info->base + UART_FCR);
1245
1246 spin_unlock_irqrestore(&info->slock, flags);
1247 /* above added by shinhay */
1248
1249 wake_up_interruptible(&tty->write_wait);
1250 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1251 (tty->ldisc.write_wakeup) (tty);
1252}
1253
1254static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1255{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001256 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 int retval;
1258 struct async_icount cprev, cnow; /* kernel counter temps */
1259 struct serial_icounter_struct __user *p_cuser;
1260 unsigned long templ;
1261 unsigned long flags;
1262 void __user *argp = (void __user *)arg;
1263
1264 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001265 return mxser_ioctl_special(cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001267 /* following add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1269 int opmode, p;
1270 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1271 int shiftbit;
1272 unsigned char val, mask;
1273
1274 p = info->port % 4;
1275 if (cmd == MOXA_SET_OP_MODE) {
1276 if (get_user(opmode, (int __user *) argp))
1277 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001278 if (opmode != RS232_MODE &&
1279 opmode != RS485_2WIRE_MODE &&
1280 opmode != RS422_MODE &&
1281 opmode != RS485_4WIRE_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return -EFAULT;
1283 mask = ModeMask[p];
1284 shiftbit = p * 2;
1285 val = inb(info->opmode_ioaddr);
1286 val &= mask;
1287 val |= (opmode << shiftbit);
1288 outb(val, info->opmode_ioaddr);
1289 } else {
1290 shiftbit = p * 2;
1291 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1292 opmode &= OP_MODE_MASK;
1293 if (copy_to_user(argp, &opmode, sizeof(int)))
1294 return -EFAULT;
1295 }
1296 return 0;
1297 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001298 /* above add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1301 if (tty->flags & (1 << TTY_IO_ERROR))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001302 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304 switch (cmd) {
1305 case TCSBRK: /* SVID version: non-zero arg --> no break */
1306 retval = tty_check_change(tty);
1307 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001308 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 tty_wait_until_sent(tty, 0);
1310 if (!arg)
1311 mxser_send_break(info, HZ / 4); /* 1/4 second */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 case TCSBRKP: /* support for POSIX tcsendbreak() */
1314 retval = tty_check_change(tty);
1315 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001316 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 tty_wait_until_sent(tty, 0);
1318 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 case TIOCGSOFTCAR:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001321 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 case TIOCSSOFTCAR:
1323 if (get_user(templ, (unsigned long __user *) argp))
1324 return -EFAULT;
1325 arg = templ;
1326 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 case TIOCGSERIAL:
1329 return mxser_get_serial_info(info, argp);
1330 case TIOCSSERIAL:
1331 return mxser_set_serial_info(info, argp);
1332 case TIOCSERGETLSR: /* Get line status register */
1333 return mxser_get_lsr_info(info, argp);
1334 /*
1335 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1336 * - mask passed in arg for lines of interest
1337 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1338 * Caller should use TIOCGICOUNT to see which one it was
1339 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001340 case TIOCMIWAIT: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 DECLARE_WAITQUEUE(wait, current);
1342 int ret;
1343 spin_lock_irqsave(&info->slock, flags);
1344 cprev = info->icount; /* note the counters on entry */
1345 spin_unlock_irqrestore(&info->slock, flags);
1346
1347 add_wait_queue(&info->delta_msr_wait, &wait);
1348 while (1) {
1349 spin_lock_irqsave(&info->slock, flags);
1350 cnow = info->icount; /* atomic copy */
1351 spin_unlock_irqrestore(&info->slock, flags);
1352
1353 set_current_state(TASK_INTERRUPTIBLE);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001354 if (((arg & TIOCM_RNG) &&
1355 (cnow.rng != cprev.rng)) ||
1356 ((arg & TIOCM_DSR) &&
1357 (cnow.dsr != cprev.dsr)) ||
1358 ((arg & TIOCM_CD) &&
1359 (cnow.dcd != cprev.dcd)) ||
1360 ((arg & TIOCM_CTS) &&
1361 (cnow.cts != cprev.cts))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 ret = 0;
1363 break;
1364 }
1365 /* see if a signal did it */
1366 if (signal_pending(current)) {
1367 ret = -ERESTARTSYS;
1368 break;
1369 }
1370 cprev = cnow;
1371 }
1372 current->state = TASK_RUNNING;
1373 remove_wait_queue(&info->delta_msr_wait, &wait);
1374 break;
1375 }
1376 /* NOTREACHED */
1377 /*
1378 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1379 * Return: write counters to the user passed counter struct
1380 * NB: both 1->0 and 0->1 transitions are counted except for
1381 * RI where only 0->1 is counted.
1382 */
1383 case TIOCGICOUNT:
1384 spin_lock_irqsave(&info->slock, flags);
1385 cnow = info->icount;
1386 spin_unlock_irqrestore(&info->slock, flags);
1387 p_cuser = argp;
1388 /* modified by casper 1/11/2000 */
1389 if (put_user(cnow.frame, &p_cuser->frame))
1390 return -EFAULT;
1391 if (put_user(cnow.brk, &p_cuser->brk))
1392 return -EFAULT;
1393 if (put_user(cnow.overrun, &p_cuser->overrun))
1394 return -EFAULT;
1395 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1396 return -EFAULT;
1397 if (put_user(cnow.parity, &p_cuser->parity))
1398 return -EFAULT;
1399 if (put_user(cnow.rx, &p_cuser->rx))
1400 return -EFAULT;
1401 if (put_user(cnow.tx, &p_cuser->tx))
1402 return -EFAULT;
1403 put_user(cnow.cts, &p_cuser->cts);
1404 put_user(cnow.dsr, &p_cuser->dsr);
1405 put_user(cnow.rng, &p_cuser->rng);
1406 put_user(cnow.dcd, &p_cuser->dcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return 0;
1408 case MOXA_HighSpeedOn:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001409 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1410 case MOXA_SDS_RSTICOUNTER: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 info->mon_data.rxcnt = 0;
1412 info->mon_data.txcnt = 0;
1413 return 0;
1414 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001415/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 case MOXA_ASPP_SETBAUD:{
1417 long baud;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001418 if (get_user(baud, (long __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return -EFAULT;
1420 mxser_set_baud(info, baud);
1421 return 0;
1422 }
1423 case MOXA_ASPP_GETBAUD:
1424 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1425 return -EFAULT;
1426
1427 return 0;
1428
1429 case MOXA_ASPP_OQUEUE:{
1430 int len, lsr;
1431
1432 len = mxser_chars_in_buffer(tty);
1433
1434 lsr = inb(info->base + UART_LSR) & UART_LSR_TEMT;
1435
1436 len += (lsr ? 0 : 1);
1437
1438 if (copy_to_user(argp, &len, sizeof(int)))
1439 return -EFAULT;
1440
1441 return 0;
1442 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001443 case MOXA_ASPP_MON: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 int mcr, status;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001445
1446 /* info->mon_data.ser_param = tty->termios->c_cflag; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 status = mxser_get_msr(info->base, 1, info->port, info);
1449 mxser_check_modem_status(info, status);
1450
1451 mcr = inb(info->base + UART_MCR);
1452 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1453 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1454 else
1455 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1456
1457 if (mcr & MOXA_MUST_MCR_TX_XON)
1458 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1459 else
1460 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1461
1462 if (info->tty->hw_stopped)
1463 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1464 else
1465 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1466
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001467 if (copy_to_user(argp, &info->mon_data,
1468 sizeof(struct mxser_mon)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return -EFAULT;
1470
1471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001474 case MOXA_ASPP_LSTATUS: {
1475 if (copy_to_user(argp, &info->err_shadow,
1476 sizeof(unsigned char)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return -EFAULT;
1478
1479 info->err_shadow = 0;
1480 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001482 case MOXA_SET_BAUD_METHOD: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 int method;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001484
1485 if (get_user(method, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return -EFAULT;
1487 mxser_set_baud_method[info->port] = method;
1488 if (copy_to_user(argp, &method, sizeof(int)))
1489 return -EFAULT;
1490
1491 return 0;
1492 }
1493 default:
1494 return -ENOIOCTLCMD;
1495 }
1496 return 0;
1497}
1498
1499#ifndef CMSPAR
1500#define CMSPAR 010000000000
1501#endif
1502
1503static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1504{
1505 int i, result, status;
1506
1507 switch (cmd) {
1508 case MOXA_GET_CONF:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001509 if (copy_to_user(argp, mxsercfg,
1510 sizeof(struct mxser_hwconf) * 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return -EFAULT;
1512 return 0;
1513 case MOXA_GET_MAJOR:
1514 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1515 return -EFAULT;
1516 return 0;
1517
1518 case MOXA_GET_CUMAJOR:
1519 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1520 return -EFAULT;
1521 return 0;
1522
1523 case MOXA_CHKPORTENABLE:
1524 result = 0;
1525 for (i = 0; i < MXSER_PORTS; i++) {
1526 if (mxvar_table[i].base)
1527 result |= (1 << i);
1528 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001529 return put_user(result, (unsigned long __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 case MOXA_GETDATACOUNT:
1531 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1532 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 case MOXA_GETMSTATUS:
1535 for (i = 0; i < MXSER_PORTS; i++) {
1536 GMStatus[i].ri = 0;
1537 if (!mxvar_table[i].base) {
1538 GMStatus[i].dcd = 0;
1539 GMStatus[i].dsr = 0;
1540 GMStatus[i].cts = 0;
1541 continue;
1542 }
1543
1544 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1545 GMStatus[i].cflag = mxvar_table[i].normal_termios.c_cflag;
1546 else
1547 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1548
1549 status = inb(mxvar_table[i].base + UART_MSR);
1550 if (status & 0x80 /*UART_MSR_DCD */ )
1551 GMStatus[i].dcd = 1;
1552 else
1553 GMStatus[i].dcd = 0;
1554
1555 if (status & 0x20 /*UART_MSR_DSR */ )
1556 GMStatus[i].dsr = 1;
1557 else
1558 GMStatus[i].dsr = 0;
1559
1560
1561 if (status & 0x10 /*UART_MSR_CTS */ )
1562 GMStatus[i].cts = 1;
1563 else
1564 GMStatus[i].cts = 0;
1565 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001566 if (copy_to_user(argp, GMStatus,
1567 sizeof(struct mxser_mstatus) * MXSER_PORTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return -EFAULT;
1569 return 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001570 case MOXA_ASPP_MON_EXT: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 int status;
1572 int opmode, p;
1573 int shiftbit;
1574 unsigned cflag, iflag;
1575
1576 for (i = 0; i < MXSER_PORTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (!mxvar_table[i].base)
1578 continue;
1579
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001580 status = mxser_get_msr(mxvar_table[i].base, 0,
1581 i, &(mxvar_table[i]));
1582 /*
1583 mxser_check_modem_status(&mxvar_table[i],
1584 status);
1585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (status & UART_MSR_TERI)
1587 mxvar_table[i].icount.rng++;
1588 if (status & UART_MSR_DDSR)
1589 mxvar_table[i].icount.dsr++;
1590 if (status & UART_MSR_DDCD)
1591 mxvar_table[i].icount.dcd++;
1592 if (status & UART_MSR_DCTS)
1593 mxvar_table[i].icount.cts++;
1594
1595 mxvar_table[i].mon_data.modem_status = status;
1596 mon_data_ext.rx_cnt[i] = mxvar_table[i].mon_data.rxcnt;
1597 mon_data_ext.tx_cnt[i] = mxvar_table[i].mon_data.txcnt;
1598 mon_data_ext.up_rxcnt[i] = mxvar_table[i].mon_data.up_rxcnt;
1599 mon_data_ext.up_txcnt[i] = mxvar_table[i].mon_data.up_txcnt;
1600 mon_data_ext.modem_status[i] = mxvar_table[i].mon_data.modem_status;
1601 mon_data_ext.baudrate[i] = mxvar_table[i].realbaud;
1602
1603 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios) {
1604 cflag = mxvar_table[i].normal_termios.c_cflag;
1605 iflag = mxvar_table[i].normal_termios.c_iflag;
1606 } else {
1607 cflag = mxvar_table[i].tty->termios->c_cflag;
1608 iflag = mxvar_table[i].tty->termios->c_iflag;
1609 }
1610
1611 mon_data_ext.databits[i] = cflag & CSIZE;
1612
1613 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1614
1615 mon_data_ext.parity[i] = cflag & (PARENB | PARODD | CMSPAR);
1616
1617 mon_data_ext.flowctrl[i] = 0x00;
1618
1619 if (cflag & CRTSCTS)
1620 mon_data_ext.flowctrl[i] |= 0x03;
1621
1622 if (iflag & (IXON | IXOFF))
1623 mon_data_ext.flowctrl[i] |= 0x0C;
1624
1625 if (mxvar_table[i].type == PORT_16550A)
1626 mon_data_ext.fifo[i] = 1;
1627 else
1628 mon_data_ext.fifo[i] = 0;
1629
1630 p = i % 4;
1631 shiftbit = p * 2;
1632 opmode = inb(mxvar_table[i].opmode_ioaddr) >> shiftbit;
1633 opmode &= OP_MODE_MASK;
1634
1635 mon_data_ext.iftype[i] = opmode;
1636
1637 }
1638 if (copy_to_user(argp, &mon_data_ext, sizeof(struct mxser_mon_ext)))
1639 return -EFAULT;
1640
1641 return 0;
1642
1643 }
1644 default:
1645 return -ENOIOCTLCMD;
1646 }
1647 return 0;
1648}
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650static void mxser_stoprx(struct tty_struct *tty)
1651{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001652 struct mxser_struct *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001653 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 info->ldisc_stop_rx = 1;
1656 if (I_IXOFF(tty)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001657 /* MX_LOCK(&info->slock); */
1658 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (info->IsMoxaMustChipFlag) {
1660 info->IER &= ~MOXA_MUST_RECV_ISR;
1661 outb(info->IER, info->base + UART_IER);
1662 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001663 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 info->x_char = STOP_CHAR(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001665 /* mask by Victor Yu. 09-02-2002 */
1666 /* outb(info->IER, 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 outb(0, info->base + UART_IER);
1668 info->IER |= UART_IER_THRI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001669 /* force Tx interrupt */
1670 outb(info->IER, info->base + UART_IER);
1671 } /* add by Victor Yu. 09-02-2002 */
1672 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674
1675 if (info->tty->termios->c_cflag & CRTSCTS) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001676 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 info->MCR &= ~UART_MCR_RTS;
1678 outb(info->MCR, info->base + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001679 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681}
1682
1683static void mxser_startrx(struct tty_struct *tty)
1684{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001685 struct mxser_struct *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001686 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 info->ldisc_stop_rx = 0;
1689 if (I_IXOFF(tty)) {
1690 if (info->x_char)
1691 info->x_char = 0;
1692 else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001693 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001695 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (info->IsMoxaMustChipFlag) {
1697 info->IER |= MOXA_MUST_RECV_ISR;
1698 outb(info->IER, info->base + UART_IER);
1699 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001700 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 info->x_char = START_CHAR(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001703 /* mask by Victor Yu. 09-02-2002 */
1704 /* outb(info->IER, 0); */
1705 /* add by Victor Yu. 09-02-2002 */
1706 outb(0, info->base + UART_IER);
1707 /* force Tx interrupt */
1708 info->IER |= UART_IER_THRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 outb(info->IER, info->base + UART_IER);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001710 } /* add by Victor Yu. 09-02-2002 */
1711 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713 }
1714
1715 if (info->tty->termios->c_cflag & CRTSCTS) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001716 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 info->MCR |= UART_MCR_RTS;
1718 outb(info->MCR, info->base + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001719 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721}
1722
1723/*
1724 * This routine is called by the upper-layer tty layer to signal that
1725 * incoming characters should be throttled.
1726 */
1727static void mxser_throttle(struct tty_struct *tty)
1728{
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001729 /* struct mxser_struct *info = tty->driver_data; */
1730 /* unsigned long flags; */
1731
1732 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001734 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
1737static void mxser_unthrottle(struct tty_struct *tty)
1738{
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001739 /* struct mxser_struct *info = tty->driver_data; */
1740 /* unsigned long flags; */
1741
1742 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 mxser_startrx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001744 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
1747static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1748{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001749 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 unsigned long flags;
1751
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001752 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1753 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 mxser_change_speed(info, old_termios);
1756
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001757 if ((old_termios->c_cflag & CRTSCTS) &&
1758 !(tty->termios->c_cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 tty->hw_stopped = 0;
1760 mxser_start(tty);
1761 }
1762 }
1763
1764/* Handle sw stopped */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001765 if ((old_termios->c_iflag & IXON) &&
1766 !(tty->termios->c_iflag & IXON)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 tty->stopped = 0;
1768
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001769 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (info->IsMoxaMustChipFlag) {
1771 spin_lock_irqsave(&info->slock, flags);
1772 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
1773 spin_unlock_irqrestore(&info->slock, flags);
1774 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001775 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 mxser_start(tty);
1778 }
1779}
1780
1781/*
1782 * mxser_stop() and mxser_start()
1783 *
1784 * This routines are called before setting or resetting tty->stopped.
1785 * They enable or disable transmitter interrupts, as necessary.
1786 */
1787static void mxser_stop(struct tty_struct *tty)
1788{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001789 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 unsigned long flags;
1791
1792 spin_lock_irqsave(&info->slock, flags);
1793 if (info->IER & UART_IER_THRI) {
1794 info->IER &= ~UART_IER_THRI;
1795 outb(info->IER, info->base + UART_IER);
1796 }
1797 spin_unlock_irqrestore(&info->slock, flags);
1798}
1799
1800static void mxser_start(struct tty_struct *tty)
1801{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001802 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 unsigned long flags;
1804
1805 spin_lock_irqsave(&info->slock, flags);
1806 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
1807 info->IER |= UART_IER_THRI;
1808 outb(info->IER, info->base + UART_IER);
1809 }
1810 spin_unlock_irqrestore(&info->slock, flags);
1811}
1812
1813/*
1814 * mxser_wait_until_sent() --- wait until the transmitter is empty
1815 */
1816static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1817{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001818 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 unsigned long orig_jiffies, char_time;
1820 int lsr;
1821
1822 if (info->type == PORT_UNKNOWN)
1823 return;
1824
1825 if (info->xmit_fifo_size == 0)
1826 return; /* Just in case.... */
1827
1828 orig_jiffies = jiffies;
1829 /*
1830 * Set the check interval to be 1/5 of the estimated time to
1831 * send a single character, and make it at least 1. The check
1832 * interval should also be less than the timeout.
1833 *
1834 * Note: we have to use pretty tight timings here to satisfy
1835 * the NIST-PCTS.
1836 */
1837 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1838 char_time = char_time / 5;
1839 if (char_time == 0)
1840 char_time = 1;
1841 if (timeout && timeout < char_time)
1842 char_time = timeout;
1843 /*
1844 * If the transmitter hasn't cleared in twice the approximate
1845 * amount of time to send the entire FIFO, it probably won't
1846 * ever clear. This assumes the UART isn't doing flow
1847 * control, which is currently the case. Hence, if it ever
1848 * takes longer than info->timeout, this is probably due to a
1849 * UART bug of some kind. So, we clamp the timeout parameter at
1850 * 2*info->timeout.
1851 */
1852 if (!timeout || timeout > 2 * info->timeout)
1853 timeout = 2 * info->timeout;
1854#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001855 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1856 timeout, char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 printk("jiff=%lu...", jiffies);
1858#endif
1859 while (!((lsr = inb(info->base + UART_LSR)) & UART_LSR_TEMT)) {
1860#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1861 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1862#endif
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001863 schedule_timeout_interruptible(char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (signal_pending(current))
1865 break;
1866 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1867 break;
1868 }
1869 set_current_state(TASK_RUNNING);
1870
1871#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1872 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1873#endif
1874}
1875
1876
1877/*
1878 * This routine is called by tty_hangup() when a hangup is signaled.
1879 */
1880void mxser_hangup(struct tty_struct *tty)
1881{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001882 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 mxser_flush_buffer(tty);
1885 mxser_shutdown(info);
1886 info->event = 0;
1887 info->count = 0;
1888 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1889 info->tty = NULL;
1890 wake_up_interruptible(&info->open_wait);
1891}
1892
1893
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001894/* added by James 03-12-2004. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895/*
1896 * mxser_rs_break() --- routine which turns the break handling on or off
1897 */
1898static void mxser_rs_break(struct tty_struct *tty, int break_state)
1899{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001900 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 unsigned long flags;
1902
1903 spin_lock_irqsave(&info->slock, flags);
1904 if (break_state == -1)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001905 outb(inb(info->base + UART_LCR) | UART_LCR_SBC,
1906 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001908 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC,
1909 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 spin_unlock_irqrestore(&info->slock, flags);
1911}
1912
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001913/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915
1916/*
1917 * This is the serial driver's generic interrupt routine
1918 */
1919static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1920{
1921 int status, iir, i;
1922 struct mxser_struct *info;
1923 struct mxser_struct *port;
1924 int max, irqbits, bits, msr;
1925 int pass_counter = 0;
1926 int handled = IRQ_NONE;
1927
1928 port = NULL;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001929 /* spin_lock(&gm_lock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 for (i = 0; i < MXSER_BOARDS; i++) {
1932 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1933 port = dev_id;
1934 break;
1935 }
1936 }
1937
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001938 if (i == MXSER_BOARDS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 goto irq_stop;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001940 if (port == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 goto irq_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 max = mxser_numports[mxsercfg[i].board_type - 1];
1943 while (1) {
1944 irqbits = inb(port->vector) & port->vectormask;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001945 if (irqbits == port->vectormask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 handled = IRQ_HANDLED;
1949 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001950 if (irqbits == port->vectormask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (bits & irqbits)
1953 continue;
1954 info = port + i;
1955
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001956 /* following add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 iir = inb(info->base + UART_IIR);
1958 if (iir & UART_IIR_NO_INT)
1959 continue;
1960 iir &= MOXA_MUST_IIR_MASK;
1961 if (!info->tty) {
1962 status = inb(info->base + UART_LSR);
1963 outb(0x27, info->base + UART_FCR);
1964 inb(info->base + UART_MSR);
1965 continue;
1966 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001967 /* above add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 /*
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001969 if (info->tty->flip.count < TTY_FLIPBUF_SIZE / 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 info->IER |= MOXA_MUST_RECV_ISR;
1971 outb(info->IER, info->base + UART_IER);
1972 }
1973 */
1974
1975
1976 /* mask by Victor Yu. 09-13-2002
1977 if ( !info->tty ||
1978 (inb(info->base + UART_IIR) & UART_IIR_NO_INT) )
1979 continue;
1980 */
1981 /* mask by Victor Yu. 09-02-2002
1982 status = inb(info->base + UART_LSR) & info->read_status_mask;
1983 */
1984
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001985 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 status = inb(info->base + UART_LSR);
1987
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001988 if (status & UART_LSR_PE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 info->err_shadow |= NPPI_NOTIFY_PARITY;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001990 if (status & UART_LSR_FE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 info->err_shadow |= NPPI_NOTIFY_FRAMING;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001992 if (status & UART_LSR_OE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 info->err_shadow |= NPPI_NOTIFY_HW_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (status & UART_LSR_BI)
1995 info->err_shadow |= NPPI_NOTIFY_BREAK;
1996
1997 if (info->IsMoxaMustChipFlag) {
1998 /*
1999 if ( (status & 0x02) && !(status & 0x01) ) {
2000 outb(info->base+UART_FCR, 0x23);
2001 continue;
2002 }
2003 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002004 if (iir == MOXA_MUST_IIR_GDA ||
2005 iir == MOXA_MUST_IIR_RDA ||
2006 iir == MOXA_MUST_IIR_RTO ||
2007 iir == MOXA_MUST_IIR_LSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 mxser_receive_chars(info, &status);
2009
2010 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002011 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 status &= info->read_status_mask;
2014 if (status & UART_LSR_DR)
2015 mxser_receive_chars(info, &status);
2016 }
2017 msr = inb(info->base + UART_MSR);
2018 if (msr & UART_MSR_ANY_DELTA) {
2019 mxser_check_modem_status(info, msr);
2020 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002021 /* following add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (info->IsMoxaMustChipFlag) {
2023 if ((iir == 0x02) && (status & UART_LSR_THRE)) {
2024 mxser_transmit_chars(info);
2025 }
2026 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002027 /* above add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 if (status & UART_LSR_THRE) {
2030/* 8-2-99 by William
2031 if ( info->x_char || (info->xmit_cnt > 0) )
2032*/
2033 mxser_transmit_chars(info);
2034 }
2035 }
2036 }
2037 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
2038 break; /* Prevent infinite loops */
2039 }
2040 }
2041
2042 irq_stop:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002043 /* spin_unlock(&gm_lock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return handled;
2045}
2046
2047static void mxser_receive_chars(struct mxser_struct *info, int *status)
2048{
2049 struct tty_struct *tty = info->tty;
2050 unsigned char ch, gdl;
2051 int ignored = 0;
2052 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 int recv_room;
2054 int max = 256;
2055 unsigned long flags;
2056
2057 spin_lock_irqsave(&info->slock, flags);
2058
Alan Cox33f0f882006-01-09 20:54:13 -08002059 recv_room = tty->receive_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if ((recv_room == 0) && (!info->ldisc_stop_rx)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002061 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002063 /* return; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002066 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
2068
2069 if (*status & UART_LSR_SPECIAL) {
2070 goto intr_old;
2071 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002072 /* following add by Victor Yu. 02-11-2004 */
2073 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID &&
2074 (*status & MOXA_MUST_LSR_RERR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 goto intr_old;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002076 /* above add by Victor Yu. 02-14-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (*status & MOXA_MUST_LSR_RERR)
2078 goto intr_old;
2079
2080 gdl = inb(info->base + MOXA_MUST_GDL_REGISTER);
2081
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002082 /* add by Victor Yu. 02-11-2004 */
2083 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU150_HWID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 gdl &= MOXA_MUST_GDL_MASK;
2085 if (gdl >= recv_room) {
2086 if (!info->ldisc_stop_rx) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002087 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 mxser_stoprx(tty);
2089 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002090 /* return; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
2092 while (gdl--) {
2093 ch = inb(info->base + UART_RX);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002094 tty_insert_flip_char(tty, ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 cnt++;
2096 /*
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002097 if ((cnt >= HI_WATER) && (info->stop_rx == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002099 info->stop_rx = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 break;
2101 } */
2102 }
2103 goto end_intr;
2104 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002105 intr_old:
2106 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 do {
2109 if (max-- < 0)
2110 break;
2111 /*
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002112 if ((cnt >= HI_WATER) && (info->stop_rx == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 mxser_stoprx(tty);
2114 info->stop_rx=1;
2115 break;
2116 }
2117 */
2118
2119 ch = inb(info->base + UART_RX);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002120 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (info->IsMoxaMustChipFlag && (*status & UART_LSR_OE) /*&& !(*status&UART_LSR_DR) */ )
2122 outb(0x23, info->base + UART_FCR);
2123 *status &= info->read_status_mask;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002124 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (*status & info->ignore_status_mask) {
2126 if (++ignored > 100)
2127 break;
2128 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002129 char flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (*status & UART_LSR_SPECIAL) {
2131 if (*status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002132 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133/* added by casper 1/11/2000 */
2134 info->icount.brk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135/* */
2136 if (info->flags & ASYNC_SAK)
2137 do_SAK(tty);
2138 } else if (*status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002139 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140/* added by casper 1/11/2000 */
2141 info->icount.parity++;
2142/* */
2143 } else if (*status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002144 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145/* added by casper 1/11/2000 */
2146 info->icount.frame++;
2147/* */
2148 } else if (*status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002149 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150/* added by casper 1/11/2000 */
2151 info->icount.overrun++;
2152/* */
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002153 }
2154 }
2155 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 cnt++;
2157 if (cnt >= recv_room) {
2158 if (!info->ldisc_stop_rx) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002159 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 mxser_stoprx(tty);
2161 }
2162 break;
2163 }
2164
2165 }
2166
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002167 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (info->IsMoxaMustChipFlag)
2169 break;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002170 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 /* mask by Victor Yu. 09-02-2002
2173 *status = inb(info->base + UART_LSR) & info->read_status_mask;
2174 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002175 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 *status = inb(info->base + UART_LSR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002177 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 } while (*status & UART_LSR_DR);
2179
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002180end_intr: /* add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 mxvar_log.rxcnt[info->port] += cnt;
2182 info->mon_data.rxcnt += cnt;
2183 info->mon_data.up_rxcnt += cnt;
2184 spin_unlock_irqrestore(&info->slock, flags);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 tty_flip_buffer_push(tty);
2187}
2188
2189static void mxser_transmit_chars(struct mxser_struct *info)
2190{
2191 int count, cnt;
2192 unsigned long flags;
2193
2194 spin_lock_irqsave(&info->slock, flags);
2195
2196 if (info->x_char) {
2197 outb(info->x_char, info->base + UART_TX);
2198 info->x_char = 0;
2199 mxvar_log.txcnt[info->port]++;
2200 info->mon_data.txcnt++;
2201 info->mon_data.up_txcnt++;
2202
2203/* added by casper 1/11/2000 */
2204 info->icount.tx++;
2205/* */
2206 spin_unlock_irqrestore(&info->slock, flags);
2207 return;
2208 }
2209
2210 if (info->xmit_buf == 0) {
2211 spin_unlock_irqrestore(&info->slock, flags);
2212 return;
2213 }
2214
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002215 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
2216 (info->tty->hw_stopped &&
2217 (info->type != PORT_16550A) &&
2218 (!info->IsMoxaMustChipFlag))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 info->IER &= ~UART_IER_THRI;
2220 outb(info->IER, info->base + UART_IER);
2221 spin_unlock_irqrestore(&info->slock, flags);
2222 return;
2223 }
2224
2225 cnt = info->xmit_cnt;
2226 count = info->xmit_fifo_size;
2227 do {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002228 outb(info->xmit_buf[info->xmit_tail++],
2229 info->base + UART_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2231 if (--info->xmit_cnt <= 0)
2232 break;
2233 } while (--count > 0);
2234 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
2235
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002236/* added by James 03-12-2004. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 info->mon_data.txcnt += (cnt - info->xmit_cnt);
2238 info->mon_data.up_txcnt += (cnt - info->xmit_cnt);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002239/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241/* added by casper 1/11/2000 */
2242 info->icount.tx += (cnt - info->xmit_cnt);
2243/* */
2244
2245 if (info->xmit_cnt < WAKEUP_CHARS) {
2246 set_bit(MXSER_EVENT_TXLOW, &info->event);
2247 schedule_work(&info->tqueue);
2248 }
2249 if (info->xmit_cnt <= 0) {
2250 info->IER &= ~UART_IER_THRI;
2251 outb(info->IER, info->base + UART_IER);
2252 }
2253 spin_unlock_irqrestore(&info->slock, flags);
2254}
2255
2256static void mxser_check_modem_status(struct mxser_struct *info, int status)
2257{
2258 /* update input line counters */
2259 if (status & UART_MSR_TERI)
2260 info->icount.rng++;
2261 if (status & UART_MSR_DDSR)
2262 info->icount.dsr++;
2263 if (status & UART_MSR_DDCD)
2264 info->icount.dcd++;
2265 if (status & UART_MSR_DCTS)
2266 info->icount.cts++;
2267 info->mon_data.modem_status = status;
2268 wake_up_interruptible(&info->delta_msr_wait);
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
2271 if (status & UART_MSR_DCD)
2272 wake_up_interruptible(&info->open_wait);
2273 schedule_work(&info->tqueue);
2274 }
2275
2276 if (info->flags & ASYNC_CTS_FLOW) {
2277 if (info->tty->hw_stopped) {
2278 if (status & UART_MSR_CTS) {
2279 info->tty->hw_stopped = 0;
2280
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002281 if ((info->type != PORT_16550A) &&
2282 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 info->IER |= UART_IER_THRI;
2284 outb(info->IER, info->base + UART_IER);
2285 }
2286 set_bit(MXSER_EVENT_TXLOW, &info->event);
2287 schedule_work(&info->tqueue); }
2288 } else {
2289 if (!(status & UART_MSR_CTS)) {
2290 info->tty->hw_stopped = 1;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002291 if ((info->type != PORT_16550A) &&
2292 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 info->IER &= ~UART_IER_THRI;
2294 outb(info->IER, info->base + UART_IER);
2295 }
2296 }
2297 }
2298 }
2299}
2300
2301static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_struct *info)
2302{
2303 DECLARE_WAITQUEUE(wait, current);
2304 int retval;
2305 int do_clocal = 0;
2306 unsigned long flags;
2307
2308 /*
2309 * If non-blocking mode is set, or the port is not enabled,
2310 * then make the check up front and then exit.
2311 */
2312 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
2313 info->flags |= ASYNC_NORMAL_ACTIVE;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
2316
2317 if (tty->termios->c_cflag & CLOCAL)
2318 do_clocal = 1;
2319
2320 /*
2321 * Block waiting for the carrier detect and the line to become
2322 * free (i.e., not in use by the callout). While we are in
2323 * this loop, info->count is dropped by one, so that
2324 * mxser_close() knows when to free things. We restore it upon
2325 * exit, either normal or abnormal.
2326 */
2327 retval = 0;
2328 add_wait_queue(&info->open_wait, &wait);
2329
2330 spin_lock_irqsave(&info->slock, flags);
2331 if (!tty_hung_up_p(filp))
2332 info->count--;
2333 spin_unlock_irqrestore(&info->slock, flags);
2334 info->blocked_open++;
2335 while (1) {
2336 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002337 outb(inb(info->base + UART_MCR) |
2338 UART_MCR_DTR | UART_MCR_RTS, info->base + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 spin_unlock_irqrestore(&info->slock, flags);
2340 set_current_state(TASK_INTERRUPTIBLE);
2341 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
2342 if (info->flags & ASYNC_HUP_NOTIFY)
2343 retval = -EAGAIN;
2344 else
2345 retval = -ERESTARTSYS;
2346 break;
2347 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002348 if (!(info->flags & ASYNC_CLOSING) &&
2349 (do_clocal ||
2350 (inb(info->base + UART_MSR) & UART_MSR_DCD)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 break;
2352 if (signal_pending(current)) {
2353 retval = -ERESTARTSYS;
2354 break;
2355 }
2356 schedule();
2357 }
2358 set_current_state(TASK_RUNNING);
2359 remove_wait_queue(&info->open_wait, &wait);
2360 if (!tty_hung_up_p(filp))
2361 info->count++;
2362 info->blocked_open--;
2363 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002364 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 info->flags |= ASYNC_NORMAL_ACTIVE;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
2369static int mxser_startup(struct mxser_struct *info)
2370{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 unsigned long page;
2372 unsigned long flags;
2373
2374 page = __get_free_page(GFP_KERNEL);
2375 if (!page)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002376 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378 spin_lock_irqsave(&info->slock, flags);
2379
2380 if (info->flags & ASYNC_INITIALIZED) {
2381 free_page(page);
2382 spin_unlock_irqrestore(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
2385
2386 if (!info->base || !info->type) {
2387 if (info->tty)
2388 set_bit(TTY_IO_ERROR, &info->tty->flags);
2389 free_page(page);
2390 spin_unlock_irqrestore(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393 if (info->xmit_buf)
2394 free_page(page);
2395 else
2396 info->xmit_buf = (unsigned char *) page;
2397
2398 /*
2399 * Clear the FIFO buffers and disable them
2400 * (they will be reenabled in mxser_change_speed())
2401 */
2402 if (info->IsMoxaMustChipFlag)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002403 outb((UART_FCR_CLEAR_RCVR |
2404 UART_FCR_CLEAR_XMIT |
2405 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002407 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2408 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 /*
2411 * At this point there's no way the LSR could still be 0xFF;
2412 * if it is, then bail out, because there's likely no UART
2413 * here.
2414 */
2415 if (inb(info->base + UART_LSR) == 0xff) {
2416 spin_unlock_irqrestore(&info->slock, flags);
2417 if (capable(CAP_SYS_ADMIN)) {
2418 if (info->tty)
2419 set_bit(TTY_IO_ERROR, &info->tty->flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 } else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002422 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
2424
2425 /*
2426 * Clear the interrupt registers.
2427 */
2428 (void) inb(info->base + UART_LSR);
2429 (void) inb(info->base + UART_RX);
2430 (void) inb(info->base + UART_IIR);
2431 (void) inb(info->base + UART_MSR);
2432
2433 /*
2434 * Now, initialize the UART
2435 */
2436 outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
2437 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
2438 outb(info->MCR, info->base + UART_MCR);
2439
2440 /*
2441 * Finally, enable interrupts
2442 */
2443 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002444 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002446 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (info->IsMoxaMustChipFlag)
2448 info->IER |= MOXA_MUST_IER_EGDAI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002449 /* above add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 outb(info->IER, info->base + UART_IER); /* enable interrupts */
2451
2452 /*
2453 * And clear the interrupt registers again for luck.
2454 */
2455 (void) inb(info->base + UART_LSR);
2456 (void) inb(info->base + UART_RX);
2457 (void) inb(info->base + UART_IIR);
2458 (void) inb(info->base + UART_MSR);
2459
2460 if (info->tty)
2461 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2462 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2463
2464 /*
2465 * and set the speed of the serial port
2466 */
2467 spin_unlock_irqrestore(&info->slock, flags);
2468 mxser_change_speed(info, NULL);
2469
2470 info->flags |= ASYNC_INITIALIZED;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
2474/*
2475 * This routine will shutdown a serial port; interrupts maybe disabled, and
2476 * DTR is dropped if the hangup on close termio flag is on.
2477 */
2478static void mxser_shutdown(struct mxser_struct *info)
2479{
2480 unsigned long flags;
2481
2482 if (!(info->flags & ASYNC_INITIALIZED))
2483 return;
2484
2485 spin_lock_irqsave(&info->slock, flags);
2486
2487 /*
2488 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2489 * here so the queue might never be waken up
2490 */
2491 wake_up_interruptible(&info->delta_msr_wait);
2492
2493 /*
2494 * Free the IRQ, if necessary
2495 */
2496 if (info->xmit_buf) {
2497 free_page((unsigned long) info->xmit_buf);
2498 info->xmit_buf = NULL;
2499 }
2500
2501 info->IER = 0;
2502 outb(0x00, info->base + UART_IER);
2503
2504 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
2505 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
2506 outb(info->MCR, info->base + UART_MCR);
2507
2508 /* clear Rx/Tx FIFO's */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002509 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 if (info->IsMoxaMustChipFlag)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002511 outb((UART_FCR_CLEAR_RCVR |
2512 UART_FCR_CLEAR_XMIT |
2513 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002515 /* above add by Victor Yu. 08-30-2002 */
2516 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2517 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 /* read data port to reset things */
2520 (void) inb(info->base + UART_RX);
2521
2522 if (info->tty)
2523 set_bit(TTY_IO_ERROR, &info->tty->flags);
2524
2525 info->flags &= ~ASYNC_INITIALIZED;
2526
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002527 /* following add by Victor Yu. 09-23-2002 */
2528 if (info->IsMoxaMustChipFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->base);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002530 /* above add by Victor Yu. 09-23-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532 spin_unlock_irqrestore(&info->slock, flags);
2533}
2534
2535/*
2536 * This routine is called to set the UART divisor registers to match
2537 * the specified baud rate for a serial port.
2538 */
2539static int mxser_change_speed(struct mxser_struct *info, struct termios *old_termios)
2540{
2541 unsigned cflag, cval, fcr;
2542 int ret = 0;
2543 unsigned char status;
2544 long baud;
2545 unsigned long flags;
2546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (!info->tty || !info->tty->termios)
2548 return ret;
2549 cflag = info->tty->termios->c_cflag;
2550 if (!(info->base))
2551 return ret;
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553#ifndef B921600
2554#define B921600 (B460800 +1)
2555#endif
2556 if (mxser_set_baud_method[info->port] == 0) {
Alan Coxc7bce302006-09-30 23:27:24 -07002557 baud = tty_get_baud_rate(info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 mxser_set_baud(info, baud);
2559 }
2560
2561 /* byte size and parity */
2562 switch (cflag & CSIZE) {
2563 case CS5:
2564 cval = 0x00;
2565 break;
2566 case CS6:
2567 cval = 0x01;
2568 break;
2569 case CS7:
2570 cval = 0x02;
2571 break;
2572 case CS8:
2573 cval = 0x03;
2574 break;
2575 default:
2576 cval = 0x00;
2577 break; /* too keep GCC shut... */
2578 }
2579 if (cflag & CSTOPB)
2580 cval |= 0x04;
2581 if (cflag & PARENB)
2582 cval |= UART_LCR_PARITY;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002583 if (!(cflag & PARODD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 cval |= UART_LCR_EPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (cflag & CMSPAR)
2586 cval |= UART_LCR_SPAR;
2587
2588 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
2589 if (info->IsMoxaMustChipFlag) {
2590 fcr = UART_FCR_ENABLE_FIFO;
2591 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2592 SET_MOXA_MUST_FIFO_VALUE(info);
2593 } else
2594 fcr = 0;
2595 } else {
2596 fcr = UART_FCR_ENABLE_FIFO;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002597 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 if (info->IsMoxaMustChipFlag) {
2599 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2600 SET_MOXA_MUST_FIFO_VALUE(info);
2601 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002602 /* above add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 switch (info->rx_trigger) {
2604 case 1:
2605 fcr |= UART_FCR_TRIGGER_1;
2606 break;
2607 case 4:
2608 fcr |= UART_FCR_TRIGGER_4;
2609 break;
2610 case 8:
2611 fcr |= UART_FCR_TRIGGER_8;
2612 break;
2613 default:
2614 fcr |= UART_FCR_TRIGGER_14;
2615 break;
2616 }
2617 }
2618 }
2619
2620 /* CTS flow control flag and modem status interrupts */
2621 info->IER &= ~UART_IER_MSI;
2622 info->MCR &= ~UART_MCR_AFE;
2623 if (cflag & CRTSCTS) {
2624 info->flags |= ASYNC_CTS_FLOW;
2625 info->IER |= UART_IER_MSI;
2626 if ((info->type == PORT_16550A) || (info->IsMoxaMustChipFlag)) {
2627 info->MCR |= UART_MCR_AFE;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002628 /* status = mxser_get_msr(info->base, 0, info->port); */
2629/*
2630 save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 cli();
2632 status = inb(baseaddr + UART_MSR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002633 restore_flags(flags);
2634*/
2635 /* mxser_check_modem_status(info, status); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002637 /* status = mxser_get_msr(info->base, 0, info->port); */
2638 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 status = inb(info->base + UART_MSR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002640 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 if (info->tty->hw_stopped) {
2642 if (status & UART_MSR_CTS) {
2643 info->tty->hw_stopped = 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002644 if ((info->type != PORT_16550A) &&
2645 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 info->IER |= UART_IER_THRI;
2647 outb(info->IER, info->base + UART_IER);
2648 }
2649 set_bit(MXSER_EVENT_TXLOW, &info->event);
2650 schedule_work(&info->tqueue); }
2651 } else {
2652 if (!(status & UART_MSR_CTS)) {
2653 info->tty->hw_stopped = 1;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002654 if ((info->type != PORT_16550A) &&
2655 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 info->IER &= ~UART_IER_THRI;
2657 outb(info->IER, info->base + UART_IER);
2658 }
2659 }
2660 }
2661 }
2662 } else {
2663 info->flags &= ~ASYNC_CTS_FLOW;
2664 }
2665 outb(info->MCR, info->base + UART_MCR);
2666 if (cflag & CLOCAL) {
2667 info->flags &= ~ASYNC_CHECK_CD;
2668 } else {
2669 info->flags |= ASYNC_CHECK_CD;
2670 info->IER |= UART_IER_MSI;
2671 }
2672 outb(info->IER, info->base + UART_IER);
2673
2674 /*
2675 * Set up parity check flag
2676 */
2677 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2678 if (I_INPCK(info->tty))
2679 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2680 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2681 info->read_status_mask |= UART_LSR_BI;
2682
2683 info->ignore_status_mask = 0;
2684
2685 if (I_IGNBRK(info->tty)) {
2686 info->ignore_status_mask |= UART_LSR_BI;
2687 info->read_status_mask |= UART_LSR_BI;
2688 /*
2689 * If we're ignore parity and break indicators, ignore
2690 * overruns too. (For real raw support).
2691 */
2692 if (I_IGNPAR(info->tty)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002693 info->ignore_status_mask |=
2694 UART_LSR_OE |
2695 UART_LSR_PE |
2696 UART_LSR_FE;
2697 info->read_status_mask |=
2698 UART_LSR_OE |
2699 UART_LSR_PE |
2700 UART_LSR_FE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 }
2702 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002703 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 if (info->IsMoxaMustChipFlag) {
2705 spin_lock_irqsave(&info->slock, flags);
2706 SET_MOXA_MUST_XON1_VALUE(info->base, START_CHAR(info->tty));
2707 SET_MOXA_MUST_XOFF1_VALUE(info->base, STOP_CHAR(info->tty));
2708 if (I_IXON(info->tty)) {
2709 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2710 } else {
2711 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2712 }
2713 if (I_IXOFF(info->tty)) {
2714 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2715 } else {
2716 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2717 }
2718 /*
2719 if ( I_IXANY(info->tty) ) {
2720 info->MCR |= MOXA_MUST_MCR_XON_ANY;
2721 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2722 } else {
2723 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
2724 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2725 }
2726 */
2727 spin_unlock_irqrestore(&info->slock, flags);
2728 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002729 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731
2732 outb(fcr, info->base + UART_FCR); /* set fcr */
2733 outb(cval, info->base + UART_LCR);
2734
2735 return ret;
2736}
2737
2738
2739static int mxser_set_baud(struct mxser_struct *info, long newspd)
2740{
2741 int quot = 0;
2742 unsigned char cval;
2743 int ret = 0;
2744 unsigned long flags;
2745
2746 if (!info->tty || !info->tty->termios)
2747 return ret;
2748
2749 if (!(info->base))
2750 return ret;
2751
2752 if (newspd > info->MaxCanSetBaudRate)
2753 return 0;
2754
2755 info->realbaud = newspd;
2756 if (newspd == 134) {
2757 quot = (2 * info->baud_base / 269);
2758 } else if (newspd) {
2759 quot = info->baud_base / newspd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 if (quot == 0)
2761 quot = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 } else {
2763 quot = 0;
2764 }
2765
2766 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
2767 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2768
2769 if (quot) {
2770 spin_lock_irqsave(&info->slock, flags);
2771 info->MCR |= UART_MCR_DTR;
2772 outb(info->MCR, info->base + UART_MCR);
2773 spin_unlock_irqrestore(&info->slock, flags);
2774 } else {
2775 spin_lock_irqsave(&info->slock, flags);
2776 info->MCR &= ~UART_MCR_DTR;
2777 outb(info->MCR, info->base + UART_MCR);
2778 spin_unlock_irqrestore(&info->slock, flags);
2779 return ret;
2780 }
2781
2782 cval = inb(info->base + UART_LCR);
2783
2784 outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2785
2786 outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2787 outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2788 outb(cval, info->base + UART_LCR); /* reset DLAB */
2789
2790
2791 return ret;
2792}
2793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794/*
2795 * ------------------------------------------------------------
2796 * friends of mxser_ioctl()
2797 * ------------------------------------------------------------
2798 */
2799static int mxser_get_serial_info(struct mxser_struct *info, struct serial_struct __user *retinfo)
2800{
2801 struct serial_struct tmp;
2802
2803 if (!retinfo)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002804 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 memset(&tmp, 0, sizeof(tmp));
2806 tmp.type = info->type;
2807 tmp.line = info->port;
2808 tmp.port = info->base;
2809 tmp.irq = info->irq;
2810 tmp.flags = info->flags;
2811 tmp.baud_base = info->baud_base;
2812 tmp.close_delay = info->close_delay;
2813 tmp.closing_wait = info->closing_wait;
2814 tmp.custom_divisor = info->custom_divisor;
2815 tmp.hub6 = 0;
2816 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2817 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
2820
2821static int mxser_set_serial_info(struct mxser_struct *info, struct serial_struct __user *new_info)
2822{
2823 struct serial_struct new_serial;
2824 unsigned int flags;
2825 int retval = 0;
2826
2827 if (!new_info || !info->base)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002828 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2830 return -EFAULT;
2831
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002832 if ((new_serial.irq != info->irq) ||
2833 (new_serial.port != info->base) ||
2834 (new_serial.custom_divisor != info->custom_divisor) ||
2835 (new_serial.baud_base != info->baud_base))
2836 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 flags = info->flags & ASYNC_SPD_MASK;
2839
2840 if (!capable(CAP_SYS_ADMIN)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002841 if ((new_serial.baud_base != info->baud_base) ||
2842 (new_serial.close_delay != info->close_delay) ||
2843 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
2844 return -EPERM;
2845 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2846 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 } else {
2848 /*
2849 * OK, past this point, all the error checking has been done.
2850 * At this point, we start making changes.....
2851 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002852 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2853 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 info->close_delay = new_serial.close_delay * HZ / 100;
2855 info->closing_wait = new_serial.closing_wait * HZ / 100;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002856 info->tty->low_latency =
2857 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2858 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 }
2860
2861 /* added by casper, 3/17/2000, for mouse */
2862 info->type = new_serial.type;
2863
2864 process_txrx_fifo(info);
2865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (info->flags & ASYNC_INITIALIZED) {
2867 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2868 mxser_change_speed(info, NULL);
2869 }
2870 } else {
2871 retval = mxser_startup(info);
2872 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002873 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874}
2875
2876/*
2877 * mxser_get_lsr_info - get line status register info
2878 *
2879 * Purpose: Let user call ioctl() to get info when the UART physically
2880 * is emptied. On bus types like RS485, the transmitter must
2881 * release the bus after transmitting. This must be done when
2882 * the transmit shift register is empty, not be done when the
2883 * transmit holding register is empty. This functionality
2884 * allows an RS485 driver to be written in user space.
2885 */
2886static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int __user *value)
2887{
2888 unsigned char status;
2889 unsigned int result;
2890 unsigned long flags;
2891
2892 spin_lock_irqsave(&info->slock, flags);
2893 status = inb(info->base + UART_LSR);
2894 spin_unlock_irqrestore(&info->slock, flags);
2895 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2896 return put_user(result, value);
2897}
2898
2899/*
2900 * This routine sends a break character out the serial port.
2901 */
2902static void mxser_send_break(struct mxser_struct *info, int duration)
2903{
2904 unsigned long flags;
2905
2906 if (!info->base)
2907 return;
2908 set_current_state(TASK_INTERRUPTIBLE);
2909 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002910 outb(inb(info->base + UART_LCR) | UART_LCR_SBC,
2911 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 spin_unlock_irqrestore(&info->slock, flags);
2913 schedule_timeout(duration);
2914 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002915 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC,
2916 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 spin_unlock_irqrestore(&info->slock, flags);
2918}
2919
2920static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2921{
Jesper Juhl56e139f2006-06-25 05:47:52 -07002922 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 unsigned char control, status;
2924 unsigned long flags;
2925
2926
2927 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002928 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 if (tty->flags & (1 << TTY_IO_ERROR))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002930 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
2932 control = info->MCR;
2933
2934 spin_lock_irqsave(&info->slock, flags);
2935 status = inb(info->base + UART_MSR);
2936 if (status & UART_MSR_ANY_DELTA)
2937 mxser_check_modem_status(info, status);
2938 spin_unlock_irqrestore(&info->slock, flags);
2939 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002940 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2941 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2942 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2943 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2944 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
2946
2947static int mxser_tiocmset(struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear)
2948{
Jesper Juhl56e139f2006-06-25 05:47:52 -07002949 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 unsigned long flags;
2951
2952
2953 if (tty->index == MXSER_PORTS)
2954 return -ENOIOCTLCMD;
2955 if (tty->flags & (1 << TTY_IO_ERROR))
2956 return -EIO;
2957
2958 spin_lock_irqsave(&info->slock, flags);
2959
2960 if (set & TIOCM_RTS)
2961 info->MCR |= UART_MCR_RTS;
2962 if (set & TIOCM_DTR)
2963 info->MCR |= UART_MCR_DTR;
2964
2965 if (clear & TIOCM_RTS)
2966 info->MCR &= ~UART_MCR_RTS;
2967 if (clear & TIOCM_DTR)
2968 info->MCR &= ~UART_MCR_DTR;
2969
2970 outb(info->MCR, info->base + UART_MCR);
2971 spin_unlock_irqrestore(&info->slock, flags);
2972 return 0;
2973}
2974
2975
2976static int mxser_read_register(int, unsigned short *);
2977static int mxser_program_mode(int);
2978static void mxser_normal_mode(int);
2979
2980static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2981{
2982 int id, i, bits;
2983 unsigned short regs[16], irq;
2984 unsigned char scratch, scratch2;
2985
2986 hwconf->IsMoxaMustChipFlag = MOXA_OTHER_UART;
2987
2988 id = mxser_read_register(cap, regs);
2989 if (id == C168_ASIC_ID) {
2990 hwconf->board_type = MXSER_BOARD_C168_ISA;
2991 hwconf->ports = 8;
2992 } else if (id == C104_ASIC_ID) {
2993 hwconf->board_type = MXSER_BOARD_C104_ISA;
2994 hwconf->ports = 4;
2995 } else if (id == C102_ASIC_ID) {
2996 hwconf->board_type = MXSER_BOARD_C102_ISA;
2997 hwconf->ports = 2;
2998 } else if (id == CI132_ASIC_ID) {
2999 hwconf->board_type = MXSER_BOARD_CI132;
3000 hwconf->ports = 2;
3001 } else if (id == CI134_ASIC_ID) {
3002 hwconf->board_type = MXSER_BOARD_CI134;
3003 hwconf->ports = 4;
3004 } else if (id == CI104J_ASIC_ID) {
3005 hwconf->board_type = MXSER_BOARD_CI104J;
3006 hwconf->ports = 4;
3007 } else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003008 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
3010 irq = 0;
3011 if (hwconf->ports == 2) {
3012 irq = regs[9] & 0xF000;
3013 irq = irq | (irq >> 4);
3014 if (irq != (regs[9] & 0xFF00))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003015 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 } else if (hwconf->ports == 4) {
3017 irq = regs[9] & 0xF000;
3018 irq = irq | (irq >> 4);
3019 irq = irq | (irq >> 8);
3020 if (irq != regs[9])
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003021 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 } else if (hwconf->ports == 8) {
3023 irq = regs[9] & 0xF000;
3024 irq = irq | (irq >> 4);
3025 irq = irq | (irq >> 8);
3026 if ((irq != regs[9]) || (irq != regs[10]))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003027 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
3029
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003030 if (!irq)
3031 return MXSER_ERR_IRQ;
3032 hwconf->irq = ((int)(irq & 0xF000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 for (i = 0; i < 8; i++)
3034 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003035 if ((regs[12] & 0x80) == 0)
3036 return MXSER_ERR_VECTOR;
3037 hwconf->vector = (int)regs[11]; /* interrupt vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (id == 1)
3039 hwconf->vector_mask = 0x00FF;
3040 else
3041 hwconf->vector_mask = 0x000F;
3042 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
3043 if (regs[12] & bits) {
3044 hwconf->baud_base[i] = 921600;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003045 hwconf->MaxCanSetBaudRate[i] = 921600; /* add by Victor Yu. 09-04-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 } else {
3047 hwconf->baud_base[i] = 115200;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003048 hwconf->MaxCanSetBaudRate[i] = 115200; /* add by Victor Yu. 09-04-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
3050 }
3051 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
3052 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
3053 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
3054 outb(scratch2, cap + UART_LCR);
3055 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
3056 scratch = inb(cap + UART_IIR);
3057
3058 if (scratch & 0xC0)
3059 hwconf->uart_type = PORT_16550A;
3060 else
3061 hwconf->uart_type = PORT_16450;
3062 if (id == 1)
3063 hwconf->ports = 8;
3064 else
3065 hwconf->ports = 4;
3066 request_region(hwconf->ioaddr[0], 8 * hwconf->ports, "mxser(IO)");
3067 request_region(hwconf->vector, 1, "mxser(vector)");
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003068 return hwconf->ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069}
3070
3071#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
3072#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
3073#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
3074#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
3075#define EN_CCMD 0x000 /* Chip's command register */
3076#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
3077#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
3078#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
3079#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
3080#define EN0_DCFG 0x00E /* Data configuration reg WR */
3081#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
3082#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
3083#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
3084static int mxser_read_register(int port, unsigned short *regs)
3085{
3086 int i, k, value, id;
3087 unsigned int j;
3088
3089 id = mxser_program_mode(port);
3090 if (id < 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003091 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 for (i = 0; i < 14; i++) {
3093 k = (i & 0x3F) | 0x180;
3094 for (j = 0x100; j > 0; j >>= 1) {
3095 outb(CHIP_CS, port);
3096 if (k & j) {
3097 outb(CHIP_CS | CHIP_DO, port);
3098 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
3099 } else {
3100 outb(CHIP_CS, port);
3101 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
3102 }
3103 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003104 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 value = 0;
3106 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
3107 outb(CHIP_CS, port);
3108 outb(CHIP_CS | CHIP_SK, port);
3109 if (inb(port) & CHIP_DI)
3110 value |= j;
3111 }
3112 regs[i] = value;
3113 outb(0, port);
3114 }
3115 mxser_normal_mode(port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003116 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117}
3118
3119static int mxser_program_mode(int port)
3120{
3121 int id, i, j, n;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003122 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
3124 spin_lock(&gm_lock);
3125 outb(0, port);
3126 outb(0, port);
3127 outb(0, port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003128 (void)inb(port);
3129 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 outb(0, port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003131 (void)inb(port);
3132 /* restore_flags(flags); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 spin_unlock(&gm_lock);
3134
3135 id = inb(port + 1) & 0x1F;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003136 if ((id != C168_ASIC_ID) &&
3137 (id != C104_ASIC_ID) &&
3138 (id != C102_ASIC_ID) &&
3139 (id != CI132_ASIC_ID) &&
3140 (id != CI134_ASIC_ID) &&
3141 (id != CI104J_ASIC_ID))
3142 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 for (i = 0, j = 0; i < 4; i++) {
3144 n = inb(port + 2);
3145 if (n == 'M') {
3146 j = 1;
3147 } else if ((j == 1) && (n == 1)) {
3148 j = 2;
3149 break;
3150 } else
3151 j = 0;
3152 }
3153 if (j != 2)
3154 id = -2;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003155 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
3157
3158static void mxser_normal_mode(int port)
3159{
3160 int i, n;
3161
3162 outb(0xA5, port + 1);
3163 outb(0x80, port + 3);
3164 outb(12, port + 0); /* 9600 bps */
3165 outb(0, port + 1);
3166 outb(0x03, port + 3); /* 8 data bits */
3167 outb(0x13, port + 4); /* loop back mode */
3168 for (i = 0; i < 16; i++) {
3169 n = inb(port + 5);
3170 if ((n & 0x61) == 0x60)
3171 break;
3172 if ((n & 1) == 1)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003173 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 }
3175 outb(0x00, port + 4);
3176}
3177
3178module_init(mxser_module_init);
3179module_exit(mxser_module_exit);