[PATCH] devfs: Remove devfs support from the serial subsystem
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / mxser.c
blob645d9d713aec84452615d641cfb73603d4eef2b5
1 /*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
4 * Copyright (C) 1999-2001 Moxa Technologies (support@moxa.com.tw).
6 * This code is loosely based on the Linux serial driver, written by
7 * Linus Torvalds, Theodore T'so and others.
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
12 * (at your option) any later version.
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.
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.
23 * Original release 10/26/00
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.
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
39 #include <linux/config.h>
40 #include <linux/module.h>
41 #include <linux/autoconf.h>
42 #include <linux/errno.h>
43 #include <linux/signal.h>
44 #include <linux/sched.h>
45 #include <linux/timer.h>
46 #include <linux/interrupt.h>
47 #include <linux/tty.h>
48 #include <linux/tty_flip.h>
49 #include <linux/serial.h>
50 #include <linux/serial_reg.h>
51 #include <linux/major.h>
52 #include <linux/string.h>
53 #include <linux/fcntl.h>
54 #include <linux/ptrace.h>
55 #include <linux/gfp.h>
56 #include <linux/ioport.h>
57 #include <linux/mm.h>
58 #include <linux/smp_lock.h>
59 #include <linux/delay.h>
60 #include <linux/pci.h>
62 #include <asm/system.h>
63 #include <asm/io.h>
64 #include <asm/irq.h>
65 #include <asm/bitops.h>
66 #include <asm/uaccess.h>
68 #include "mxser.h"
70 #define MXSER_VERSION "1.8"
71 #define MXSERMAJOR 174
72 #define MXSERCUMAJOR 175
74 #define MXSER_EVENT_TXLOW 1
75 #define MXSER_EVENT_HANGUP 2
77 #define MXSER_BOARDS 4 /* Max. boards */
78 #define MXSER_PORTS 32 /* Max. ports */
79 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
80 #define MXSER_ISR_PASS_LIMIT 256
82 #define MXSER_ERR_IOADDR -1
83 #define MXSER_ERR_IRQ -2
84 #define MXSER_ERR_IRQ_CONFLIT -3
85 #define MXSER_ERR_VECTOR -4
87 #define SERIAL_TYPE_NORMAL 1
88 #define SERIAL_TYPE_CALLOUT 2
90 #define WAKEUP_CHARS 256
92 #define UART_MCR_AFE 0x20
93 #define UART_LSR_SPECIAL 0x1E
95 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
96 IXON|IXOFF))
98 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
100 #define C168_ASIC_ID 1
101 #define C104_ASIC_ID 2
102 #define C102_ASIC_ID 0xB
103 #define CI132_ASIC_ID 4
104 #define CI134_ASIC_ID 3
105 #define CI104J_ASIC_ID 5
107 enum {
108 MXSER_BOARD_C168_ISA = 1,
109 MXSER_BOARD_C104_ISA,
110 MXSER_BOARD_CI104J,
111 MXSER_BOARD_C168_PCI,
112 MXSER_BOARD_C104_PCI,
113 MXSER_BOARD_C102_ISA,
114 MXSER_BOARD_CI132,
115 MXSER_BOARD_CI134,
116 MXSER_BOARD_CP132,
117 MXSER_BOARD_CP114,
118 MXSER_BOARD_CT114,
119 MXSER_BOARD_CP102,
120 MXSER_BOARD_CP104U,
121 MXSER_BOARD_CP168U,
122 MXSER_BOARD_CP132U,
123 MXSER_BOARD_CP134U,
124 MXSER_BOARD_CP104JU,
125 MXSER_BOARD_RC7000,
126 MXSER_BOARD_CP118U,
127 MXSER_BOARD_CP102UL,
128 MXSER_BOARD_CP102U,
131 static char *mxser_brdname[] = {
132 "C168 series",
133 "C104 series",
134 "CI-104J series",
135 "C168H/PCI series",
136 "C104H/PCI series",
137 "C102 series",
138 "CI-132 series",
139 "CI-134 series",
140 "CP-132 series",
141 "CP-114 series",
142 "CT-114 series",
143 "CP-102 series",
144 "CP-104U series",
145 "CP-168U series",
146 "CP-132U series",
147 "CP-134U series",
148 "CP-104JU series",
149 "Moxa UC7000 Serial",
150 "CP-118U series",
151 "CP-102UL series",
152 "CP-102U series",
155 static int mxser_numports[] = {
156 8, /* C168-ISA */
157 4, /* C104-ISA */
158 4, /* CI104J */
159 8, /* C168-PCI */
160 4, /* C104-PCI */
161 2, /* C102-ISA */
162 2, /* CI132 */
163 4, /* CI134 */
164 2, /* CP132 */
165 4, /* CP114 */
166 4, /* CT114 */
167 2, /* CP102 */
168 4, /* CP104U */
169 8, /* CP168U */
170 2, /* CP132U */
171 4, /* CP134U */
172 4, /* CP104JU */
173 8, /* RC7000 */
174 8, /* CP118U */
175 2, /* CP102UL */
176 2, /* CP102U */
179 #define UART_TYPE_NUM 2
181 static const unsigned int Gmoxa_uart_id[UART_TYPE_NUM] = {
182 MOXA_MUST_MU150_HWID,
183 MOXA_MUST_MU860_HWID
186 /* This is only for PCI */
187 #define UART_INFO_NUM 3
188 struct mxpciuart_info {
189 int type;
190 int tx_fifo;
191 int rx_fifo;
192 int xmit_fifo_size;
193 int rx_high_water;
194 int rx_trigger;
195 int rx_low_water;
196 long max_baud;
199 static const struct mxpciuart_info Gpci_uart_info[UART_INFO_NUM] = {
200 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
201 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
202 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
206 #ifdef CONFIG_PCI
208 static struct pci_device_id mxser_pcibrds[] = {
209 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_C168_PCI},
210 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_C104_PCI},
211 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP132},
212 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP114},
213 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CT114},
214 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102},
215 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP104U},
216 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP168U},
217 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP132U},
218 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP134U},
219 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP104JU},
220 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_RC7000},
221 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP118U},
222 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102UL},
223 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102U},
227 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
230 #endif
232 typedef struct _moxa_pci_info {
233 unsigned short busNum;
234 unsigned short devNum;
235 struct pci_dev *pdev; /* add by Victor Yu. 06-23-2003 */
236 } moxa_pci_info;
238 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
239 static int ttymajor = MXSERMAJOR;
240 static int calloutmajor = MXSERCUMAJOR;
241 static int verbose = 0;
243 /* Variables for insmod */
245 MODULE_AUTHOR("Casper Yang");
246 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
247 module_param_array(ioaddr, int, NULL, 0);
248 module_param(ttymajor, int, 0);
249 module_param(calloutmajor, int, 0);
250 module_param(verbose, bool, 0);
251 MODULE_LICENSE("GPL");
253 struct mxser_log {
254 int tick;
255 unsigned long rxcnt[MXSER_PORTS];
256 unsigned long txcnt[MXSER_PORTS];
260 struct mxser_mon {
261 unsigned long rxcnt;
262 unsigned long txcnt;
263 unsigned long up_rxcnt;
264 unsigned long up_txcnt;
265 int modem_status;
266 unsigned char hold_reason;
269 struct mxser_mon_ext {
270 unsigned long rx_cnt[32];
271 unsigned long tx_cnt[32];
272 unsigned long up_rxcnt[32];
273 unsigned long up_txcnt[32];
274 int modem_status[32];
276 long baudrate[32];
277 int databits[32];
278 int stopbits[32];
279 int parity[32];
280 int flowctrl[32];
281 int fifo[32];
282 int iftype[32];
285 struct mxser_hwconf {
286 int board_type;
287 int ports;
288 int irq;
289 int vector;
290 int vector_mask;
291 int uart_type;
292 int ioaddr[MXSER_PORTS_PER_BOARD];
293 int baud_base[MXSER_PORTS_PER_BOARD];
294 moxa_pci_info pciInfo;
295 int IsMoxaMustChipFlag; /* add by Victor Yu. 08-30-2002 */
296 int MaxCanSetBaudRate[MXSER_PORTS_PER_BOARD]; /* add by Victor Yu. 09-04-2002 */
297 int opmode_ioaddr[MXSER_PORTS_PER_BOARD]; /* add by Victor Yu. 01-05-2004 */
300 struct mxser_struct {
301 int port;
302 int base; /* port base address */
303 int irq; /* port using irq no. */
304 int vector; /* port irq vector */
305 int vectormask; /* port vector mask */
306 int rx_high_water;
307 int rx_trigger; /* Rx fifo trigger level */
308 int rx_low_water;
309 int baud_base; /* max. speed */
310 int flags; /* defined in tty.h */
311 int type; /* UART type */
312 struct tty_struct *tty;
313 int read_status_mask;
314 int ignore_status_mask;
315 int xmit_fifo_size;
316 int custom_divisor;
317 int x_char; /* xon/xoff character */
318 int close_delay;
319 unsigned short closing_wait;
320 int IER; /* Interrupt Enable Register */
321 int MCR; /* Modem control register */
322 unsigned long event;
323 int count; /* # of fd on device */
324 int blocked_open; /* # of blocked opens */
325 long session; /* Session of opening process */
326 long pgrp; /* pgrp of opening process */
327 unsigned char *xmit_buf;
328 int xmit_head;
329 int xmit_tail;
330 int xmit_cnt;
331 struct work_struct tqueue;
332 struct termios normal_termios;
333 struct termios callout_termios;
334 wait_queue_head_t open_wait;
335 wait_queue_head_t close_wait;
336 wait_queue_head_t delta_msr_wait;
337 struct async_icount icount; /* kernel counters for the 4 input interrupts */
338 int timeout;
339 int IsMoxaMustChipFlag; /* add by Victor Yu. 08-30-2002 */
340 int MaxCanSetBaudRate; /* add by Victor Yu. 09-04-2002 */
341 int opmode_ioaddr; /* add by Victor Yu. 01-05-2004 */
342 unsigned char stop_rx;
343 unsigned char ldisc_stop_rx;
344 long realbaud;
345 struct mxser_mon mon_data;
346 unsigned char err_shadow;
347 spinlock_t slock;
350 struct mxser_mstatus {
351 tcflag_t cflag;
352 int cts;
353 int dsr;
354 int ri;
355 int dcd;
358 static struct mxser_mstatus GMStatus[MXSER_PORTS];
360 static int mxserBoardCAP[MXSER_BOARDS] = {
361 0, 0, 0, 0
362 /* 0x180, 0x280, 0x200, 0x320 */
365 static struct tty_driver *mxvar_sdriver;
366 static struct mxser_struct mxvar_table[MXSER_PORTS];
367 static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
368 static struct termios *mxvar_termios[MXSER_PORTS + 1];
369 static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
370 static struct mxser_log mxvar_log;
371 static int mxvar_diagflag;
372 static unsigned char mxser_msr[MXSER_PORTS + 1];
373 static struct mxser_mon_ext mon_data_ext;
374 static int mxser_set_baud_method[MXSER_PORTS + 1];
375 static spinlock_t gm_lock;
378 * This is used to figure out the divisor speeds and the timeouts
381 static struct mxser_hwconf mxsercfg[MXSER_BOARDS];
384 * static functions:
387 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
388 static int mxser_init(void);
390 /* static void mxser_poll(unsigned long); */
391 static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
392 static int mxser_get_PCI_conf(int, int, int, struct mxser_hwconf *);
393 static void mxser_do_softint(void *);
394 static int mxser_open(struct tty_struct *, struct file *);
395 static void mxser_close(struct tty_struct *, struct file *);
396 static int mxser_write(struct tty_struct *, const unsigned char *, int);
397 static int mxser_write_room(struct tty_struct *);
398 static void mxser_flush_buffer(struct tty_struct *);
399 static int mxser_chars_in_buffer(struct tty_struct *);
400 static void mxser_flush_chars(struct tty_struct *);
401 static void mxser_put_char(struct tty_struct *, unsigned char);
402 static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
403 static int mxser_ioctl_special(unsigned int, void __user *);
404 static void mxser_throttle(struct tty_struct *);
405 static void mxser_unthrottle(struct tty_struct *);
406 static void mxser_set_termios(struct tty_struct *, struct termios *);
407 static void mxser_stop(struct tty_struct *);
408 static void mxser_start(struct tty_struct *);
409 static void mxser_hangup(struct tty_struct *);
410 static void mxser_rs_break(struct tty_struct *, int);
411 static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
412 static void mxser_receive_chars(struct mxser_struct *, int *);
413 static void mxser_transmit_chars(struct mxser_struct *);
414 static void mxser_check_modem_status(struct mxser_struct *, int);
415 static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
416 static int mxser_startup(struct mxser_struct *);
417 static void mxser_shutdown(struct mxser_struct *);
418 static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
419 static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct __user *);
420 static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct __user *);
421 static int mxser_get_lsr_info(struct mxser_struct *, unsigned int __user *);
422 static void mxser_send_break(struct mxser_struct *, int);
423 static int mxser_tiocmget(struct tty_struct *, struct file *);
424 static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
425 static int mxser_set_baud(struct mxser_struct *info, long newspd);
426 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout);
428 static void mxser_startrx(struct tty_struct *tty);
429 static void mxser_stoprx(struct tty_struct *tty);
432 static int CheckIsMoxaMust(int io)
434 u8 oldmcr, hwid;
435 int i;
437 outb(0, io + UART_LCR);
438 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
439 oldmcr = inb(io + UART_MCR);
440 outb(0, io + UART_MCR);
441 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
442 if ((hwid = inb(io + UART_MCR)) != 0) {
443 outb(oldmcr, io + UART_MCR);
444 return MOXA_OTHER_UART;
447 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
448 for (i = 0; i < UART_TYPE_NUM; i++) {
449 if (hwid == Gmoxa_uart_id[i])
450 return (int)hwid;
452 return MOXA_OTHER_UART;
455 /* above is modified by Victor Yu. 08-15-2002 */
457 static struct tty_operations mxser_ops = {
458 .open = mxser_open,
459 .close = mxser_close,
460 .write = mxser_write,
461 .put_char = mxser_put_char,
462 .flush_chars = mxser_flush_chars,
463 .write_room = mxser_write_room,
464 .chars_in_buffer = mxser_chars_in_buffer,
465 .flush_buffer = mxser_flush_buffer,
466 .ioctl = mxser_ioctl,
467 .throttle = mxser_throttle,
468 .unthrottle = mxser_unthrottle,
469 .set_termios = mxser_set_termios,
470 .stop = mxser_stop,
471 .start = mxser_start,
472 .hangup = mxser_hangup,
473 .break_ctl = mxser_rs_break,
474 .wait_until_sent = mxser_wait_until_sent,
475 .tiocmget = mxser_tiocmget,
476 .tiocmset = mxser_tiocmset,
480 * The MOXA Smartio/Industio serial driver boot-time initialization code!
483 static int __init mxser_module_init(void)
485 int ret;
487 if (verbose)
488 printk(KERN_DEBUG "Loading module mxser ...\n");
489 ret = mxser_init();
490 if (verbose)
491 printk(KERN_DEBUG "Done.\n");
492 return ret;
495 static void __exit mxser_module_exit(void)
497 int i, err;
499 if (verbose)
500 printk(KERN_DEBUG "Unloading module mxser ...\n");
502 err = tty_unregister_driver(mxvar_sdriver);
503 if (!err)
504 put_tty_driver(mxvar_sdriver);
505 else
506 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
508 for (i = 0; i < MXSER_BOARDS; i++) {
509 struct pci_dev *pdev;
511 if (mxsercfg[i].board_type == -1)
512 continue;
513 else {
514 pdev = mxsercfg[i].pciInfo.pdev;
515 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
516 if (pdev != NULL) { /* PCI */
517 release_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2));
518 release_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3));
519 } else {
520 release_region(mxsercfg[i].ioaddr[0], 8 * mxsercfg[i].ports);
521 release_region(mxsercfg[i].vector, 1);
525 if (verbose)
526 printk(KERN_DEBUG "Done.\n");
529 static void process_txrx_fifo(struct mxser_struct *info)
531 int i;
533 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
534 info->rx_trigger = 1;
535 info->rx_high_water = 1;
536 info->rx_low_water = 1;
537 info->xmit_fifo_size = 1;
538 } else {
539 for (i = 0; i < UART_INFO_NUM; i++) {
540 if (info->IsMoxaMustChipFlag == Gpci_uart_info[i].type) {
541 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
542 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
543 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
544 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
545 break;
551 static int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
553 struct mxser_struct *info;
554 int retval;
555 int i, n;
557 n = board * MXSER_PORTS_PER_BOARD;
558 info = &mxvar_table[n];
559 /*if (verbose) */ {
560 printk(KERN_DEBUG " ttyM%d - ttyM%d ",
561 n, n + hwconf->ports - 1);
562 printk(" max. baud rate = %d bps.\n",
563 hwconf->MaxCanSetBaudRate[0]);
566 for (i = 0; i < hwconf->ports; i++, n++, info++) {
567 info->port = n;
568 info->base = hwconf->ioaddr[i];
569 info->irq = hwconf->irq;
570 info->vector = hwconf->vector;
571 info->vectormask = hwconf->vector_mask;
572 info->opmode_ioaddr = hwconf->opmode_ioaddr[i]; /* add by Victor Yu. 01-05-2004 */
573 info->stop_rx = 0;
574 info->ldisc_stop_rx = 0;
576 info->IsMoxaMustChipFlag = hwconf->IsMoxaMustChipFlag;
577 /* Enhance mode enabled here */
578 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
579 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->base);
582 info->flags = ASYNC_SHARE_IRQ;
583 info->type = hwconf->uart_type;
584 info->baud_base = hwconf->baud_base[i];
586 info->MaxCanSetBaudRate = hwconf->MaxCanSetBaudRate[i];
588 process_txrx_fifo(info);
591 info->custom_divisor = hwconf->baud_base[i] * 16;
592 info->close_delay = 5 * HZ / 10;
593 info->closing_wait = 30 * HZ;
594 INIT_WORK(&info->tqueue, mxser_do_softint, info);
595 info->normal_termios = mxvar_sdriver->init_termios;
596 init_waitqueue_head(&info->open_wait);
597 init_waitqueue_head(&info->close_wait);
598 init_waitqueue_head(&info->delta_msr_wait);
599 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
600 info->err_shadow = 0;
601 spin_lock_init(&info->slock);
604 * Allocate the IRQ if necessary
608 /* before set INT ISR, disable all int */
609 for (i = 0; i < hwconf->ports; i++) {
610 outb(inb(hwconf->ioaddr[i] + UART_IER) & 0xf0,
611 hwconf->ioaddr[i] + UART_IER);
614 n = board * MXSER_PORTS_PER_BOARD;
615 info = &mxvar_table[n];
617 retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
618 "mxser", info);
619 if (retval) {
620 printk(KERN_ERR "Board %d: %s",
621 board, mxser_brdname[hwconf->board_type - 1]);
622 printk(" Request irq failed, IRQ (%d) may conflict with"
623 " another device.\n", info->irq);
624 return retval;
626 return 0;
629 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
631 mxsercfg[board] = *hwconf;
634 #ifdef CONFIG_PCI
635 static int mxser_get_PCI_conf(int busnum, int devnum, int board_type, struct mxser_hwconf *hwconf)
637 int i, j;
638 /* unsigned int val; */
639 unsigned int ioaddress;
640 struct pci_dev *pdev = hwconf->pciInfo.pdev;
642 /* io address */
643 hwconf->board_type = board_type;
644 hwconf->ports = mxser_numports[board_type - 1];
645 ioaddress = pci_resource_start(pdev, 2);
646 request_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2),
647 "mxser(IO)");
649 for (i = 0; i < hwconf->ports; i++)
650 hwconf->ioaddr[i] = ioaddress + 8 * i;
652 /* vector */
653 ioaddress = pci_resource_start(pdev, 3);
654 request_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3),
655 "mxser(vector)");
656 hwconf->vector = ioaddress;
658 /* irq */
659 hwconf->irq = hwconf->pciInfo.pdev->irq;
661 hwconf->IsMoxaMustChipFlag = CheckIsMoxaMust(hwconf->ioaddr[0]);
662 hwconf->uart_type = PORT_16550A;
663 hwconf->vector_mask = 0;
666 for (i = 0; i < hwconf->ports; i++) {
667 for (j = 0; j < UART_INFO_NUM; j++) {
668 if (Gpci_uart_info[j].type == hwconf->IsMoxaMustChipFlag) {
669 hwconf->MaxCanSetBaudRate[i] = Gpci_uart_info[j].max_baud;
671 /* exception....CP-102 */
672 if (board_type == MXSER_BOARD_CP102)
673 hwconf->MaxCanSetBaudRate[i] = 921600;
674 break;
679 if (hwconf->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID) {
680 for (i = 0; i < hwconf->ports; i++) {
681 if (i < 4)
682 hwconf->opmode_ioaddr[i] = ioaddress + 4;
683 else
684 hwconf->opmode_ioaddr[i] = ioaddress + 0x0c;
686 outb(0, ioaddress + 4); /* default set to RS232 mode */
687 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
690 for (i = 0; i < hwconf->ports; i++) {
691 hwconf->vector_mask |= (1 << i);
692 hwconf->baud_base[i] = 921600;
694 return 0;
696 #endif
698 static int mxser_init(void)
700 int i, m, retval, b, n;
701 struct pci_dev *pdev = NULL;
702 int index;
703 unsigned char busnum, devnum;
704 struct mxser_hwconf hwconf;
706 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
707 if (!mxvar_sdriver)
708 return -ENOMEM;
709 spin_lock_init(&gm_lock);
711 for (i = 0; i < MXSER_BOARDS; i++) {
712 mxsercfg[i].board_type = -1;
715 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
716 MXSER_VERSION);
718 /* Initialize the tty_driver structure */
719 memset(mxvar_sdriver, 0, sizeof(struct tty_driver));
720 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
721 mxvar_sdriver->name = "ttyM";
722 mxvar_sdriver->major = ttymajor;
723 mxvar_sdriver->minor_start = 0;
724 mxvar_sdriver->num = MXSER_PORTS + 1;
725 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
726 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
727 mxvar_sdriver->init_termios = tty_std_termios;
728 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
729 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
730 tty_set_operations(mxvar_sdriver, &mxser_ops);
731 mxvar_sdriver->ttys = mxvar_tty;
732 mxvar_sdriver->termios = mxvar_termios;
733 mxvar_sdriver->termios_locked = mxvar_termios_locked;
735 mxvar_diagflag = 0;
736 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
737 memset(&mxvar_log, 0, sizeof(struct mxser_log));
739 memset(&mxser_msr, 0, sizeof(unsigned char) * (MXSER_PORTS + 1));
740 memset(&mon_data_ext, 0, sizeof(struct mxser_mon_ext));
741 memset(&mxser_set_baud_method, 0, sizeof(int) * (MXSER_PORTS + 1));
742 memset(&hwconf, 0, sizeof(struct mxser_hwconf));
744 m = 0;
745 /* Start finding ISA boards here */
746 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
747 int cap;
749 if (!(cap = mxserBoardCAP[b]))
750 continue;
752 retval = mxser_get_ISA_conf(cap, &hwconf);
754 if (retval != 0)
755 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
756 mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
758 if (retval <= 0) {
759 if (retval == MXSER_ERR_IRQ)
760 printk(KERN_ERR "Invalid interrupt number, "
761 "board not configured\n");
762 else if (retval == MXSER_ERR_IRQ_CONFLIT)
763 printk(KERN_ERR "Invalid interrupt number, "
764 "board not configured\n");
765 else if (retval == MXSER_ERR_VECTOR)
766 printk(KERN_ERR "Invalid interrupt vector, "
767 "board not configured\n");
768 else if (retval == MXSER_ERR_IOADDR)
769 printk(KERN_ERR "Invalid I/O address, "
770 "board not configured\n");
772 continue;
775 hwconf.pciInfo.busNum = 0;
776 hwconf.pciInfo.devNum = 0;
777 hwconf.pciInfo.pdev = NULL;
779 mxser_getcfg(m, &hwconf);
781 * init mxsercfg first,
782 * or mxsercfg data is not correct on ISR.
784 /* mxser_initbrd will hook ISR. */
785 if (mxser_initbrd(m, &hwconf) < 0)
786 continue;
788 m++;
791 /* Start finding ISA boards from module arg */
792 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
793 int cap;
795 if (!(cap = ioaddr[b]))
796 continue;
798 retval = mxser_get_ISA_conf(cap, &hwconf);
800 if (retval != 0)
801 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
802 mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
804 if (retval <= 0) {
805 if (retval == MXSER_ERR_IRQ)
806 printk(KERN_ERR "Invalid interrupt number, "
807 "board not configured\n");
808 else if (retval == MXSER_ERR_IRQ_CONFLIT)
809 printk(KERN_ERR "Invalid interrupt number, "
810 "board not configured\n");
811 else if (retval == MXSER_ERR_VECTOR)
812 printk(KERN_ERR "Invalid interrupt vector, "
813 "board not configured\n");
814 else if (retval == MXSER_ERR_IOADDR)
815 printk(KERN_ERR "Invalid I/O address, "
816 "board not configured\n");
818 continue;
821 hwconf.pciInfo.busNum = 0;
822 hwconf.pciInfo.devNum = 0;
823 hwconf.pciInfo.pdev = NULL;
825 mxser_getcfg(m, &hwconf);
827 * init mxsercfg first,
828 * or mxsercfg data is not correct on ISR.
830 /* mxser_initbrd will hook ISR. */
831 if (mxser_initbrd(m, &hwconf) < 0)
832 continue;
834 m++;
837 /* start finding PCI board here */
838 #ifdef CONFIG_PCI
839 n = ARRAY_SIZE(mxser_pcibrds) - 1;
840 index = 0;
841 b = 0;
842 while (b < n) {
843 pdev = pci_find_device(mxser_pcibrds[b].vendor,
844 mxser_pcibrds[b].device, pdev);
845 if (pdev == NULL) {
846 b++;
847 continue;
849 hwconf.pciInfo.busNum = busnum = pdev->bus->number;
850 hwconf.pciInfo.devNum = devnum = PCI_SLOT(pdev->devfn) << 3;
851 hwconf.pciInfo.pdev = pdev;
852 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
853 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
854 busnum, devnum >> 3);
855 index++;
856 if (m >= MXSER_BOARDS)
857 printk(KERN_ERR
858 "Too many Smartio/Industio family boards find "
859 "(maximum %d), board not configured\n",
860 MXSER_BOARDS);
861 else {
862 if (pci_enable_device(pdev)) {
863 printk(KERN_ERR "Moxa SmartI/O PCI enable "
864 "fail !\n");
865 continue;
867 retval = mxser_get_PCI_conf(busnum, devnum,
868 (int)mxser_pcibrds[b].driver_data,
869 &hwconf);
870 if (retval < 0) {
871 if (retval == MXSER_ERR_IRQ)
872 printk(KERN_ERR
873 "Invalid interrupt number, "
874 "board not configured\n");
875 else if (retval == MXSER_ERR_IRQ_CONFLIT)
876 printk(KERN_ERR
877 "Invalid interrupt number, "
878 "board not configured\n");
879 else if (retval == MXSER_ERR_VECTOR)
880 printk(KERN_ERR
881 "Invalid interrupt vector, "
882 "board not configured\n");
883 else if (retval == MXSER_ERR_IOADDR)
884 printk(KERN_ERR
885 "Invalid I/O address, "
886 "board not configured\n");
887 continue;
889 mxser_getcfg(m, &hwconf);
890 /* init mxsercfg first,
891 * or mxsercfg data is not correct on ISR.
893 /* mxser_initbrd will hook ISR. */
894 if (mxser_initbrd(m, &hwconf) < 0)
895 continue;
896 m++;
899 #endif
901 retval = tty_register_driver(mxvar_sdriver);
902 if (retval) {
903 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
904 " driver !\n");
905 put_tty_driver(mxvar_sdriver);
907 for (i = 0; i < MXSER_BOARDS; i++) {
908 if (mxsercfg[i].board_type == -1)
909 continue;
910 else {
911 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
912 /* todo: release io, vector */
915 return retval;
918 return 0;
921 static void mxser_do_softint(void *private_)
923 struct mxser_struct *info = private_;
924 struct tty_struct *tty;
926 tty = info->tty;
928 if (tty) {
929 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
930 tty_wakeup(tty);
931 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
932 tty_hangup(tty);
936 static unsigned char mxser_get_msr(int baseaddr, int mode, int port, struct mxser_struct *info)
938 unsigned char status = 0;
940 status = inb(baseaddr + UART_MSR);
942 mxser_msr[port] &= 0x0F;
943 mxser_msr[port] |= status;
944 status = mxser_msr[port];
945 if (mode)
946 mxser_msr[port] = 0;
948 return status;
952 * This routine is called whenever a serial port is opened. It
953 * enables interrupts for a serial port, linking in its async structure into
954 * the IRQ chain. It also performs the serial-specific
955 * initialization for the tty structure.
957 static int mxser_open(struct tty_struct *tty, struct file *filp)
959 struct mxser_struct *info;
960 int retval, line;
962 /* initialize driver_data in case something fails */
963 tty->driver_data = NULL;
965 line = tty->index;
966 if (line == MXSER_PORTS)
967 return 0;
968 if (line < 0 || line > MXSER_PORTS)
969 return -ENODEV;
970 info = mxvar_table + line;
971 if (!info->base)
972 return -ENODEV;
974 tty->driver_data = info;
975 info->tty = tty;
977 * Start up serial port
979 retval = mxser_startup(info);
980 if (retval)
981 return retval;
983 retval = mxser_block_til_ready(tty, filp, info);
984 if (retval)
985 return retval;
987 info->count++;
989 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
990 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
991 *tty->termios = info->normal_termios;
992 else
993 *tty->termios = info->callout_termios;
994 mxser_change_speed(info, NULL);
997 info->session = current->signal->session;
998 info->pgrp = process_group(current);
999 clear_bit(TTY_DONT_FLIP, &tty->flags);
1002 status = mxser_get_msr(info->base, 0, info->port);
1003 mxser_check_modem_status(info, status);
1006 /* unmark here for very high baud rate (ex. 921600 bps) used */
1007 tty->low_latency = 1;
1008 return 0;
1012 * This routine is called when the serial port gets closed. First, we
1013 * wait for the last remaining data to be sent. Then, we unlink its
1014 * async structure from the interrupt chain if necessary, and we free
1015 * that IRQ if nothing is left in the chain.
1017 static void mxser_close(struct tty_struct *tty, struct file *filp)
1019 struct mxser_struct *info = tty->driver_data;
1021 unsigned long timeout;
1022 unsigned long flags;
1023 struct tty_ldisc *ld;
1025 if (tty->index == MXSER_PORTS)
1026 return;
1027 if (!info)
1028 return;
1030 spin_lock_irqsave(&info->slock, flags);
1032 if (tty_hung_up_p(filp)) {
1033 spin_unlock_irqrestore(&info->slock, flags);
1034 return;
1036 if ((tty->count == 1) && (info->count != 1)) {
1038 * Uh, oh. tty->count is 1, which means that the tty
1039 * structure will be freed. Info->count should always
1040 * be one in these conditions. If it's greater than
1041 * one, we've got real problems, since it means the
1042 * serial port won't be shutdown.
1044 printk(KERN_ERR "mxser_close: bad serial port count; "
1045 "tty->count is 1, info->count is %d\n", info->count);
1046 info->count = 1;
1048 if (--info->count < 0) {
1049 printk(KERN_ERR "mxser_close: bad serial port count for "
1050 "ttys%d: %d\n", info->port, info->count);
1051 info->count = 0;
1053 if (info->count) {
1054 spin_unlock_irqrestore(&info->slock, flags);
1055 return;
1057 info->flags |= ASYNC_CLOSING;
1058 spin_unlock_irqrestore(&info->slock, flags);
1060 * Save the termios structure, since this port may have
1061 * separate termios for callout and dialin.
1063 if (info->flags & ASYNC_NORMAL_ACTIVE)
1064 info->normal_termios = *tty->termios;
1066 * Now we wait for the transmit buffer to clear; and we notify
1067 * the line discipline to only process XON/XOFF characters.
1069 tty->closing = 1;
1070 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1071 tty_wait_until_sent(tty, info->closing_wait);
1073 * At this point we stop accepting input. To do this, we
1074 * disable the receive line status interrupts, and tell the
1075 * interrupt driver to stop checking the data ready bit in the
1076 * line status register.
1078 info->IER &= ~UART_IER_RLSI;
1079 if (info->IsMoxaMustChipFlag)
1080 info->IER &= ~MOXA_MUST_RECV_ISR;
1081 /* by William
1082 info->read_status_mask &= ~UART_LSR_DR;
1084 if (info->flags & ASYNC_INITIALIZED) {
1085 outb(info->IER, info->base + UART_IER);
1087 * Before we drop DTR, make sure the UART transmitter
1088 * has completely drained; this is especially
1089 * important if there is a transmit FIFO!
1091 timeout = jiffies + HZ;
1092 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
1093 schedule_timeout_interruptible(5);
1094 if (time_after(jiffies, timeout))
1095 break;
1098 mxser_shutdown(info);
1100 if (tty->driver->flush_buffer)
1101 tty->driver->flush_buffer(tty);
1103 ld = tty_ldisc_ref(tty);
1104 if (ld) {
1105 if (ld->flush_buffer)
1106 ld->flush_buffer(tty);
1107 tty_ldisc_deref(ld);
1110 tty->closing = 0;
1111 info->event = 0;
1112 info->tty = NULL;
1113 if (info->blocked_open) {
1114 if (info->close_delay)
1115 schedule_timeout_interruptible(info->close_delay);
1116 wake_up_interruptible(&info->open_wait);
1119 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1120 wake_up_interruptible(&info->close_wait);
1124 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1126 int c, total = 0;
1127 struct mxser_struct *info = tty->driver_data;
1128 unsigned long flags;
1130 if (!info->xmit_buf)
1131 return 0;
1133 while (1) {
1134 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1135 SERIAL_XMIT_SIZE - info->xmit_head));
1136 if (c <= 0)
1137 break;
1139 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1140 spin_lock_irqsave(&info->slock, flags);
1141 info->xmit_head = (info->xmit_head + c) &
1142 (SERIAL_XMIT_SIZE - 1);
1143 info->xmit_cnt += c;
1144 spin_unlock_irqrestore(&info->slock, flags);
1146 buf += c;
1147 count -= c;
1148 total += c;
1151 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1152 if (!tty->hw_stopped ||
1153 (info->type == PORT_16550A) ||
1154 (info->IsMoxaMustChipFlag)) {
1155 spin_lock_irqsave(&info->slock, flags);
1156 info->IER |= UART_IER_THRI;
1157 outb(info->IER, info->base + UART_IER);
1158 spin_unlock_irqrestore(&info->slock, flags);
1161 return total;
1164 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1166 struct mxser_struct *info = tty->driver_data;
1167 unsigned long flags;
1169 if (!info->xmit_buf)
1170 return;
1172 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1173 return;
1175 spin_lock_irqsave(&info->slock, flags);
1176 info->xmit_buf[info->xmit_head++] = ch;
1177 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1178 info->xmit_cnt++;
1179 spin_unlock_irqrestore(&info->slock, flags);
1180 if (!tty->stopped && !(info->IER & UART_IER_THRI)) {
1181 if (!tty->hw_stopped ||
1182 (info->type == PORT_16550A) ||
1183 info->IsMoxaMustChipFlag) {
1184 spin_lock_irqsave(&info->slock, flags);
1185 info->IER |= UART_IER_THRI;
1186 outb(info->IER, info->base + UART_IER);
1187 spin_unlock_irqrestore(&info->slock, flags);
1193 static void mxser_flush_chars(struct tty_struct *tty)
1195 struct mxser_struct *info = tty->driver_data;
1196 unsigned long flags;
1198 if (info->xmit_cnt <= 0 ||
1199 tty->stopped ||
1200 !info->xmit_buf ||
1201 (tty->hw_stopped &&
1202 (info->type != PORT_16550A) &&
1203 (!info->IsMoxaMustChipFlag)
1205 return;
1207 spin_lock_irqsave(&info->slock, flags);
1209 info->IER |= UART_IER_THRI;
1210 outb(info->IER, info->base + UART_IER);
1212 spin_unlock_irqrestore(&info->slock, flags);
1215 static int mxser_write_room(struct tty_struct *tty)
1217 struct mxser_struct *info = tty->driver_data;
1218 int ret;
1220 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1221 if (ret < 0)
1222 ret = 0;
1223 return ret;
1226 static int mxser_chars_in_buffer(struct tty_struct *tty)
1228 struct mxser_struct *info = tty->driver_data;
1229 return info->xmit_cnt;
1232 static void mxser_flush_buffer(struct tty_struct *tty)
1234 struct mxser_struct *info = tty->driver_data;
1235 char fcr;
1236 unsigned long flags;
1239 spin_lock_irqsave(&info->slock, flags);
1240 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1242 /* below added by shinhay */
1243 fcr = inb(info->base + UART_FCR);
1244 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1245 info->base + UART_FCR);
1246 outb(fcr, info->base + UART_FCR);
1248 spin_unlock_irqrestore(&info->slock, flags);
1249 /* above added by shinhay */
1251 wake_up_interruptible(&tty->write_wait);
1252 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1253 (tty->ldisc.write_wakeup) (tty);
1256 static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1258 struct mxser_struct *info = tty->driver_data;
1259 int retval;
1260 struct async_icount cprev, cnow; /* kernel counter temps */
1261 struct serial_icounter_struct __user *p_cuser;
1262 unsigned long templ;
1263 unsigned long flags;
1264 void __user *argp = (void __user *)arg;
1266 if (tty->index == MXSER_PORTS)
1267 return mxser_ioctl_special(cmd, argp);
1269 /* following add by Victor Yu. 01-05-2004 */
1270 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1271 int opmode, p;
1272 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1273 int shiftbit;
1274 unsigned char val, mask;
1276 p = info->port % 4;
1277 if (cmd == MOXA_SET_OP_MODE) {
1278 if (get_user(opmode, (int __user *) argp))
1279 return -EFAULT;
1280 if (opmode != RS232_MODE &&
1281 opmode != RS485_2WIRE_MODE &&
1282 opmode != RS422_MODE &&
1283 opmode != RS485_4WIRE_MODE)
1284 return -EFAULT;
1285 mask = ModeMask[p];
1286 shiftbit = p * 2;
1287 val = inb(info->opmode_ioaddr);
1288 val &= mask;
1289 val |= (opmode << shiftbit);
1290 outb(val, info->opmode_ioaddr);
1291 } else {
1292 shiftbit = p * 2;
1293 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1294 opmode &= OP_MODE_MASK;
1295 if (copy_to_user(argp, &opmode, sizeof(int)))
1296 return -EFAULT;
1298 return 0;
1300 /* above add by Victor Yu. 01-05-2004 */
1302 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1303 if (tty->flags & (1 << TTY_IO_ERROR))
1304 return -EIO;
1306 switch (cmd) {
1307 case TCSBRK: /* SVID version: non-zero arg --> no break */
1308 retval = tty_check_change(tty);
1309 if (retval)
1310 return retval;
1311 tty_wait_until_sent(tty, 0);
1312 if (!arg)
1313 mxser_send_break(info, HZ / 4); /* 1/4 second */
1314 return 0;
1315 case TCSBRKP: /* support for POSIX tcsendbreak() */
1316 retval = tty_check_change(tty);
1317 if (retval)
1318 return retval;
1319 tty_wait_until_sent(tty, 0);
1320 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1321 return 0;
1322 case TIOCGSOFTCAR:
1323 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1324 case TIOCSSOFTCAR:
1325 if (get_user(templ, (unsigned long __user *) argp))
1326 return -EFAULT;
1327 arg = templ;
1328 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1329 return 0;
1330 case TIOCGSERIAL:
1331 return mxser_get_serial_info(info, argp);
1332 case TIOCSSERIAL:
1333 return mxser_set_serial_info(info, argp);
1334 case TIOCSERGETLSR: /* Get line status register */
1335 return mxser_get_lsr_info(info, argp);
1337 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1338 * - mask passed in arg for lines of interest
1339 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1340 * Caller should use TIOCGICOUNT to see which one it was
1342 case TIOCMIWAIT: {
1343 DECLARE_WAITQUEUE(wait, current);
1344 int ret;
1345 spin_lock_irqsave(&info->slock, flags);
1346 cprev = info->icount; /* note the counters on entry */
1347 spin_unlock_irqrestore(&info->slock, flags);
1349 add_wait_queue(&info->delta_msr_wait, &wait);
1350 while (1) {
1351 spin_lock_irqsave(&info->slock, flags);
1352 cnow = info->icount; /* atomic copy */
1353 spin_unlock_irqrestore(&info->slock, flags);
1355 set_current_state(TASK_INTERRUPTIBLE);
1356 if (((arg & TIOCM_RNG) &&
1357 (cnow.rng != cprev.rng)) ||
1358 ((arg & TIOCM_DSR) &&
1359 (cnow.dsr != cprev.dsr)) ||
1360 ((arg & TIOCM_CD) &&
1361 (cnow.dcd != cprev.dcd)) ||
1362 ((arg & TIOCM_CTS) &&
1363 (cnow.cts != cprev.cts))) {
1364 ret = 0;
1365 break;
1367 /* see if a signal did it */
1368 if (signal_pending(current)) {
1369 ret = -ERESTARTSYS;
1370 break;
1372 cprev = cnow;
1374 current->state = TASK_RUNNING;
1375 remove_wait_queue(&info->delta_msr_wait, &wait);
1376 break;
1378 /* NOTREACHED */
1380 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1381 * Return: write counters to the user passed counter struct
1382 * NB: both 1->0 and 0->1 transitions are counted except for
1383 * RI where only 0->1 is counted.
1385 case TIOCGICOUNT:
1386 spin_lock_irqsave(&info->slock, flags);
1387 cnow = info->icount;
1388 spin_unlock_irqrestore(&info->slock, flags);
1389 p_cuser = argp;
1390 /* modified by casper 1/11/2000 */
1391 if (put_user(cnow.frame, &p_cuser->frame))
1392 return -EFAULT;
1393 if (put_user(cnow.brk, &p_cuser->brk))
1394 return -EFAULT;
1395 if (put_user(cnow.overrun, &p_cuser->overrun))
1396 return -EFAULT;
1397 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1398 return -EFAULT;
1399 if (put_user(cnow.parity, &p_cuser->parity))
1400 return -EFAULT;
1401 if (put_user(cnow.rx, &p_cuser->rx))
1402 return -EFAULT;
1403 if (put_user(cnow.tx, &p_cuser->tx))
1404 return -EFAULT;
1405 put_user(cnow.cts, &p_cuser->cts);
1406 put_user(cnow.dsr, &p_cuser->dsr);
1407 put_user(cnow.rng, &p_cuser->rng);
1408 put_user(cnow.dcd, &p_cuser->dcd);
1409 return 0;
1410 case MOXA_HighSpeedOn:
1411 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1412 case MOXA_SDS_RSTICOUNTER: {
1413 info->mon_data.rxcnt = 0;
1414 info->mon_data.txcnt = 0;
1415 return 0;
1417 /* (above) added by James. */
1418 case MOXA_ASPP_SETBAUD:{
1419 long baud;
1420 if (get_user(baud, (long __user *)argp))
1421 return -EFAULT;
1422 mxser_set_baud(info, baud);
1423 return 0;
1425 case MOXA_ASPP_GETBAUD:
1426 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1427 return -EFAULT;
1429 return 0;
1431 case MOXA_ASPP_OQUEUE:{
1432 int len, lsr;
1434 len = mxser_chars_in_buffer(tty);
1436 lsr = inb(info->base + UART_LSR) & UART_LSR_TEMT;
1438 len += (lsr ? 0 : 1);
1440 if (copy_to_user(argp, &len, sizeof(int)))
1441 return -EFAULT;
1443 return 0;
1445 case MOXA_ASPP_MON: {
1446 int mcr, status;
1448 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1450 status = mxser_get_msr(info->base, 1, info->port, info);
1451 mxser_check_modem_status(info, status);
1453 mcr = inb(info->base + UART_MCR);
1454 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1455 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1456 else
1457 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1459 if (mcr & MOXA_MUST_MCR_TX_XON)
1460 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1461 else
1462 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1464 if (info->tty->hw_stopped)
1465 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1466 else
1467 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1469 if (copy_to_user(argp, &info->mon_data,
1470 sizeof(struct mxser_mon)))
1471 return -EFAULT;
1473 return 0;
1476 case MOXA_ASPP_LSTATUS: {
1477 if (copy_to_user(argp, &info->err_shadow,
1478 sizeof(unsigned char)))
1479 return -EFAULT;
1481 info->err_shadow = 0;
1482 return 0;
1484 case MOXA_SET_BAUD_METHOD: {
1485 int method;
1487 if (get_user(method, (int __user *)argp))
1488 return -EFAULT;
1489 mxser_set_baud_method[info->port] = method;
1490 if (copy_to_user(argp, &method, sizeof(int)))
1491 return -EFAULT;
1493 return 0;
1495 default:
1496 return -ENOIOCTLCMD;
1498 return 0;
1501 #ifndef CMSPAR
1502 #define CMSPAR 010000000000
1503 #endif
1505 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1507 int i, result, status;
1509 switch (cmd) {
1510 case MOXA_GET_CONF:
1511 if (copy_to_user(argp, mxsercfg,
1512 sizeof(struct mxser_hwconf) * 4))
1513 return -EFAULT;
1514 return 0;
1515 case MOXA_GET_MAJOR:
1516 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1517 return -EFAULT;
1518 return 0;
1520 case MOXA_GET_CUMAJOR:
1521 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1522 return -EFAULT;
1523 return 0;
1525 case MOXA_CHKPORTENABLE:
1526 result = 0;
1527 for (i = 0; i < MXSER_PORTS; i++) {
1528 if (mxvar_table[i].base)
1529 result |= (1 << i);
1531 return put_user(result, (unsigned long __user *)argp);
1532 case MOXA_GETDATACOUNT:
1533 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1534 return -EFAULT;
1535 return 0;
1536 case MOXA_GETMSTATUS:
1537 for (i = 0; i < MXSER_PORTS; i++) {
1538 GMStatus[i].ri = 0;
1539 if (!mxvar_table[i].base) {
1540 GMStatus[i].dcd = 0;
1541 GMStatus[i].dsr = 0;
1542 GMStatus[i].cts = 0;
1543 continue;
1546 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1547 GMStatus[i].cflag = mxvar_table[i].normal_termios.c_cflag;
1548 else
1549 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1551 status = inb(mxvar_table[i].base + UART_MSR);
1552 if (status & 0x80 /*UART_MSR_DCD */ )
1553 GMStatus[i].dcd = 1;
1554 else
1555 GMStatus[i].dcd = 0;
1557 if (status & 0x20 /*UART_MSR_DSR */ )
1558 GMStatus[i].dsr = 1;
1559 else
1560 GMStatus[i].dsr = 0;
1563 if (status & 0x10 /*UART_MSR_CTS */ )
1564 GMStatus[i].cts = 1;
1565 else
1566 GMStatus[i].cts = 0;
1568 if (copy_to_user(argp, GMStatus,
1569 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1570 return -EFAULT;
1571 return 0;
1572 case MOXA_ASPP_MON_EXT: {
1573 int status;
1574 int opmode, p;
1575 int shiftbit;
1576 unsigned cflag, iflag;
1578 for (i = 0; i < MXSER_PORTS; i++) {
1579 if (!mxvar_table[i].base)
1580 continue;
1582 status = mxser_get_msr(mxvar_table[i].base, 0,
1583 i, &(mxvar_table[i]));
1585 mxser_check_modem_status(&mxvar_table[i],
1586 status);
1588 if (status & UART_MSR_TERI)
1589 mxvar_table[i].icount.rng++;
1590 if (status & UART_MSR_DDSR)
1591 mxvar_table[i].icount.dsr++;
1592 if (status & UART_MSR_DDCD)
1593 mxvar_table[i].icount.dcd++;
1594 if (status & UART_MSR_DCTS)
1595 mxvar_table[i].icount.cts++;
1597 mxvar_table[i].mon_data.modem_status = status;
1598 mon_data_ext.rx_cnt[i] = mxvar_table[i].mon_data.rxcnt;
1599 mon_data_ext.tx_cnt[i] = mxvar_table[i].mon_data.txcnt;
1600 mon_data_ext.up_rxcnt[i] = mxvar_table[i].mon_data.up_rxcnt;
1601 mon_data_ext.up_txcnt[i] = mxvar_table[i].mon_data.up_txcnt;
1602 mon_data_ext.modem_status[i] = mxvar_table[i].mon_data.modem_status;
1603 mon_data_ext.baudrate[i] = mxvar_table[i].realbaud;
1605 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios) {
1606 cflag = mxvar_table[i].normal_termios.c_cflag;
1607 iflag = mxvar_table[i].normal_termios.c_iflag;
1608 } else {
1609 cflag = mxvar_table[i].tty->termios->c_cflag;
1610 iflag = mxvar_table[i].tty->termios->c_iflag;
1613 mon_data_ext.databits[i] = cflag & CSIZE;
1615 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1617 mon_data_ext.parity[i] = cflag & (PARENB | PARODD | CMSPAR);
1619 mon_data_ext.flowctrl[i] = 0x00;
1621 if (cflag & CRTSCTS)
1622 mon_data_ext.flowctrl[i] |= 0x03;
1624 if (iflag & (IXON | IXOFF))
1625 mon_data_ext.flowctrl[i] |= 0x0C;
1627 if (mxvar_table[i].type == PORT_16550A)
1628 mon_data_ext.fifo[i] = 1;
1629 else
1630 mon_data_ext.fifo[i] = 0;
1632 p = i % 4;
1633 shiftbit = p * 2;
1634 opmode = inb(mxvar_table[i].opmode_ioaddr) >> shiftbit;
1635 opmode &= OP_MODE_MASK;
1637 mon_data_ext.iftype[i] = opmode;
1640 if (copy_to_user(argp, &mon_data_ext, sizeof(struct mxser_mon_ext)))
1641 return -EFAULT;
1643 return 0;
1646 default:
1647 return -ENOIOCTLCMD;
1649 return 0;
1652 static void mxser_stoprx(struct tty_struct *tty)
1654 struct mxser_struct *info = tty->driver_data;
1655 /* unsigned long flags; */
1657 info->ldisc_stop_rx = 1;
1658 if (I_IXOFF(tty)) {
1659 /* MX_LOCK(&info->slock); */
1660 /* following add by Victor Yu. 09-02-2002 */
1661 if (info->IsMoxaMustChipFlag) {
1662 info->IER &= ~MOXA_MUST_RECV_ISR;
1663 outb(info->IER, info->base + UART_IER);
1664 } else {
1665 /* above add by Victor Yu. 09-02-2002 */
1666 info->x_char = STOP_CHAR(tty);
1667 /* mask by Victor Yu. 09-02-2002 */
1668 /* outb(info->IER, 0); */
1669 outb(0, info->base + UART_IER);
1670 info->IER |= UART_IER_THRI;
1671 /* force Tx interrupt */
1672 outb(info->IER, info->base + UART_IER);
1673 } /* add by Victor Yu. 09-02-2002 */
1674 /* MX_UNLOCK(&info->slock); */
1677 if (info->tty->termios->c_cflag & CRTSCTS) {
1678 /* MX_LOCK(&info->slock); */
1679 info->MCR &= ~UART_MCR_RTS;
1680 outb(info->MCR, info->base + UART_MCR);
1681 /* MX_UNLOCK(&info->slock); */
1685 static void mxser_startrx(struct tty_struct *tty)
1687 struct mxser_struct *info = tty->driver_data;
1688 /* unsigned long flags; */
1690 info->ldisc_stop_rx = 0;
1691 if (I_IXOFF(tty)) {
1692 if (info->x_char)
1693 info->x_char = 0;
1694 else {
1695 /* MX_LOCK(&info->slock); */
1697 /* following add by Victor Yu. 09-02-2002 */
1698 if (info->IsMoxaMustChipFlag) {
1699 info->IER |= MOXA_MUST_RECV_ISR;
1700 outb(info->IER, info->base + UART_IER);
1701 } else {
1702 /* above add by Victor Yu. 09-02-2002 */
1704 info->x_char = START_CHAR(tty);
1705 /* mask by Victor Yu. 09-02-2002 */
1706 /* outb(info->IER, 0); */
1707 /* add by Victor Yu. 09-02-2002 */
1708 outb(0, info->base + UART_IER);
1709 /* force Tx interrupt */
1710 info->IER |= UART_IER_THRI;
1711 outb(info->IER, info->base + UART_IER);
1712 } /* add by Victor Yu. 09-02-2002 */
1713 /* MX_UNLOCK(&info->slock); */
1717 if (info->tty->termios->c_cflag & CRTSCTS) {
1718 /* MX_LOCK(&info->slock); */
1719 info->MCR |= UART_MCR_RTS;
1720 outb(info->MCR, info->base + UART_MCR);
1721 /* MX_UNLOCK(&info->slock); */
1726 * This routine is called by the upper-layer tty layer to signal that
1727 * incoming characters should be throttled.
1729 static void mxser_throttle(struct tty_struct *tty)
1731 /* struct mxser_struct *info = tty->driver_data; */
1732 /* unsigned long flags; */
1734 /* MX_LOCK(&info->slock); */
1735 mxser_stoprx(tty);
1736 /* MX_UNLOCK(&info->slock); */
1739 static void mxser_unthrottle(struct tty_struct *tty)
1741 /* struct mxser_struct *info = tty->driver_data; */
1742 /* unsigned long flags; */
1744 /* MX_LOCK(&info->slock); */
1745 mxser_startrx(tty);
1746 /* MX_UNLOCK(&info->slock); */
1749 static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1751 struct mxser_struct *info = tty->driver_data;
1752 unsigned long flags;
1754 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1755 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1757 mxser_change_speed(info, old_termios);
1759 if ((old_termios->c_cflag & CRTSCTS) &&
1760 !(tty->termios->c_cflag & CRTSCTS)) {
1761 tty->hw_stopped = 0;
1762 mxser_start(tty);
1766 /* Handle sw stopped */
1767 if ((old_termios->c_iflag & IXON) &&
1768 !(tty->termios->c_iflag & IXON)) {
1769 tty->stopped = 0;
1771 /* following add by Victor Yu. 09-02-2002 */
1772 if (info->IsMoxaMustChipFlag) {
1773 spin_lock_irqsave(&info->slock, flags);
1774 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
1775 spin_unlock_irqrestore(&info->slock, flags);
1777 /* above add by Victor Yu. 09-02-2002 */
1779 mxser_start(tty);
1784 * mxser_stop() and mxser_start()
1786 * This routines are called before setting or resetting tty->stopped.
1787 * They enable or disable transmitter interrupts, as necessary.
1789 static void mxser_stop(struct tty_struct *tty)
1791 struct mxser_struct *info = tty->driver_data;
1792 unsigned long flags;
1794 spin_lock_irqsave(&info->slock, flags);
1795 if (info->IER & UART_IER_THRI) {
1796 info->IER &= ~UART_IER_THRI;
1797 outb(info->IER, info->base + UART_IER);
1799 spin_unlock_irqrestore(&info->slock, flags);
1802 static void mxser_start(struct tty_struct *tty)
1804 struct mxser_struct *info = tty->driver_data;
1805 unsigned long flags;
1807 spin_lock_irqsave(&info->slock, flags);
1808 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
1809 info->IER |= UART_IER_THRI;
1810 outb(info->IER, info->base + UART_IER);
1812 spin_unlock_irqrestore(&info->slock, flags);
1816 * mxser_wait_until_sent() --- wait until the transmitter is empty
1818 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1820 struct mxser_struct *info = tty->driver_data;
1821 unsigned long orig_jiffies, char_time;
1822 int lsr;
1824 if (info->type == PORT_UNKNOWN)
1825 return;
1827 if (info->xmit_fifo_size == 0)
1828 return; /* Just in case.... */
1830 orig_jiffies = jiffies;
1832 * Set the check interval to be 1/5 of the estimated time to
1833 * send a single character, and make it at least 1. The check
1834 * interval should also be less than the timeout.
1836 * Note: we have to use pretty tight timings here to satisfy
1837 * the NIST-PCTS.
1839 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1840 char_time = char_time / 5;
1841 if (char_time == 0)
1842 char_time = 1;
1843 if (timeout && timeout < char_time)
1844 char_time = timeout;
1846 * If the transmitter hasn't cleared in twice the approximate
1847 * amount of time to send the entire FIFO, it probably won't
1848 * ever clear. This assumes the UART isn't doing flow
1849 * control, which is currently the case. Hence, if it ever
1850 * takes longer than info->timeout, this is probably due to a
1851 * UART bug of some kind. So, we clamp the timeout parameter at
1852 * 2*info->timeout.
1854 if (!timeout || timeout > 2 * info->timeout)
1855 timeout = 2 * info->timeout;
1856 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1857 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1858 timeout, char_time);
1859 printk("jiff=%lu...", jiffies);
1860 #endif
1861 while (!((lsr = inb(info->base + UART_LSR)) & UART_LSR_TEMT)) {
1862 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1863 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1864 #endif
1865 schedule_timeout_interruptible(char_time);
1866 if (signal_pending(current))
1867 break;
1868 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1869 break;
1871 set_current_state(TASK_RUNNING);
1873 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1874 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1875 #endif
1880 * This routine is called by tty_hangup() when a hangup is signaled.
1882 void mxser_hangup(struct tty_struct *tty)
1884 struct mxser_struct *info = tty->driver_data;
1886 mxser_flush_buffer(tty);
1887 mxser_shutdown(info);
1888 info->event = 0;
1889 info->count = 0;
1890 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1891 info->tty = NULL;
1892 wake_up_interruptible(&info->open_wait);
1896 /* added by James 03-12-2004. */
1898 * mxser_rs_break() --- routine which turns the break handling on or off
1900 static void mxser_rs_break(struct tty_struct *tty, int break_state)
1902 struct mxser_struct *info = tty->driver_data;
1903 unsigned long flags;
1905 spin_lock_irqsave(&info->slock, flags);
1906 if (break_state == -1)
1907 outb(inb(info->base + UART_LCR) | UART_LCR_SBC,
1908 info->base + UART_LCR);
1909 else
1910 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC,
1911 info->base + UART_LCR);
1912 spin_unlock_irqrestore(&info->slock, flags);
1915 /* (above) added by James. */
1919 * This is the serial driver's generic interrupt routine
1921 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1923 int status, iir, i;
1924 struct mxser_struct *info;
1925 struct mxser_struct *port;
1926 int max, irqbits, bits, msr;
1927 int pass_counter = 0;
1928 int handled = IRQ_NONE;
1930 port = NULL;
1931 /* spin_lock(&gm_lock); */
1933 for (i = 0; i < MXSER_BOARDS; i++) {
1934 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1935 port = dev_id;
1936 break;
1940 if (i == MXSER_BOARDS)
1941 goto irq_stop;
1942 if (port == 0)
1943 goto irq_stop;
1944 max = mxser_numports[mxsercfg[i].board_type - 1];
1945 while (1) {
1946 irqbits = inb(port->vector) & port->vectormask;
1947 if (irqbits == port->vectormask)
1948 break;
1950 handled = IRQ_HANDLED;
1951 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1952 if (irqbits == port->vectormask)
1953 break;
1954 if (bits & irqbits)
1955 continue;
1956 info = port + i;
1958 /* following add by Victor Yu. 09-13-2002 */
1959 iir = inb(info->base + UART_IIR);
1960 if (iir & UART_IIR_NO_INT)
1961 continue;
1962 iir &= MOXA_MUST_IIR_MASK;
1963 if (!info->tty) {
1964 status = inb(info->base + UART_LSR);
1965 outb(0x27, info->base + UART_FCR);
1966 inb(info->base + UART_MSR);
1967 continue;
1969 /* above add by Victor Yu. 09-13-2002 */
1971 if (info->tty->flip.count < TTY_FLIPBUF_SIZE / 4) {
1972 info->IER |= MOXA_MUST_RECV_ISR;
1973 outb(info->IER, info->base + UART_IER);
1978 /* mask by Victor Yu. 09-13-2002
1979 if ( !info->tty ||
1980 (inb(info->base + UART_IIR) & UART_IIR_NO_INT) )
1981 continue;
1983 /* mask by Victor Yu. 09-02-2002
1984 status = inb(info->base + UART_LSR) & info->read_status_mask;
1987 /* following add by Victor Yu. 09-02-2002 */
1988 status = inb(info->base + UART_LSR);
1990 if (status & UART_LSR_PE)
1991 info->err_shadow |= NPPI_NOTIFY_PARITY;
1992 if (status & UART_LSR_FE)
1993 info->err_shadow |= NPPI_NOTIFY_FRAMING;
1994 if (status & UART_LSR_OE)
1995 info->err_shadow |= NPPI_NOTIFY_HW_OVERRUN;
1996 if (status & UART_LSR_BI)
1997 info->err_shadow |= NPPI_NOTIFY_BREAK;
1999 if (info->IsMoxaMustChipFlag) {
2001 if ( (status & 0x02) && !(status & 0x01) ) {
2002 outb(info->base+UART_FCR, 0x23);
2003 continue;
2006 if (iir == MOXA_MUST_IIR_GDA ||
2007 iir == MOXA_MUST_IIR_RDA ||
2008 iir == MOXA_MUST_IIR_RTO ||
2009 iir == MOXA_MUST_IIR_LSR)
2010 mxser_receive_chars(info, &status);
2012 } else {
2013 /* above add by Victor Yu. 09-02-2002 */
2015 status &= info->read_status_mask;
2016 if (status & UART_LSR_DR)
2017 mxser_receive_chars(info, &status);
2019 msr = inb(info->base + UART_MSR);
2020 if (msr & UART_MSR_ANY_DELTA) {
2021 mxser_check_modem_status(info, msr);
2023 /* following add by Victor Yu. 09-13-2002 */
2024 if (info->IsMoxaMustChipFlag) {
2025 if ((iir == 0x02) && (status & UART_LSR_THRE)) {
2026 mxser_transmit_chars(info);
2028 } else {
2029 /* above add by Victor Yu. 09-13-2002 */
2031 if (status & UART_LSR_THRE) {
2032 /* 8-2-99 by William
2033 if ( info->x_char || (info->xmit_cnt > 0) )
2035 mxser_transmit_chars(info);
2039 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
2040 break; /* Prevent infinite loops */
2044 irq_stop:
2045 /* spin_unlock(&gm_lock); */
2046 return handled;
2049 static void mxser_receive_chars(struct mxser_struct *info, int *status)
2051 struct tty_struct *tty = info->tty;
2052 unsigned char ch, gdl;
2053 int ignored = 0;
2054 int cnt = 0;
2055 int recv_room;
2056 int max = 256;
2057 unsigned long flags;
2059 spin_lock_irqsave(&info->slock, flags);
2061 recv_room = tty->receive_room;
2062 if ((recv_room == 0) && (!info->ldisc_stop_rx)) {
2063 /* mxser_throttle(tty); */
2064 mxser_stoprx(tty);
2065 /* return; */
2068 /* following add by Victor Yu. 09-02-2002 */
2069 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
2071 if (*status & UART_LSR_SPECIAL) {
2072 goto intr_old;
2074 /* following add by Victor Yu. 02-11-2004 */
2075 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID &&
2076 (*status & MOXA_MUST_LSR_RERR))
2077 goto intr_old;
2078 /* above add by Victor Yu. 02-14-2004 */
2079 if (*status & MOXA_MUST_LSR_RERR)
2080 goto intr_old;
2082 gdl = inb(info->base + MOXA_MUST_GDL_REGISTER);
2084 /* add by Victor Yu. 02-11-2004 */
2085 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU150_HWID)
2086 gdl &= MOXA_MUST_GDL_MASK;
2087 if (gdl >= recv_room) {
2088 if (!info->ldisc_stop_rx) {
2089 /* mxser_throttle(tty); */
2090 mxser_stoprx(tty);
2092 /* return; */
2094 while (gdl--) {
2095 ch = inb(info->base + UART_RX);
2096 tty_insert_flip_char(tty, ch, 0);
2097 cnt++;
2099 if ((cnt >= HI_WATER) && (info->stop_rx == 0)) {
2100 mxser_stoprx(tty);
2101 info->stop_rx = 1;
2102 break;
2103 } */
2105 goto end_intr;
2107 intr_old:
2108 /* above add by Victor Yu. 09-02-2002 */
2110 do {
2111 if (max-- < 0)
2112 break;
2114 if ((cnt >= HI_WATER) && (info->stop_rx == 0)) {
2115 mxser_stoprx(tty);
2116 info->stop_rx=1;
2117 break;
2121 ch = inb(info->base + UART_RX);
2122 /* following add by Victor Yu. 09-02-2002 */
2123 if (info->IsMoxaMustChipFlag && (*status & UART_LSR_OE) /*&& !(*status&UART_LSR_DR) */ )
2124 outb(0x23, info->base + UART_FCR);
2125 *status &= info->read_status_mask;
2126 /* above add by Victor Yu. 09-02-2002 */
2127 if (*status & info->ignore_status_mask) {
2128 if (++ignored > 100)
2129 break;
2130 } else {
2131 char flag = 0;
2132 if (*status & UART_LSR_SPECIAL) {
2133 if (*status & UART_LSR_BI) {
2134 flag = TTY_BREAK;
2135 /* added by casper 1/11/2000 */
2136 info->icount.brk++;
2137 /* */
2138 if (info->flags & ASYNC_SAK)
2139 do_SAK(tty);
2140 } else if (*status & UART_LSR_PE) {
2141 flag = TTY_PARITY;
2142 /* added by casper 1/11/2000 */
2143 info->icount.parity++;
2144 /* */
2145 } else if (*status & UART_LSR_FE) {
2146 flag = TTY_FRAME;
2147 /* added by casper 1/11/2000 */
2148 info->icount.frame++;
2149 /* */
2150 } else if (*status & UART_LSR_OE) {
2151 flag = TTY_OVERRUN;
2152 /* added by casper 1/11/2000 */
2153 info->icount.overrun++;
2154 /* */
2157 tty_insert_flip_char(tty, ch, flag);
2158 cnt++;
2159 if (cnt >= recv_room) {
2160 if (!info->ldisc_stop_rx) {
2161 /* mxser_throttle(tty); */
2162 mxser_stoprx(tty);
2164 break;
2169 /* following add by Victor Yu. 09-02-2002 */
2170 if (info->IsMoxaMustChipFlag)
2171 break;
2172 /* above add by Victor Yu. 09-02-2002 */
2174 /* mask by Victor Yu. 09-02-2002
2175 *status = inb(info->base + UART_LSR) & info->read_status_mask;
2177 /* following add by Victor Yu. 09-02-2002 */
2178 *status = inb(info->base + UART_LSR);
2179 /* above add by Victor Yu. 09-02-2002 */
2180 } while (*status & UART_LSR_DR);
2182 end_intr: /* add by Victor Yu. 09-02-2002 */
2183 mxvar_log.rxcnt[info->port] += cnt;
2184 info->mon_data.rxcnt += cnt;
2185 info->mon_data.up_rxcnt += cnt;
2186 spin_unlock_irqrestore(&info->slock, flags);
2188 tty_flip_buffer_push(tty);
2191 static void mxser_transmit_chars(struct mxser_struct *info)
2193 int count, cnt;
2194 unsigned long flags;
2196 spin_lock_irqsave(&info->slock, flags);
2198 if (info->x_char) {
2199 outb(info->x_char, info->base + UART_TX);
2200 info->x_char = 0;
2201 mxvar_log.txcnt[info->port]++;
2202 info->mon_data.txcnt++;
2203 info->mon_data.up_txcnt++;
2205 /* added by casper 1/11/2000 */
2206 info->icount.tx++;
2207 /* */
2208 spin_unlock_irqrestore(&info->slock, flags);
2209 return;
2212 if (info->xmit_buf == 0) {
2213 spin_unlock_irqrestore(&info->slock, flags);
2214 return;
2217 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
2218 (info->tty->hw_stopped &&
2219 (info->type != PORT_16550A) &&
2220 (!info->IsMoxaMustChipFlag))) {
2221 info->IER &= ~UART_IER_THRI;
2222 outb(info->IER, info->base + UART_IER);
2223 spin_unlock_irqrestore(&info->slock, flags);
2224 return;
2227 cnt = info->xmit_cnt;
2228 count = info->xmit_fifo_size;
2229 do {
2230 outb(info->xmit_buf[info->xmit_tail++],
2231 info->base + UART_TX);
2232 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2233 if (--info->xmit_cnt <= 0)
2234 break;
2235 } while (--count > 0);
2236 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
2238 /* added by James 03-12-2004. */
2239 info->mon_data.txcnt += (cnt - info->xmit_cnt);
2240 info->mon_data.up_txcnt += (cnt - info->xmit_cnt);
2241 /* (above) added by James. */
2243 /* added by casper 1/11/2000 */
2244 info->icount.tx += (cnt - info->xmit_cnt);
2245 /* */
2247 if (info->xmit_cnt < WAKEUP_CHARS) {
2248 set_bit(MXSER_EVENT_TXLOW, &info->event);
2249 schedule_work(&info->tqueue);
2251 if (info->xmit_cnt <= 0) {
2252 info->IER &= ~UART_IER_THRI;
2253 outb(info->IER, info->base + UART_IER);
2255 spin_unlock_irqrestore(&info->slock, flags);
2258 static void mxser_check_modem_status(struct mxser_struct *info, int status)
2260 /* update input line counters */
2261 if (status & UART_MSR_TERI)
2262 info->icount.rng++;
2263 if (status & UART_MSR_DDSR)
2264 info->icount.dsr++;
2265 if (status & UART_MSR_DDCD)
2266 info->icount.dcd++;
2267 if (status & UART_MSR_DCTS)
2268 info->icount.cts++;
2269 info->mon_data.modem_status = status;
2270 wake_up_interruptible(&info->delta_msr_wait);
2272 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
2273 if (status & UART_MSR_DCD)
2274 wake_up_interruptible(&info->open_wait);
2275 schedule_work(&info->tqueue);
2278 if (info->flags & ASYNC_CTS_FLOW) {
2279 if (info->tty->hw_stopped) {
2280 if (status & UART_MSR_CTS) {
2281 info->tty->hw_stopped = 0;
2283 if ((info->type != PORT_16550A) &&
2284 (!info->IsMoxaMustChipFlag)) {
2285 info->IER |= UART_IER_THRI;
2286 outb(info->IER, info->base + UART_IER);
2288 set_bit(MXSER_EVENT_TXLOW, &info->event);
2289 schedule_work(&info->tqueue); }
2290 } else {
2291 if (!(status & UART_MSR_CTS)) {
2292 info->tty->hw_stopped = 1;
2293 if ((info->type != PORT_16550A) &&
2294 (!info->IsMoxaMustChipFlag)) {
2295 info->IER &= ~UART_IER_THRI;
2296 outb(info->IER, info->base + UART_IER);
2303 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_struct *info)
2305 DECLARE_WAITQUEUE(wait, current);
2306 int retval;
2307 int do_clocal = 0;
2308 unsigned long flags;
2311 * If non-blocking mode is set, or the port is not enabled,
2312 * then make the check up front and then exit.
2314 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
2315 info->flags |= ASYNC_NORMAL_ACTIVE;
2316 return 0;
2319 if (tty->termios->c_cflag & CLOCAL)
2320 do_clocal = 1;
2323 * Block waiting for the carrier detect and the line to become
2324 * free (i.e., not in use by the callout). While we are in
2325 * this loop, info->count is dropped by one, so that
2326 * mxser_close() knows when to free things. We restore it upon
2327 * exit, either normal or abnormal.
2329 retval = 0;
2330 add_wait_queue(&info->open_wait, &wait);
2332 spin_lock_irqsave(&info->slock, flags);
2333 if (!tty_hung_up_p(filp))
2334 info->count--;
2335 spin_unlock_irqrestore(&info->slock, flags);
2336 info->blocked_open++;
2337 while (1) {
2338 spin_lock_irqsave(&info->slock, flags);
2339 outb(inb(info->base + UART_MCR) |
2340 UART_MCR_DTR | UART_MCR_RTS, info->base + UART_MCR);
2341 spin_unlock_irqrestore(&info->slock, flags);
2342 set_current_state(TASK_INTERRUPTIBLE);
2343 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
2344 if (info->flags & ASYNC_HUP_NOTIFY)
2345 retval = -EAGAIN;
2346 else
2347 retval = -ERESTARTSYS;
2348 break;
2350 if (!(info->flags & ASYNC_CLOSING) &&
2351 (do_clocal ||
2352 (inb(info->base + UART_MSR) & UART_MSR_DCD)))
2353 break;
2354 if (signal_pending(current)) {
2355 retval = -ERESTARTSYS;
2356 break;
2358 schedule();
2360 set_current_state(TASK_RUNNING);
2361 remove_wait_queue(&info->open_wait, &wait);
2362 if (!tty_hung_up_p(filp))
2363 info->count++;
2364 info->blocked_open--;
2365 if (retval)
2366 return retval;
2367 info->flags |= ASYNC_NORMAL_ACTIVE;
2368 return 0;
2371 static int mxser_startup(struct mxser_struct *info)
2373 unsigned long page;
2374 unsigned long flags;
2376 page = __get_free_page(GFP_KERNEL);
2377 if (!page)
2378 return -ENOMEM;
2380 spin_lock_irqsave(&info->slock, flags);
2382 if (info->flags & ASYNC_INITIALIZED) {
2383 free_page(page);
2384 spin_unlock_irqrestore(&info->slock, flags);
2385 return 0;
2388 if (!info->base || !info->type) {
2389 if (info->tty)
2390 set_bit(TTY_IO_ERROR, &info->tty->flags);
2391 free_page(page);
2392 spin_unlock_irqrestore(&info->slock, flags);
2393 return 0;
2395 if (info->xmit_buf)
2396 free_page(page);
2397 else
2398 info->xmit_buf = (unsigned char *) page;
2401 * Clear the FIFO buffers and disable them
2402 * (they will be reenabled in mxser_change_speed())
2404 if (info->IsMoxaMustChipFlag)
2405 outb((UART_FCR_CLEAR_RCVR |
2406 UART_FCR_CLEAR_XMIT |
2407 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
2408 else
2409 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2410 info->base + UART_FCR);
2413 * At this point there's no way the LSR could still be 0xFF;
2414 * if it is, then bail out, because there's likely no UART
2415 * here.
2417 if (inb(info->base + UART_LSR) == 0xff) {
2418 spin_unlock_irqrestore(&info->slock, flags);
2419 if (capable(CAP_SYS_ADMIN)) {
2420 if (info->tty)
2421 set_bit(TTY_IO_ERROR, &info->tty->flags);
2422 return 0;
2423 } else
2424 return -ENODEV;
2428 * Clear the interrupt registers.
2430 (void) inb(info->base + UART_LSR);
2431 (void) inb(info->base + UART_RX);
2432 (void) inb(info->base + UART_IIR);
2433 (void) inb(info->base + UART_MSR);
2436 * Now, initialize the UART
2438 outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
2439 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
2440 outb(info->MCR, info->base + UART_MCR);
2443 * Finally, enable interrupts
2445 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
2446 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
2448 /* following add by Victor Yu. 08-30-2002 */
2449 if (info->IsMoxaMustChipFlag)
2450 info->IER |= MOXA_MUST_IER_EGDAI;
2451 /* above add by Victor Yu. 08-30-2002 */
2452 outb(info->IER, info->base + UART_IER); /* enable interrupts */
2455 * And clear the interrupt registers again for luck.
2457 (void) inb(info->base + UART_LSR);
2458 (void) inb(info->base + UART_RX);
2459 (void) inb(info->base + UART_IIR);
2460 (void) inb(info->base + UART_MSR);
2462 if (info->tty)
2463 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2464 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2467 * and set the speed of the serial port
2469 spin_unlock_irqrestore(&info->slock, flags);
2470 mxser_change_speed(info, NULL);
2472 info->flags |= ASYNC_INITIALIZED;
2473 return 0;
2477 * This routine will shutdown a serial port; interrupts maybe disabled, and
2478 * DTR is dropped if the hangup on close termio flag is on.
2480 static void mxser_shutdown(struct mxser_struct *info)
2482 unsigned long flags;
2484 if (!(info->flags & ASYNC_INITIALIZED))
2485 return;
2487 spin_lock_irqsave(&info->slock, flags);
2490 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2491 * here so the queue might never be waken up
2493 wake_up_interruptible(&info->delta_msr_wait);
2496 * Free the IRQ, if necessary
2498 if (info->xmit_buf) {
2499 free_page((unsigned long) info->xmit_buf);
2500 info->xmit_buf = NULL;
2503 info->IER = 0;
2504 outb(0x00, info->base + UART_IER);
2506 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
2507 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
2508 outb(info->MCR, info->base + UART_MCR);
2510 /* clear Rx/Tx FIFO's */
2511 /* following add by Victor Yu. 08-30-2002 */
2512 if (info->IsMoxaMustChipFlag)
2513 outb((UART_FCR_CLEAR_RCVR |
2514 UART_FCR_CLEAR_XMIT |
2515 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
2516 else
2517 /* above add by Victor Yu. 08-30-2002 */
2518 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2519 info->base + UART_FCR);
2521 /* read data port to reset things */
2522 (void) inb(info->base + UART_RX);
2524 if (info->tty)
2525 set_bit(TTY_IO_ERROR, &info->tty->flags);
2527 info->flags &= ~ASYNC_INITIALIZED;
2529 /* following add by Victor Yu. 09-23-2002 */
2530 if (info->IsMoxaMustChipFlag)
2531 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->base);
2532 /* above add by Victor Yu. 09-23-2002 */
2534 spin_unlock_irqrestore(&info->slock, flags);
2538 * This routine is called to set the UART divisor registers to match
2539 * the specified baud rate for a serial port.
2541 static int mxser_change_speed(struct mxser_struct *info, struct termios *old_termios)
2543 unsigned cflag, cval, fcr;
2544 int ret = 0;
2545 unsigned char status;
2546 long baud;
2547 unsigned long flags;
2549 if (!info->tty || !info->tty->termios)
2550 return ret;
2551 cflag = info->tty->termios->c_cflag;
2552 if (!(info->base))
2553 return ret;
2555 #ifndef B921600
2556 #define B921600 (B460800 +1)
2557 #endif
2558 if (mxser_set_baud_method[info->port] == 0) {
2559 switch (cflag & (CBAUD | CBAUDEX)) {
2560 case B921600:
2561 baud = 921600;
2562 break;
2563 case B460800:
2564 baud = 460800;
2565 break;
2566 case B230400:
2567 baud = 230400;
2568 break;
2569 case B115200:
2570 baud = 115200;
2571 break;
2572 case B57600:
2573 baud = 57600;
2574 break;
2575 case B38400:
2576 baud = 38400;
2577 break;
2578 case B19200:
2579 baud = 19200;
2580 break;
2581 case B9600:
2582 baud = 9600;
2583 break;
2584 case B4800:
2585 baud = 4800;
2586 break;
2587 case B2400:
2588 baud = 2400;
2589 break;
2590 case B1800:
2591 baud = 1800;
2592 break;
2593 case B1200:
2594 baud = 1200;
2595 break;
2596 case B600:
2597 baud = 600;
2598 break;
2599 case B300:
2600 baud = 300;
2601 break;
2602 case B200:
2603 baud = 200;
2604 break;
2605 case B150:
2606 baud = 150;
2607 break;
2608 case B134:
2609 baud = 134;
2610 break;
2611 case B110:
2612 baud = 110;
2613 break;
2614 case B75:
2615 baud = 75;
2616 break;
2617 case B50:
2618 baud = 50;
2619 break;
2620 default:
2621 baud = 0;
2622 break;
2624 mxser_set_baud(info, baud);
2627 /* byte size and parity */
2628 switch (cflag & CSIZE) {
2629 case CS5:
2630 cval = 0x00;
2631 break;
2632 case CS6:
2633 cval = 0x01;
2634 break;
2635 case CS7:
2636 cval = 0x02;
2637 break;
2638 case CS8:
2639 cval = 0x03;
2640 break;
2641 default:
2642 cval = 0x00;
2643 break; /* too keep GCC shut... */
2645 if (cflag & CSTOPB)
2646 cval |= 0x04;
2647 if (cflag & PARENB)
2648 cval |= UART_LCR_PARITY;
2649 if (!(cflag & PARODD))
2650 cval |= UART_LCR_EPAR;
2651 if (cflag & CMSPAR)
2652 cval |= UART_LCR_SPAR;
2654 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
2655 if (info->IsMoxaMustChipFlag) {
2656 fcr = UART_FCR_ENABLE_FIFO;
2657 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2658 SET_MOXA_MUST_FIFO_VALUE(info);
2659 } else
2660 fcr = 0;
2661 } else {
2662 fcr = UART_FCR_ENABLE_FIFO;
2663 /* following add by Victor Yu. 08-30-2002 */
2664 if (info->IsMoxaMustChipFlag) {
2665 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2666 SET_MOXA_MUST_FIFO_VALUE(info);
2667 } else {
2668 /* above add by Victor Yu. 08-30-2002 */
2669 switch (info->rx_trigger) {
2670 case 1:
2671 fcr |= UART_FCR_TRIGGER_1;
2672 break;
2673 case 4:
2674 fcr |= UART_FCR_TRIGGER_4;
2675 break;
2676 case 8:
2677 fcr |= UART_FCR_TRIGGER_8;
2678 break;
2679 default:
2680 fcr |= UART_FCR_TRIGGER_14;
2681 break;
2686 /* CTS flow control flag and modem status interrupts */
2687 info->IER &= ~UART_IER_MSI;
2688 info->MCR &= ~UART_MCR_AFE;
2689 if (cflag & CRTSCTS) {
2690 info->flags |= ASYNC_CTS_FLOW;
2691 info->IER |= UART_IER_MSI;
2692 if ((info->type == PORT_16550A) || (info->IsMoxaMustChipFlag)) {
2693 info->MCR |= UART_MCR_AFE;
2694 /* status = mxser_get_msr(info->base, 0, info->port); */
2696 save_flags(flags);
2697 cli();
2698 status = inb(baseaddr + UART_MSR);
2699 restore_flags(flags);
2701 /* mxser_check_modem_status(info, status); */
2702 } else {
2703 /* status = mxser_get_msr(info->base, 0, info->port); */
2704 /* MX_LOCK(&info->slock); */
2705 status = inb(info->base + UART_MSR);
2706 /* MX_UNLOCK(&info->slock); */
2707 if (info->tty->hw_stopped) {
2708 if (status & UART_MSR_CTS) {
2709 info->tty->hw_stopped = 0;
2710 if ((info->type != PORT_16550A) &&
2711 (!info->IsMoxaMustChipFlag)) {
2712 info->IER |= UART_IER_THRI;
2713 outb(info->IER, info->base + UART_IER);
2715 set_bit(MXSER_EVENT_TXLOW, &info->event);
2716 schedule_work(&info->tqueue); }
2717 } else {
2718 if (!(status & UART_MSR_CTS)) {
2719 info->tty->hw_stopped = 1;
2720 if ((info->type != PORT_16550A) &&
2721 (!info->IsMoxaMustChipFlag)) {
2722 info->IER &= ~UART_IER_THRI;
2723 outb(info->IER, info->base + UART_IER);
2728 } else {
2729 info->flags &= ~ASYNC_CTS_FLOW;
2731 outb(info->MCR, info->base + UART_MCR);
2732 if (cflag & CLOCAL) {
2733 info->flags &= ~ASYNC_CHECK_CD;
2734 } else {
2735 info->flags |= ASYNC_CHECK_CD;
2736 info->IER |= UART_IER_MSI;
2738 outb(info->IER, info->base + UART_IER);
2741 * Set up parity check flag
2743 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2744 if (I_INPCK(info->tty))
2745 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2746 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2747 info->read_status_mask |= UART_LSR_BI;
2749 info->ignore_status_mask = 0;
2751 if (I_IGNBRK(info->tty)) {
2752 info->ignore_status_mask |= UART_LSR_BI;
2753 info->read_status_mask |= UART_LSR_BI;
2755 * If we're ignore parity and break indicators, ignore
2756 * overruns too. (For real raw support).
2758 if (I_IGNPAR(info->tty)) {
2759 info->ignore_status_mask |=
2760 UART_LSR_OE |
2761 UART_LSR_PE |
2762 UART_LSR_FE;
2763 info->read_status_mask |=
2764 UART_LSR_OE |
2765 UART_LSR_PE |
2766 UART_LSR_FE;
2769 /* following add by Victor Yu. 09-02-2002 */
2770 if (info->IsMoxaMustChipFlag) {
2771 spin_lock_irqsave(&info->slock, flags);
2772 SET_MOXA_MUST_XON1_VALUE(info->base, START_CHAR(info->tty));
2773 SET_MOXA_MUST_XOFF1_VALUE(info->base, STOP_CHAR(info->tty));
2774 if (I_IXON(info->tty)) {
2775 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2776 } else {
2777 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2779 if (I_IXOFF(info->tty)) {
2780 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2781 } else {
2782 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2785 if ( I_IXANY(info->tty) ) {
2786 info->MCR |= MOXA_MUST_MCR_XON_ANY;
2787 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2788 } else {
2789 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
2790 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2793 spin_unlock_irqrestore(&info->slock, flags);
2795 /* above add by Victor Yu. 09-02-2002 */
2798 outb(fcr, info->base + UART_FCR); /* set fcr */
2799 outb(cval, info->base + UART_LCR);
2801 return ret;
2805 static int mxser_set_baud(struct mxser_struct *info, long newspd)
2807 int quot = 0;
2808 unsigned char cval;
2809 int ret = 0;
2810 unsigned long flags;
2812 if (!info->tty || !info->tty->termios)
2813 return ret;
2815 if (!(info->base))
2816 return ret;
2818 if (newspd > info->MaxCanSetBaudRate)
2819 return 0;
2821 info->realbaud = newspd;
2822 if (newspd == 134) {
2823 quot = (2 * info->baud_base / 269);
2824 } else if (newspd) {
2825 quot = info->baud_base / newspd;
2826 if (quot == 0)
2827 quot = 1;
2828 } else {
2829 quot = 0;
2832 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
2833 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2835 if (quot) {
2836 spin_lock_irqsave(&info->slock, flags);
2837 info->MCR |= UART_MCR_DTR;
2838 outb(info->MCR, info->base + UART_MCR);
2839 spin_unlock_irqrestore(&info->slock, flags);
2840 } else {
2841 spin_lock_irqsave(&info->slock, flags);
2842 info->MCR &= ~UART_MCR_DTR;
2843 outb(info->MCR, info->base + UART_MCR);
2844 spin_unlock_irqrestore(&info->slock, flags);
2845 return ret;
2848 cval = inb(info->base + UART_LCR);
2850 outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2852 outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2853 outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2854 outb(cval, info->base + UART_LCR); /* reset DLAB */
2857 return ret;
2861 * ------------------------------------------------------------
2862 * friends of mxser_ioctl()
2863 * ------------------------------------------------------------
2865 static int mxser_get_serial_info(struct mxser_struct *info, struct serial_struct __user *retinfo)
2867 struct serial_struct tmp;
2869 if (!retinfo)
2870 return -EFAULT;
2871 memset(&tmp, 0, sizeof(tmp));
2872 tmp.type = info->type;
2873 tmp.line = info->port;
2874 tmp.port = info->base;
2875 tmp.irq = info->irq;
2876 tmp.flags = info->flags;
2877 tmp.baud_base = info->baud_base;
2878 tmp.close_delay = info->close_delay;
2879 tmp.closing_wait = info->closing_wait;
2880 tmp.custom_divisor = info->custom_divisor;
2881 tmp.hub6 = 0;
2882 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2883 return -EFAULT;
2884 return 0;
2887 static int mxser_set_serial_info(struct mxser_struct *info, struct serial_struct __user *new_info)
2889 struct serial_struct new_serial;
2890 unsigned int flags;
2891 int retval = 0;
2893 if (!new_info || !info->base)
2894 return -EFAULT;
2895 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2896 return -EFAULT;
2898 if ((new_serial.irq != info->irq) ||
2899 (new_serial.port != info->base) ||
2900 (new_serial.custom_divisor != info->custom_divisor) ||
2901 (new_serial.baud_base != info->baud_base))
2902 return -EPERM;
2904 flags = info->flags & ASYNC_SPD_MASK;
2906 if (!capable(CAP_SYS_ADMIN)) {
2907 if ((new_serial.baud_base != info->baud_base) ||
2908 (new_serial.close_delay != info->close_delay) ||
2909 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
2910 return -EPERM;
2911 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2912 (new_serial.flags & ASYNC_USR_MASK));
2913 } else {
2915 * OK, past this point, all the error checking has been done.
2916 * At this point, we start making changes.....
2918 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2919 (new_serial.flags & ASYNC_FLAGS));
2920 info->close_delay = new_serial.close_delay * HZ / 100;
2921 info->closing_wait = new_serial.closing_wait * HZ / 100;
2922 info->tty->low_latency =
2923 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2924 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
2927 /* added by casper, 3/17/2000, for mouse */
2928 info->type = new_serial.type;
2930 process_txrx_fifo(info);
2932 if (info->flags & ASYNC_INITIALIZED) {
2933 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2934 mxser_change_speed(info, NULL);
2936 } else {
2937 retval = mxser_startup(info);
2939 return retval;
2943 * mxser_get_lsr_info - get line status register info
2945 * Purpose: Let user call ioctl() to get info when the UART physically
2946 * is emptied. On bus types like RS485, the transmitter must
2947 * release the bus after transmitting. This must be done when
2948 * the transmit shift register is empty, not be done when the
2949 * transmit holding register is empty. This functionality
2950 * allows an RS485 driver to be written in user space.
2952 static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int __user *value)
2954 unsigned char status;
2955 unsigned int result;
2956 unsigned long flags;
2958 spin_lock_irqsave(&info->slock, flags);
2959 status = inb(info->base + UART_LSR);
2960 spin_unlock_irqrestore(&info->slock, flags);
2961 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2962 return put_user(result, value);
2966 * This routine sends a break character out the serial port.
2968 static void mxser_send_break(struct mxser_struct *info, int duration)
2970 unsigned long flags;
2972 if (!info->base)
2973 return;
2974 set_current_state(TASK_INTERRUPTIBLE);
2975 spin_lock_irqsave(&info->slock, flags);
2976 outb(inb(info->base + UART_LCR) | UART_LCR_SBC,
2977 info->base + UART_LCR);
2978 spin_unlock_irqrestore(&info->slock, flags);
2979 schedule_timeout(duration);
2980 spin_lock_irqsave(&info->slock, flags);
2981 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC,
2982 info->base + UART_LCR);
2983 spin_unlock_irqrestore(&info->slock, flags);
2986 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2988 struct mxser_struct *info = tty->driver_data;
2989 unsigned char control, status;
2990 unsigned long flags;
2993 if (tty->index == MXSER_PORTS)
2994 return -ENOIOCTLCMD;
2995 if (tty->flags & (1 << TTY_IO_ERROR))
2996 return -EIO;
2998 control = info->MCR;
3000 spin_lock_irqsave(&info->slock, flags);
3001 status = inb(info->base + UART_MSR);
3002 if (status & UART_MSR_ANY_DELTA)
3003 mxser_check_modem_status(info, status);
3004 spin_unlock_irqrestore(&info->slock, flags);
3005 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
3006 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
3007 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
3008 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
3009 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
3010 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
3013 static int mxser_tiocmset(struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear)
3015 struct mxser_struct *info = tty->driver_data;
3016 unsigned long flags;
3019 if (tty->index == MXSER_PORTS)
3020 return -ENOIOCTLCMD;
3021 if (tty->flags & (1 << TTY_IO_ERROR))
3022 return -EIO;
3024 spin_lock_irqsave(&info->slock, flags);
3026 if (set & TIOCM_RTS)
3027 info->MCR |= UART_MCR_RTS;
3028 if (set & TIOCM_DTR)
3029 info->MCR |= UART_MCR_DTR;
3031 if (clear & TIOCM_RTS)
3032 info->MCR &= ~UART_MCR_RTS;
3033 if (clear & TIOCM_DTR)
3034 info->MCR &= ~UART_MCR_DTR;
3036 outb(info->MCR, info->base + UART_MCR);
3037 spin_unlock_irqrestore(&info->slock, flags);
3038 return 0;
3042 static int mxser_read_register(int, unsigned short *);
3043 static int mxser_program_mode(int);
3044 static void mxser_normal_mode(int);
3046 static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
3048 int id, i, bits;
3049 unsigned short regs[16], irq;
3050 unsigned char scratch, scratch2;
3052 hwconf->IsMoxaMustChipFlag = MOXA_OTHER_UART;
3054 id = mxser_read_register(cap, regs);
3055 if (id == C168_ASIC_ID) {
3056 hwconf->board_type = MXSER_BOARD_C168_ISA;
3057 hwconf->ports = 8;
3058 } else if (id == C104_ASIC_ID) {
3059 hwconf->board_type = MXSER_BOARD_C104_ISA;
3060 hwconf->ports = 4;
3061 } else if (id == C102_ASIC_ID) {
3062 hwconf->board_type = MXSER_BOARD_C102_ISA;
3063 hwconf->ports = 2;
3064 } else if (id == CI132_ASIC_ID) {
3065 hwconf->board_type = MXSER_BOARD_CI132;
3066 hwconf->ports = 2;
3067 } else if (id == CI134_ASIC_ID) {
3068 hwconf->board_type = MXSER_BOARD_CI134;
3069 hwconf->ports = 4;
3070 } else if (id == CI104J_ASIC_ID) {
3071 hwconf->board_type = MXSER_BOARD_CI104J;
3072 hwconf->ports = 4;
3073 } else
3074 return 0;
3076 irq = 0;
3077 if (hwconf->ports == 2) {
3078 irq = regs[9] & 0xF000;
3079 irq = irq | (irq >> 4);
3080 if (irq != (regs[9] & 0xFF00))
3081 return MXSER_ERR_IRQ_CONFLIT;
3082 } else if (hwconf->ports == 4) {
3083 irq = regs[9] & 0xF000;
3084 irq = irq | (irq >> 4);
3085 irq = irq | (irq >> 8);
3086 if (irq != regs[9])
3087 return MXSER_ERR_IRQ_CONFLIT;
3088 } else if (hwconf->ports == 8) {
3089 irq = regs[9] & 0xF000;
3090 irq = irq | (irq >> 4);
3091 irq = irq | (irq >> 8);
3092 if ((irq != regs[9]) || (irq != regs[10]))
3093 return MXSER_ERR_IRQ_CONFLIT;
3096 if (!irq)
3097 return MXSER_ERR_IRQ;
3098 hwconf->irq = ((int)(irq & 0xF000) >> 12);
3099 for (i = 0; i < 8; i++)
3100 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
3101 if ((regs[12] & 0x80) == 0)
3102 return MXSER_ERR_VECTOR;
3103 hwconf->vector = (int)regs[11]; /* interrupt vector */
3104 if (id == 1)
3105 hwconf->vector_mask = 0x00FF;
3106 else
3107 hwconf->vector_mask = 0x000F;
3108 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
3109 if (regs[12] & bits) {
3110 hwconf->baud_base[i] = 921600;
3111 hwconf->MaxCanSetBaudRate[i] = 921600; /* add by Victor Yu. 09-04-2002 */
3112 } else {
3113 hwconf->baud_base[i] = 115200;
3114 hwconf->MaxCanSetBaudRate[i] = 115200; /* add by Victor Yu. 09-04-2002 */
3117 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
3118 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
3119 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
3120 outb(scratch2, cap + UART_LCR);
3121 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
3122 scratch = inb(cap + UART_IIR);
3124 if (scratch & 0xC0)
3125 hwconf->uart_type = PORT_16550A;
3126 else
3127 hwconf->uart_type = PORT_16450;
3128 if (id == 1)
3129 hwconf->ports = 8;
3130 else
3131 hwconf->ports = 4;
3132 request_region(hwconf->ioaddr[0], 8 * hwconf->ports, "mxser(IO)");
3133 request_region(hwconf->vector, 1, "mxser(vector)");
3134 return hwconf->ports;
3137 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
3138 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
3139 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
3140 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
3141 #define EN_CCMD 0x000 /* Chip's command register */
3142 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
3143 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
3144 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
3145 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
3146 #define EN0_DCFG 0x00E /* Data configuration reg WR */
3147 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
3148 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
3149 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
3150 static int mxser_read_register(int port, unsigned short *regs)
3152 int i, k, value, id;
3153 unsigned int j;
3155 id = mxser_program_mode(port);
3156 if (id < 0)
3157 return id;
3158 for (i = 0; i < 14; i++) {
3159 k = (i & 0x3F) | 0x180;
3160 for (j = 0x100; j > 0; j >>= 1) {
3161 outb(CHIP_CS, port);
3162 if (k & j) {
3163 outb(CHIP_CS | CHIP_DO, port);
3164 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
3165 } else {
3166 outb(CHIP_CS, port);
3167 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
3170 (void)inb(port);
3171 value = 0;
3172 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
3173 outb(CHIP_CS, port);
3174 outb(CHIP_CS | CHIP_SK, port);
3175 if (inb(port) & CHIP_DI)
3176 value |= j;
3178 regs[i] = value;
3179 outb(0, port);
3181 mxser_normal_mode(port);
3182 return id;
3185 static int mxser_program_mode(int port)
3187 int id, i, j, n;
3188 /* unsigned long flags; */
3190 spin_lock(&gm_lock);
3191 outb(0, port);
3192 outb(0, port);
3193 outb(0, port);
3194 (void)inb(port);
3195 (void)inb(port);
3196 outb(0, port);
3197 (void)inb(port);
3198 /* restore_flags(flags); */
3199 spin_unlock(&gm_lock);
3201 id = inb(port + 1) & 0x1F;
3202 if ((id != C168_ASIC_ID) &&
3203 (id != C104_ASIC_ID) &&
3204 (id != C102_ASIC_ID) &&
3205 (id != CI132_ASIC_ID) &&
3206 (id != CI134_ASIC_ID) &&
3207 (id != CI104J_ASIC_ID))
3208 return -1;
3209 for (i = 0, j = 0; i < 4; i++) {
3210 n = inb(port + 2);
3211 if (n == 'M') {
3212 j = 1;
3213 } else if ((j == 1) && (n == 1)) {
3214 j = 2;
3215 break;
3216 } else
3217 j = 0;
3219 if (j != 2)
3220 id = -2;
3221 return id;
3224 static void mxser_normal_mode(int port)
3226 int i, n;
3228 outb(0xA5, port + 1);
3229 outb(0x80, port + 3);
3230 outb(12, port + 0); /* 9600 bps */
3231 outb(0, port + 1);
3232 outb(0x03, port + 3); /* 8 data bits */
3233 outb(0x13, port + 4); /* loop back mode */
3234 for (i = 0; i < 16; i++) {
3235 n = inb(port + 5);
3236 if ((n & 0x61) == 0x60)
3237 break;
3238 if ((n & 1) == 1)
3239 (void)inb(port);
3241 outb(0x00, port + 4);
3244 module_init(mxser_module_init);
3245 module_exit(mxser_module_exit);