1 /*****************************************************************************/
3 * mxser.c -- MOXA Smartio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * MOXA Smartio Family Serial Driver
28 * Copyright (C) 1999,2000 Moxa Technologies Co., LTD.
30 * for : LINUX 2.0.X, 2.2.X, 2.4.X
34 * Fixes for C104H/PCI by Tim Hockin <thockin@sun.com>
35 * Added support for: C102, CI-132, CI-134, CP-132, CP-114, CT-114 cards
36 * by Damian Wrobel <dwrobel@ertel.com.pl>
38 * Added support for serial card CP104
39 * by James Nelson Provident Solutions <linux-info@provident-solutions.com>
42 #include <linux/config.h>
43 #include <linux/module.h>
44 #include <linux/errno.h>
45 #include <linux/signal.h>
46 #include <linux/sched.h>
47 #include <linux/timer.h>
48 #include <linux/interrupt.h>
49 #include <linux/tty.h>
50 #include <linux/tty_flip.h>
51 #include <linux/serial.h>
52 #include <linux/serial_reg.h>
53 #include <linux/major.h>
54 #include <linux/string.h>
55 #include <linux/fcntl.h>
56 #include <linux/ptrace.h>
57 #include <linux/ioport.h>
59 #include <linux/smp_lock.h>
60 #include <linux/pci.h>
61 #include <linux/init.h>
63 #include <asm/system.h>
66 #include <asm/bitops.h>
67 #include <asm/uaccess.h>
69 #define MXSER_VERSION "1.2.1"
71 #define MXSERMAJOR 174
72 #define MXSERCUMAJOR 175
75 #define MXSER_EVENT_TXLOW 1
76 #define MXSER_EVENT_HANGUP 2
79 #define SERIAL_DO_RESTART
81 #define MXSER_BOARDS 4 /* Max. boards */
82 #define MXSER_PORTS 32 /* Max. ports */
83 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
84 #define MXSER_ISR_PASS_LIMIT 256
86 #define MXSER_ERR_IOADDR -1
87 #define MXSER_ERR_IRQ -2
88 #define MXSER_ERR_IRQ_CONFLIT -3
89 #define MXSER_ERR_VECTOR -4
91 #define SERIAL_TYPE_NORMAL 1
93 #define WAKEUP_CHARS 256
95 #define UART_MCR_AFE 0x20
96 #define UART_LSR_SPECIAL 0x1E
98 #define PORTNO(x) ((x)->index)
100 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
102 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
105 #define MIN(a,b) ((a) < (b) ? (a) : (b))
109 * Define the Moxa PCI vendor and device IDs.
112 #ifndef PCI_VENDOR_ID_MOXA
113 #define PCI_VENDOR_ID_MOXA 0x1393
115 #ifndef PCI_DEVICE_ID_C168
116 #define PCI_DEVICE_ID_C168 0x1680
118 #ifndef PCI_DEVICE_ID_C104
119 #define PCI_DEVICE_ID_C104 0x1040
121 #ifndef PCI_DEVICE_ID_CP104
122 #define PCI_DEVICE_ID_CP104 0x1041
124 #ifndef PCI_DEVICE_ID_CP132
125 #define PCI_DEVICE_ID_CP132 0x1320
127 #ifndef PCI_DEVICE_ID_CP114
128 #define PCI_DEVICE_ID_CP114 0x1141
130 #ifndef PCI_DEVICE_ID_CT114
131 #define PCI_DEVICE_ID_CT114 0x1140
134 #define C168_ASIC_ID 1
135 #define C104_ASIC_ID 2
136 #define CI134_ASIC_ID 3
137 #define CI132_ASIC_ID 4
138 #define CI104J_ASIC_ID 5
139 #define C102_ASIC_ID 0xB
142 MXSER_BOARD_C168_ISA = 0,
143 MXSER_BOARD_C104_ISA,
145 MXSER_BOARD_C168_PCI,
146 MXSER_BOARD_C104_PCI,
147 MXSER_BOARD_CP104_PCI,
148 MXSER_BOARD_C102_ISA,
151 MXSER_BOARD_CP132_PCI,
152 MXSER_BOARD_CP114_PCI,
153 MXSER_BOARD_CT114_PCI
156 static char *mxser_brdname[] =
172 static int mxser_numports[] =
192 #define MOXA_GETDATACOUNT (MOXA + 23)
193 #define MOXA_GET_CONF (MOXA + 35)
194 #define MOXA_DIAGNOSE (MOXA + 50)
195 #define MOXA_CHKPORTENABLE (MOXA + 60)
196 #define MOXA_HighSpeedOn (MOXA + 61)
197 #define MOXA_GET_MAJOR (MOXA + 63)
198 #define MOXA_GET_CUMAJOR (MOXA + 64)
199 #define MOXA_GETMSTATUS (MOXA + 65)
201 static struct pci_device_id mxser_pcibrds[] = {
202 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C168, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
203 MXSER_BOARD_C168_PCI },
204 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C104, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
205 MXSER_BOARD_C104_PCI },
206 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP104, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
207 MXSER_BOARD_CP104_PCI },
208 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP132, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
209 MXSER_BOARD_CP132_PCI },
210 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP114, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
211 MXSER_BOARD_CP114_PCI },
212 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CT114, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
213 MXSER_BOARD_CT114_PCI },
216 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
218 static int ioaddr[MXSER_BOARDS];
219 static int ttymajor = MXSERMAJOR;
222 /* Variables for insmod */
224 MODULE_AUTHOR("William Chen");
225 MODULE_DESCRIPTION("MOXA Smartio Family Multiport Board Device Driver");
226 MODULE_LICENSE("GPL");
227 MODULE_PARM(ioaddr, "1-4i");
228 MODULE_PARM(ttymajor, "i");
229 MODULE_PARM(verbose, "i");
231 struct mxser_hwconf {
238 int ioaddr[MXSER_PORTS_PER_BOARD];
239 int baud_base[MXSER_PORTS_PER_BOARD];
240 struct pci_dev *pdev;
243 struct mxser_struct {
245 int base; /* port base address */
246 int irq; /* port using irq no. */
247 int vector; /* port irq vector */
248 int vectormask; /* port vector mask */
249 int rx_trigger; /* Rx fifo trigger level */
250 int baud_base; /* max. speed */
251 int flags; /* defined in tty.h */
252 int type; /* UART type */
253 struct tty_struct *tty;
254 int read_status_mask;
255 int ignore_status_mask;
258 int x_char; /* xon/xoff character */
260 unsigned short closing_wait;
261 int IER; /* Interrupt Enable Register */
262 int MCR; /* Modem control register */
264 int count; /* # of fd on device */
265 int blocked_open; /* # of blocked opens */
266 unsigned char *xmit_buf;
270 struct work_struct tqueue;
272 wait_queue_head_t open_wait;
273 wait_queue_head_t close_wait;
274 wait_queue_head_t delta_msr_wait;
275 struct async_icount icount; /* kernel counters for the 4 input interrupts */
280 int rxcnt[MXSER_PORTS];
281 int txcnt[MXSER_PORTS];
284 struct mxser_mstatus {
292 static struct mxser_mstatus GMStatus[MXSER_PORTS];
294 static int mxserBoardCAP[MXSER_BOARDS] =
297 /* 0x180, 0x280, 0x200, 0x320 */
301 static struct tty_driver *mxvar_sdriver;
302 static struct mxser_struct mxvar_table[MXSER_PORTS];
303 static struct mxser_log mxvar_log;
304 static int mxvar_diagflag;
306 * mxvar_tmp_buf is used as a temporary buffer by serial_write. We need
307 * to lock it in case the memcpy_fromfs blocks while swapping in a page,
308 * and some other program tries to do a serial write at the same time.
309 * Since the lock will only come under contention when the system is
310 * swapping and available memory is low, it makes sense to share one
311 * buffer across all the serial ports, since it significantly saves
312 * memory if large numbers of serial ports are open.
314 static unsigned char *mxvar_tmp_buf;
315 static struct semaphore mxvar_tmp_buf_sem;
318 * This is used to figure out the divisor speeds and the timeouts
320 static int mxvar_baud_table[] =
322 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
323 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 0};
325 struct mxser_hwconf mxsercfg[MXSER_BOARDS];
331 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
332 static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
333 static int mxser_get_PCI_conf(struct pci_dev *, int, struct mxser_hwconf *);
334 static void mxser_do_softint(void *);
335 static int mxser_open(struct tty_struct *, struct file *);
336 static void mxser_close(struct tty_struct *, struct file *);
337 static int mxser_write(struct tty_struct *, int, const unsigned char *, int);
338 static int mxser_write_room(struct tty_struct *);
339 static void mxser_flush_buffer(struct tty_struct *);
340 static int mxser_chars_in_buffer(struct tty_struct *);
341 static void mxser_flush_chars(struct tty_struct *);
342 static void mxser_put_char(struct tty_struct *, unsigned char);
343 static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
344 static int mxser_ioctl_special(unsigned int, unsigned long);
345 static void mxser_throttle(struct tty_struct *);
346 static void mxser_unthrottle(struct tty_struct *);
347 static void mxser_set_termios(struct tty_struct *, struct termios *);
348 static void mxser_stop(struct tty_struct *);
349 static void mxser_start(struct tty_struct *);
350 static void mxser_hangup(struct tty_struct *);
351 static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
352 static inline void mxser_receive_chars(struct mxser_struct *, int *);
353 static inline void mxser_transmit_chars(struct mxser_struct *);
354 static inline void mxser_check_modem_status(struct mxser_struct *, int);
355 static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
356 static int mxser_startup(struct mxser_struct *);
357 static void mxser_shutdown(struct mxser_struct *);
358 static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
359 static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct *);
360 static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct *);
361 static int mxser_get_lsr_info(struct mxser_struct *, unsigned int *);
362 static void mxser_send_break(struct mxser_struct *, int);
363 static int mxser_tiocmget(struct tty_struct *, struct file *);
364 static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
367 * The MOXA C168/C104 serial driver boot-time initialization code!
370 static void __exit mxser_module_exit(void)
376 printk("Unloading module mxser ...\n");
377 if ((err |= tty_unregister_driver(mxvar_sdriver)))
378 printk("Couldn't unregister MOXA Smartio family serial driver\n");
379 put_tty_driver(mxvar_sdriver);
381 for (i = 0; i < MXSER_BOARDS; i++) {
382 if (mxsercfg[i].board_type == -1)
385 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
394 int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
396 struct mxser_struct *info;
401 init_MUTEX(&mxvar_tmp_buf_sem);
403 n = board * MXSER_PORTS_PER_BOARD;
404 info = &mxvar_table[n];
405 for (i = 0; i < hwconf->ports; i++, n++, info++) {
407 printk(" ttyM%d/cum%d at 0x%04x ", n, n, hwconf->ioaddr[i]);
408 if (hwconf->baud_base[i] == 115200)
409 printk(" max. baud rate up to 115200 bps.\n");
411 printk(" max. baud rate up to 921600 bps.\n");
414 info->base = hwconf->ioaddr[i];
415 info->irq = hwconf->irq;
416 info->vector = hwconf->vector;
417 info->vectormask = hwconf->vector_mask;
418 info->rx_trigger = 14;
419 info->baud_base = hwconf->baud_base[i];
420 info->flags = ASYNC_SHARE_IRQ;
421 info->type = hwconf->uart_type;
422 if ((info->type == PORT_16450) || (info->type == PORT_8250))
423 info->xmit_fifo_size = 1;
425 info->xmit_fifo_size = 16;
426 info->custom_divisor = hwconf->baud_base[i] * 16;
427 info->close_delay = 5 * HZ / 10;
428 info->closing_wait = 30 * HZ;
429 INIT_WORK(&info->tqueue, mxser_do_softint, info);
430 info->cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
431 init_waitqueue_head(&info->open_wait);
432 init_waitqueue_head(&info->close_wait);
433 init_waitqueue_head(&info->delta_msr_wait);
437 * Allocate the IRQ if necessary
441 n = board * MXSER_PORTS_PER_BOARD;
442 info = &mxvar_table[n];
445 retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
448 restore_flags(flags);
449 printk("Board %d: %s", board, mxser_brdname[hwconf->board_type]);
450 printk(" Request irq fail,IRQ (%d) may be conflit with another device.\n", info->irq);
453 restore_flags(flags);
459 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
461 mxsercfg[board] = *hwconf;
464 static int mxser_get_PCI_conf(struct pci_dev *pdev, int board_type, struct mxser_hwconf *hwconf)
467 unsigned int ioaddress;
469 hwconf->board_type = board_type;
470 hwconf->ports = mxser_numports[board_type];
471 ioaddress = pci_resource_start (pdev, 2);
472 for (i = 0; i < hwconf->ports; i++)
473 hwconf->ioaddr[i] = ioaddress + 8 * i;
475 ioaddress = pci_resource_start (pdev, 3);
476 hwconf->vector = ioaddress;
478 hwconf->irq = pdev->irq;
480 hwconf->uart_type = PORT_16550A;
481 hwconf->vector_mask = 0;
482 for (i = 0; i < hwconf->ports; i++) {
483 hwconf->vector_mask |= (1 << i);
484 hwconf->baud_base[i] = 921600;
489 static struct tty_operations mxser_ops = {
491 .close = mxser_close,
492 .write = mxser_write,
493 .put_char = mxser_put_char,
494 .flush_chars = mxser_flush_chars,
495 .write_room = mxser_write_room,
496 .chars_in_buffer = mxser_chars_in_buffer,
497 .flush_buffer = mxser_flush_buffer,
498 .ioctl = mxser_ioctl,
499 .throttle = mxser_throttle,
500 .unthrottle = mxser_unthrottle,
501 .set_termios = mxser_set_termios,
503 .start = mxser_start,
504 .hangup = mxser_hangup,
505 .tiocmget = mxser_tiocmget,
506 .tiocmset = mxser_tiocmset,
509 static int __init mxser_module_init(void)
512 struct mxser_hwconf hwconf;
514 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
518 printk("MOXA Smartio family driver version %s\n", MXSER_VERSION);
520 /* Initialize the tty_driver structure */
522 mxvar_sdriver->owner = THIS_MODULE;
523 mxvar_sdriver->name = "ttyM";
524 mxvar_sdriver->devfs_name = "tts/M";
525 mxvar_sdriver->major = ttymajor;
526 mxvar_sdriver->minor_start = 0;
527 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
528 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
529 mxvar_sdriver->init_termios = tty_std_termios;
530 mxvar_sdriver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
531 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
532 tty_set_operations(mxvar_sdriver, &mxser_ops);
533 printk("Tty devices major number = %d\n", ttymajor);
536 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
537 memset(&mxvar_log, 0, sizeof(struct mxser_log));
541 /* Start finding ISA boards here */
542 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
544 if (!(cap = mxserBoardCAP[b]))
547 retval = mxser_get_ISA_conf(cap, &hwconf);
550 printk("Found MOXA %s board (CAP=0x%x)\n",
551 mxser_brdname[hwconf.board_type],
555 if (retval == MXSER_ERR_IRQ)
556 printk("Invalid interrupt number,board not configured\n");
557 else if (retval == MXSER_ERR_IRQ_CONFLIT)
558 printk("Invalid interrupt number,board not configured\n");
559 else if (retval == MXSER_ERR_VECTOR)
560 printk("Invalid interrupt vector,board not configured\n");
561 else if (retval == MXSER_ERR_IOADDR)
562 printk("Invalid I/O address,board not configured\n");
568 if (mxser_initbrd(m, &hwconf) < 0)
571 mxser_getcfg(m, &hwconf);
576 /* Start finding ISA boards from module arg */
577 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
579 if (!(cap = ioaddr[b]))
582 retval = mxser_get_ISA_conf(cap, &hwconf);
585 printk("Found MOXA %s board (CAP=0x%x)\n",
586 mxser_brdname[hwconf.board_type],
590 if (retval == MXSER_ERR_IRQ)
591 printk("Invalid interrupt number,board not configured\n");
592 else if (retval == MXSER_ERR_IRQ_CONFLIT)
593 printk("Invalid interrupt number,board not configured\n");
594 else if (retval == MXSER_ERR_VECTOR)
595 printk("Invalid interrupt vector,board not configured\n");
596 else if (retval == MXSER_ERR_IOADDR)
597 printk("Invalid I/O address,board not configured\n");
603 if (mxser_initbrd(m, &hwconf) < 0)
606 mxser_getcfg(m, &hwconf);
611 /* start finding PCI board here */
615 struct pci_dev *pdev = NULL;
616 int n = (sizeof(mxser_pcibrds) / sizeof(mxser_pcibrds[0])) - 1;
618 for (b = 0; b < n; b++) {
619 while ((pdev = pci_find_device(mxser_pcibrds[b].vendor, mxser_pcibrds[b].device, pdev)))
621 if (pci_enable_device(pdev))
624 printk("Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
625 mxser_brdname[mxser_pcibrds[b].driver_data],
626 pdev->bus->number, PCI_SLOT(pdev->devfn));
627 if (m >= MXSER_BOARDS) {
628 printk("Too many Smartio family boards found (maximum %d),board not configured\n", MXSER_BOARDS);
630 retval = mxser_get_PCI_conf(pdev, mxser_pcibrds[b].driver_data, &hwconf);
632 if (retval == MXSER_ERR_IRQ)
633 printk("Invalid interrupt number,board not configured\n");
634 else if (retval == MXSER_ERR_IRQ_CONFLIT)
635 printk("Invalid interrupt number,board not configured\n");
636 else if (retval == MXSER_ERR_VECTOR)
637 printk("Invalid interrupt vector,board not configured\n");
638 else if (retval == MXSER_ERR_IOADDR)
639 printk("Invalid I/O address,board not configured\n");
642 if (mxser_initbrd(m, &hwconf) < 0)
644 mxser_getcfg(m, &hwconf);
652 for (i = m; i < MXSER_BOARDS; i++) {
653 mxsercfg[i].board_type = -1;
657 if (!tty_register_driver(mxvar_sdriver))
660 put_tty_driver(mxvar_sdriver);
661 printk("Couldn't install MOXA Smartio family driver !\n");
663 for (i = 0; i < MXSER_BOARDS; i++) {
664 if (mxsercfg[i].board_type == -1)
666 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
671 static void mxser_do_softint(void *private_)
673 struct mxser_struct *info = (struct mxser_struct *) private_;
674 struct tty_struct *tty;
678 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event)) {
679 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
680 tty->ldisc.write_wakeup)
681 (tty->ldisc.write_wakeup) (tty);
682 wake_up_interruptible(&tty->write_wait);
684 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event)) {
685 tty_hangup(tty); /* FIXME: module removal race here - AKPM */
691 * This routine is called whenever a serial port is opened. It
692 * enables interrupts for a serial port, linking in its async structure into
693 * the IRQ chain. It also performs the serial-specific
694 * initialization for the tty structure.
697 static int mxser_open(struct tty_struct *tty, struct file *filp)
699 struct mxser_struct *info;
704 if (line == MXSER_PORTS)
706 if ((line < 0) || (line > MXSER_PORTS))
708 info = mxvar_table + line;
713 tty->driver_data = info;
716 if (!mxvar_tmp_buf) {
717 page = get_zeroed_page(GFP_KERNEL);
723 mxvar_tmp_buf = (unsigned char *) page;
726 * Start up serial port
728 retval = mxser_startup(info);
732 return mxser_block_til_ready(tty, filp, info);
736 * This routine is called when the serial port gets closed. First, we
737 * wait for the last remaining data to be sent. Then, we unlink its
738 * async structure from the interrupt chain if necessary, and we free
739 * that IRQ if nothing is left in the chain.
742 static void mxser_close(struct tty_struct *tty, struct file *filp)
744 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
746 unsigned long timeout;
748 if (PORTNO(tty) == MXSER_PORTS)
756 if (tty_hung_up_p(filp)) {
757 restore_flags(flags);
760 if ((tty->count == 1) && (info->count != 1)) {
762 * Uh, oh. tty->count is 1, which means that the tty
763 * structure will be freed. Info->count should always
764 * be one in these conditions. If it's greater than
765 * one, we've got real problems, since it means the
766 * serial port won't be shutdown.
768 printk("mxser_close: bad serial port count; tty->count is 1, "
769 "info->count is %d\n", info->count);
772 if (--info->count < 0) {
773 printk("mxser_close: bad serial port count for ttys%d: %d\n",
774 info->port, info->count);
778 restore_flags(flags);
781 info->flags |= ASYNC_CLOSING;
782 info->cflag = tty->termios->c_cflag;
784 * Now we wait for the transmit buffer to clear; and we notify
785 * the line discipline to only process XON/XOFF characters.
788 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
789 tty_wait_until_sent(tty, info->closing_wait);
791 * At this point we stop accepting input. To do this, we
792 * disable the receive line status interrupts, and tell the
793 * interrupt driver to stop checking the data ready bit in the
794 * line status register.
796 info->IER &= ~UART_IER_RLSI;
798 info->read_status_mask &= ~UART_LSR_DR;
800 if (info->flags & ASYNC_INITIALIZED) {
801 outb(info->IER, info->base + UART_IER);
803 * Before we drop DTR, make sure the UART transmitter
804 * has completely drained; this is especially
805 * important if there is a transmit FIFO!
807 timeout = jiffies + HZ;
808 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
809 set_current_state(TASK_INTERRUPTIBLE);
811 if (time_after(jiffies, timeout))
815 mxser_shutdown(info);
816 if (tty->driver->flush_buffer)
817 tty->driver->flush_buffer(tty);
818 if (tty->ldisc.flush_buffer)
819 tty->ldisc.flush_buffer(tty);
823 if (info->blocked_open) {
824 if (info->close_delay) {
825 set_current_state(TASK_INTERRUPTIBLE);
826 schedule_timeout(info->close_delay);
828 wake_up_interruptible(&info->open_wait);
830 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
831 wake_up_interruptible(&info->close_wait);
832 restore_flags(flags);
836 static int mxser_write(struct tty_struct *tty, int from_user,
837 const unsigned char *buf, int count)
840 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
843 if (!tty || !info->xmit_buf || !mxvar_tmp_buf)
848 down(&mxvar_tmp_buf_sem);
850 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
851 SERIAL_XMIT_SIZE - info->xmit_head));
855 c -= copy_from_user(mxvar_tmp_buf, buf, c);
863 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
864 SERIAL_XMIT_SIZE - info->xmit_head));
865 memcpy(info->xmit_buf + info->xmit_head, mxvar_tmp_buf, c);
866 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
868 restore_flags(flags);
874 up(&mxvar_tmp_buf_sem);
878 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
879 SERIAL_XMIT_SIZE - info->xmit_head));
881 restore_flags(flags);
885 memcpy(info->xmit_buf + info->xmit_head, buf, c);
886 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
888 restore_flags(flags);
897 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
898 !(info->IER & UART_IER_THRI)) {
899 info->IER |= UART_IER_THRI;
900 outb(info->IER, info->base + UART_IER);
902 restore_flags(flags);
906 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
908 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
911 if (!tty || !info->xmit_buf)
916 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
917 restore_flags(flags);
920 info->xmit_buf[info->xmit_head++] = ch;
921 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
923 /********************************************** why ??? ***********
924 if ( !tty->stopped && !tty->hw_stopped &&
925 !(info->IER & UART_IER_THRI) ) {
926 info->IER |= UART_IER_THRI;
927 outb(info->IER, info->base + UART_IER);
929 *****************************************************************/
930 restore_flags(flags);
933 static void mxser_flush_chars(struct tty_struct *tty)
935 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
938 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
944 info->IER |= UART_IER_THRI;
945 outb(info->IER, info->base + UART_IER);
946 restore_flags(flags);
949 static int mxser_write_room(struct tty_struct *tty)
951 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
954 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
960 static int mxser_chars_in_buffer(struct tty_struct *tty)
962 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
964 return (info->xmit_cnt);
967 static void mxser_flush_buffer(struct tty_struct *tty)
969 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
974 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
975 restore_flags(flags);
976 wake_up_interruptible(&tty->write_wait);
977 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
978 tty->ldisc.write_wakeup)
979 (tty->ldisc.write_wakeup) (tty);
982 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
983 unsigned int cmd, unsigned long arg)
986 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
988 struct async_icount cprev, cnow; /* kernel counter temps */
989 struct serial_icounter_struct *p_cuser; /* user space */
992 if (PORTNO(tty) == MXSER_PORTS)
993 return (mxser_ioctl_special(cmd, arg));
994 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) &&
995 (cmd != TIOCGICOUNT)) {
996 if (tty->flags & (1 << TTY_IO_ERROR))
1000 case TCSBRK: /* SVID version: non-zero arg --> no break */
1001 retval = tty_check_change(tty);
1004 tty_wait_until_sent(tty, 0);
1006 mxser_send_break(info, HZ / 4); /* 1/4 second */
1008 case TCSBRKP: /* support for POSIX tcsendbreak() */
1009 retval = tty_check_change(tty);
1012 tty_wait_until_sent(tty, 0);
1013 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1016 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
1018 if(get_user(templ, (unsigned long *) arg))
1021 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1022 (arg ? CLOCAL : 0));
1025 return (mxser_get_serial_info(info, (struct serial_struct *) arg));
1027 return (mxser_set_serial_info(info, (struct serial_struct *) arg));
1028 case TIOCSERGETLSR: /* Get line status register */
1029 return (mxser_get_lsr_info(info, (unsigned int *) arg));
1031 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1032 * - mask passed in arg for lines of interest
1033 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1034 * Caller should use TIOCGICOUNT to see which one it was
1039 cprev = info->icount; /* note the counters on entry */
1040 restore_flags(flags);
1042 interruptible_sleep_on(&info->delta_msr_wait);
1043 /* see if a signal did it */
1044 if (signal_pending(current))
1045 return (-ERESTARTSYS);
1048 cnow = info->icount; /* atomic copy */
1049 restore_flags(flags);
1050 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1051 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1052 return (-EIO); /* no change => error */
1053 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1054 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1055 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1056 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1063 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1064 * Return: write counters to the user passed counter struct
1065 * NB: both 1->0 and 0->1 transitions are counted except for
1066 * RI where only 0->1 is counted.
1071 cnow = info->icount;
1072 restore_flags(flags);
1073 p_cuser = (struct serial_icounter_struct *) arg;
1074 if(put_user(cnow.cts, &p_cuser->cts))
1076 if(put_user(cnow.dsr, &p_cuser->dsr))
1078 if(put_user(cnow.rng, &p_cuser->rng))
1080 return put_user(cnow.dcd, &p_cuser->dcd);
1081 case MOXA_HighSpeedOn:
1082 return put_user(info->baud_base != 115200 ? 1 : 0, (int *) arg);
1084 return (-ENOIOCTLCMD);
1089 static int mxser_ioctl_special(unsigned int cmd, unsigned long arg)
1091 int i, result, status;
1095 if(copy_to_user((struct mxser_hwconf *) arg, mxsercfg,
1096 sizeof(struct mxser_hwconf) * 4))
1099 case MOXA_GET_MAJOR:
1100 if(copy_to_user((int *) arg, &ttymajor, sizeof(int)))
1104 case MOXA_GET_CUMAJOR:
1106 if(copy_to_user((int *) arg, &result, sizeof(int)))
1110 case MOXA_CHKPORTENABLE:
1112 for (i = 0; i < MXSER_PORTS; i++) {
1113 if (mxvar_table[i].base)
1116 return put_user(result, (unsigned long *) arg);
1117 case MOXA_GETDATACOUNT:
1118 if(copy_to_user((struct mxser_log *) arg, &mxvar_log, sizeof(mxvar_log)))
1121 case MOXA_GETMSTATUS:
1122 for (i = 0; i < MXSER_PORTS; i++) {
1124 if (!mxvar_table[i].base) {
1125 GMStatus[i].dcd = 0;
1126 GMStatus[i].dsr = 0;
1127 GMStatus[i].cts = 0;
1130 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1131 GMStatus[i].cflag = mxvar_table[i].cflag;
1133 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1135 status = inb(mxvar_table[i].base + UART_MSR);
1136 if (status & 0x80 /*UART_MSR_DCD */ )
1137 GMStatus[i].dcd = 1;
1139 GMStatus[i].dcd = 0;
1141 if (status & 0x20 /*UART_MSR_DSR */ )
1142 GMStatus[i].dsr = 1;
1144 GMStatus[i].dsr = 0;
1147 if (status & 0x10 /*UART_MSR_CTS */ )
1148 GMStatus[i].cts = 1;
1150 GMStatus[i].cts = 0;
1152 if(copy_to_user((struct mxser_mstatus *) arg, GMStatus,
1153 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1157 return (-ENOIOCTLCMD);
1163 * This routine is called by the upper-layer tty layer to signal that
1164 * incoming characters should be throttled.
1166 static void mxser_throttle(struct tty_struct *tty)
1168 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1169 unsigned long flags;
1172 info->x_char = STOP_CHAR(tty);
1176 info->IER |= UART_IER_THRI;
1177 outb(info->IER, info->base + UART_IER); /* force Tx interrupt */
1178 restore_flags(flags);
1180 if (info->tty->termios->c_cflag & CRTSCTS) {
1181 info->MCR &= ~UART_MCR_RTS;
1184 outb(info->MCR, info->base + UART_MCR);
1185 restore_flags(flags);
1189 static void mxser_unthrottle(struct tty_struct *tty)
1191 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1192 unsigned long flags;
1198 info->x_char = START_CHAR(tty);
1202 info->IER |= UART_IER_THRI; /* force Tx interrupt */
1203 outb(info->IER, info->base + UART_IER);
1204 restore_flags(flags);
1207 if (info->tty->termios->c_cflag & CRTSCTS) {
1208 info->MCR |= UART_MCR_RTS;
1211 outb(info->MCR, info->base + UART_MCR);
1212 restore_flags(flags);
1216 static void mxser_set_termios(struct tty_struct *tty,
1217 struct termios *old_termios)
1219 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1221 /* 8-2-99 by William
1222 if ( (tty->termios->c_cflag == old_termios->c_cflag) &&
1223 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
1224 RELEVANT_IFLAG(old_termios->c_iflag)) )
1227 mxser_change_speed(info, old_termios);
1229 if ( (old_termios->c_cflag & CRTSCTS) &&
1230 !(tty->termios->c_cflag & CRTSCTS) ) {
1231 tty->hw_stopped = 0;
1235 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1236 (RELEVANT_IFLAG(tty->termios->c_iflag) !=
1237 RELEVANT_IFLAG(old_termios->c_iflag))) {
1239 mxser_change_speed(info, old_termios);
1241 if ((old_termios->c_cflag & CRTSCTS) &&
1242 !(tty->termios->c_cflag & CRTSCTS)) {
1243 tty->hw_stopped = 0;
1247 /* Handle sw stopped */
1248 if ((old_termios->c_iflag & IXON) &&
1249 !(tty->termios->c_iflag & IXON)) {
1256 * mxser_stop() and mxser_start()
1258 * This routines are called before setting or resetting tty->stopped.
1259 * They enable or disable transmitter interrupts, as necessary.
1261 static void mxser_stop(struct tty_struct *tty)
1263 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1264 unsigned long flags;
1268 if (info->IER & UART_IER_THRI) {
1269 info->IER &= ~UART_IER_THRI;
1270 outb(info->IER, info->base + UART_IER);
1272 restore_flags(flags);
1275 static void mxser_start(struct tty_struct *tty)
1277 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1278 unsigned long flags;
1282 if (info->xmit_cnt && info->xmit_buf &&
1283 !(info->IER & UART_IER_THRI)) {
1284 info->IER |= UART_IER_THRI;
1285 outb(info->IER, info->base + UART_IER);
1287 restore_flags(flags);
1291 * This routine is called by tty_hangup() when a hangup is signaled.
1293 void mxser_hangup(struct tty_struct *tty)
1295 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1297 mxser_flush_buffer(tty);
1298 mxser_shutdown(info);
1301 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1303 wake_up_interruptible(&info->open_wait);
1307 * This is the serial driver's generic interrupt routine
1309 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1312 struct mxser_struct *info;
1313 struct mxser_struct *port;
1314 int max, irqbits, bits, msr;
1315 int pass_counter = 0;
1319 for (i = 0; i < MXSER_BOARDS; i++) {
1320 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1326 if (i == MXSER_BOARDS)
1330 max = mxser_numports[mxsercfg[i].board_type];
1333 irqbits = inb(port->vector) & port->vectormask;
1334 if (irqbits == port->vectormask)
1337 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1338 if (irqbits == port->vectormask)
1344 (inb(info->base + UART_IIR) & UART_IIR_NO_INT))
1346 status = inb(info->base + UART_LSR) & info->read_status_mask;
1347 if (status & UART_LSR_DR)
1348 mxser_receive_chars(info, &status);
1349 msr = inb(info->base + UART_MSR);
1350 if (msr & UART_MSR_ANY_DELTA)
1351 mxser_check_modem_status(info, msr);
1352 if (status & UART_LSR_THRE) {
1353 /* 8-2-99 by William
1354 if ( info->x_char || (info->xmit_cnt > 0) )
1356 mxser_transmit_chars(info);
1359 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
1361 printk("MOXA Smartio/Indusrtio family driver interrupt loop break\n");
1363 break; /* Prevent infinite loops */
1366 return IRQ_RETVAL(handled);
1369 static inline void mxser_receive_chars(struct mxser_struct *info,
1372 struct tty_struct *tty = info->tty;
1378 ch = inb(info->base + UART_RX);
1379 if (*status & info->ignore_status_mask) {
1380 if (++ignored > 100)
1383 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1386 if (*status & UART_LSR_SPECIAL) {
1387 if (*status & UART_LSR_BI) {
1388 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
1389 if (info->flags & ASYNC_SAK)
1391 } else if (*status & UART_LSR_PE) {
1392 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
1393 } else if (*status & UART_LSR_FE) {
1394 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
1395 } else if (*status & UART_LSR_OE) {
1396 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
1398 *tty->flip.flag_buf_ptr++ = 0;
1400 *tty->flip.flag_buf_ptr++ = 0;
1401 *tty->flip.char_buf_ptr++ = ch;
1404 *status = inb(info->base + UART_LSR) & info->read_status_mask;
1405 } while (*status & UART_LSR_DR);
1406 mxvar_log.rxcnt[info->port] += cnt;
1407 schedule_delayed_work(&tty->flip.work, 1);
1411 static inline void mxser_transmit_chars(struct mxser_struct *info)
1416 outb(info->x_char, info->base + UART_TX);
1418 mxvar_log.txcnt[info->port]++;
1421 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
1422 info->tty->hw_stopped) {
1423 info->IER &= ~UART_IER_THRI;
1424 outb(info->IER, info->base + UART_IER);
1427 cnt = info->xmit_cnt;
1428 count = info->xmit_fifo_size;
1430 outb(info->xmit_buf[info->xmit_tail++], info->base + UART_TX);
1431 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1432 if (--info->xmit_cnt <= 0)
1434 } while (--count > 0);
1435 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
1437 if (info->xmit_cnt < WAKEUP_CHARS) {
1438 set_bit(MXSER_EVENT_TXLOW, &info->event);
1439 schedule_work(&info->tqueue);
1441 if (info->xmit_cnt <= 0) {
1442 info->IER &= ~UART_IER_THRI;
1443 outb(info->IER, info->base + UART_IER);
1447 static inline void mxser_check_modem_status(struct mxser_struct *info,
1451 /* update input line counters */
1452 if (status & UART_MSR_TERI)
1454 if (status & UART_MSR_DDSR)
1456 if (status & UART_MSR_DDCD)
1458 if (status & UART_MSR_DCTS)
1460 wake_up_interruptible(&info->delta_msr_wait);
1462 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
1463 if (status & UART_MSR_DCD)
1464 wake_up_interruptible(&info->open_wait);
1466 set_bit(MXSER_EVENT_HANGUP, &info->event);
1467 schedule_work(&info->tqueue);
1469 if (info->flags & ASYNC_CTS_FLOW) {
1470 if (info->tty->hw_stopped) {
1471 if (status & UART_MSR_CTS) {
1472 info->tty->hw_stopped = 0;
1473 info->IER |= UART_IER_THRI;
1474 outb(info->IER, info->base + UART_IER);
1476 set_bit(MXSER_EVENT_TXLOW, &info->event);
1477 schedule_work(&info->tqueue);
1480 if (!(status & UART_MSR_CTS)) {
1481 info->tty->hw_stopped = 1;
1482 info->IER &= ~UART_IER_THRI;
1483 outb(info->IER, info->base + UART_IER);
1489 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
1490 struct mxser_struct *info)
1492 DECLARE_WAITQUEUE(wait, current);
1493 unsigned long flags;
1498 * If the device is in the middle of being closed, then block
1499 * until it's done, and then try again.
1501 if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
1502 if (info->flags & ASYNC_CLOSING)
1503 interruptible_sleep_on(&info->close_wait);
1504 #ifdef SERIAL_DO_RESTART
1505 if (info->flags & ASYNC_HUP_NOTIFY)
1508 return (-ERESTARTSYS);
1514 * If non-blocking mode is set, or the port is not enabled,
1515 * then make the check up front and then exit.
1517 if ((filp->f_flags & O_NONBLOCK) ||
1518 (tty->flags & (1 << TTY_IO_ERROR))) {
1519 info->flags |= ASYNC_NORMAL_ACTIVE;
1522 if (tty->termios->c_cflag & CLOCAL)
1526 * Block waiting for the carrier detect and the line to become
1527 * free (i.e., not in use by the callout). While we are in
1528 * this loop, info->count is dropped by one, so that
1529 * mxser_close() knows when to free things. We restore it upon
1530 * exit, either normal or abnormal.
1533 add_wait_queue(&info->open_wait, &wait);
1536 if (!tty_hung_up_p(filp))
1538 restore_flags(flags);
1539 info->blocked_open++;
1543 outb(inb(info->base + UART_MCR) | UART_MCR_DTR | UART_MCR_RTS,
1544 info->base + UART_MCR);
1545 restore_flags(flags);
1546 set_current_state(TASK_INTERRUPTIBLE);
1547 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
1548 #ifdef SERIAL_DO_RESTART
1549 if (info->flags & ASYNC_HUP_NOTIFY)
1552 retval = -ERESTARTSYS;
1558 if (!(info->flags & ASYNC_CLOSING) &&
1559 (do_clocal || (inb(info->base + UART_MSR) & UART_MSR_DCD)))
1561 if (signal_pending(current)) {
1562 retval = -ERESTARTSYS;
1567 set_current_state(TASK_RUNNING);
1568 remove_wait_queue(&info->open_wait, &wait);
1569 if (!tty_hung_up_p(filp))
1571 info->blocked_open--;
1574 info->flags |= ASYNC_NORMAL_ACTIVE;
1578 static int mxser_startup(struct mxser_struct *info)
1580 unsigned long flags;
1583 page = get_zeroed_page(GFP_KERNEL);
1590 if (info->flags & ASYNC_INITIALIZED) {
1592 restore_flags(flags);
1595 if (!info->base || !info->type) {
1597 set_bit(TTY_IO_ERROR, &info->tty->flags);
1599 restore_flags(flags);
1605 info->xmit_buf = (unsigned char *) page;
1608 * Clear the FIFO buffers and disable them
1609 * (they will be reenabled in mxser_change_speed())
1611 if (info->xmit_fifo_size == 16)
1612 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1613 info->base + UART_FCR);
1616 * At this point there's no way the LSR could still be 0xFF;
1617 * if it is, then bail out, because there's likely no UART
1620 if (inb(info->base + UART_LSR) == 0xff) {
1621 restore_flags(flags);
1622 if (capable(CAP_SYS_ADMIN)) {
1624 set_bit(TTY_IO_ERROR, &info->tty->flags);
1630 * Clear the interrupt registers.
1632 (void) inb(info->base + UART_LSR);
1633 (void) inb(info->base + UART_RX);
1634 (void) inb(info->base + UART_IIR);
1635 (void) inb(info->base + UART_MSR);
1638 * Now, initialize the UART
1640 outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
1641 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
1642 outb(info->MCR, info->base + UART_MCR);
1645 * Finally, enable interrupts
1647 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
1648 outb(info->IER, info->base + UART_IER); /* enable interrupts */
1651 * And clear the interrupt registers again for luck.
1653 (void) inb(info->base + UART_LSR);
1654 (void) inb(info->base + UART_RX);
1655 (void) inb(info->base + UART_IIR);
1656 (void) inb(info->base + UART_MSR);
1659 test_and_clear_bit(TTY_IO_ERROR, &info->tty->flags);
1661 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1664 * and set the speed of the serial port
1666 mxser_change_speed(info, 0);
1668 info->flags |= ASYNC_INITIALIZED;
1669 restore_flags(flags);
1674 * This routine will shutdown a serial port; interrupts maybe disabled, and
1675 * DTR is dropped if the hangup on close termio flag is on.
1677 static void mxser_shutdown(struct mxser_struct *info)
1679 unsigned long flags;
1681 if (!(info->flags & ASYNC_INITIALIZED))
1685 cli(); /* Disable interrupts */
1688 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1689 * here so the queue might never be waken up
1691 wake_up_interruptible(&info->delta_msr_wait);
1694 * Free the IRQ, if necessary
1696 if (info->xmit_buf) {
1697 free_page((unsigned long) info->xmit_buf);
1701 outb(0x00, info->base + UART_IER); /* disable all intrs */
1703 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1704 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1705 outb(info->MCR, info->base + UART_MCR);
1707 /* clear Rx/Tx FIFO's */
1708 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
1709 /* read data port to reset things */
1710 (void) inb(info->base + UART_RX);
1713 set_bit(TTY_IO_ERROR, &info->tty->flags);
1715 info->flags &= ~ASYNC_INITIALIZED;
1716 restore_flags(flags);
1720 * This routine is called to set the UART divisor registers to match
1721 * the specified baud rate for a serial port.
1723 static int mxser_change_speed(struct mxser_struct *info,
1724 struct termios *old_termios)
1727 unsigned cflag, cval, fcr;
1730 unsigned long flags;
1732 if (!info->tty || !info->tty->termios)
1734 cflag = info->tty->termios->c_cflag;
1739 #define B921600 (B460800 +1)
1741 switch (cflag & (CBAUD | CBAUDEX)) {
1808 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1809 i = 16; /* 57600 bps */
1810 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1811 i = 17; /* 115200 bps */
1813 #ifdef ASYNC_SPD_SHI
1814 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1818 #ifdef ASYNC_SPD_WARP
1819 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1823 if (mxvar_baud_table[i] == 134) {
1824 quot = (2 * info->baud_base / 269);
1825 } else if (mxvar_baud_table[i]) {
1826 quot = info->baud_base / mxvar_baud_table[i];
1827 if (!quot && old_termios) {
1829 info->tty->termios->c_cflag &= ~CBAUD;
1830 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1831 switch (info->tty->termios->c_cflag & (CBAUD | CBAUDEX)) {
1897 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1898 i = 16; /* 57600 bps */
1899 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1900 i = 17; /* 115200 bps */
1901 #ifdef ASYNC_SPD_SHI
1902 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1905 #ifdef ASYNC_SPD_WARP
1906 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1910 if (mxvar_baud_table[i] == 134) {
1911 quot = (2 * info->baud_base / 269);
1912 } else if (mxvar_baud_table[i]) {
1913 quot = info->baud_base / mxvar_baud_table[i];
1919 } else if (quot == 0)
1926 info->MCR |= UART_MCR_DTR;
1929 outb(info->MCR, info->base + UART_MCR);
1930 restore_flags(flags);
1932 info->MCR &= ~UART_MCR_DTR;
1935 outb(info->MCR, info->base + UART_MCR);
1936 restore_flags(flags);
1939 /* byte size and parity */
1940 switch (cflag & CSIZE) {
1955 break; /* too keep GCC shut... */
1960 cval |= UART_LCR_PARITY;
1961 if (!(cflag & PARODD))
1962 cval |= UART_LCR_EPAR;
1963 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
1966 fcr = UART_FCR_ENABLE_FIFO;
1967 switch (info->rx_trigger) {
1969 fcr |= UART_FCR_TRIGGER_1;
1972 fcr |= UART_FCR_TRIGGER_4;
1975 fcr |= UART_FCR_TRIGGER_8;
1978 fcr |= UART_FCR_TRIGGER_14;
1982 /* CTS flow control flag and modem status interrupts */
1983 info->IER &= ~UART_IER_MSI;
1984 info->MCR &= ~UART_MCR_AFE;
1985 if (cflag & CRTSCTS) {
1986 info->flags |= ASYNC_CTS_FLOW;
1987 info->IER |= UART_IER_MSI;
1988 if (info->type == PORT_16550A)
1989 info->MCR |= UART_MCR_AFE;
1991 info->flags &= ~ASYNC_CTS_FLOW;
1993 outb(info->MCR, info->base + UART_MCR);
1995 info->flags &= ~ASYNC_CHECK_CD;
1997 info->flags |= ASYNC_CHECK_CD;
1998 info->IER |= UART_IER_MSI;
2000 outb(info->IER, info->base + UART_IER);
2003 * Set up parity check flag
2005 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2006 if (I_INPCK(info->tty))
2007 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2008 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2009 info->read_status_mask |= UART_LSR_BI;
2011 info->ignore_status_mask = 0;
2013 /* This should be safe, but for some broken bits of hardware... */
2014 if (I_IGNPAR(info->tty)) {
2015 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2016 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
2019 if (I_IGNBRK(info->tty)) {
2020 info->ignore_status_mask |= UART_LSR_BI;
2021 info->read_status_mask |= UART_LSR_BI;
2023 * If we're ignore parity and break indicators, ignore
2024 * overruns too. (For real raw support).
2026 if (I_IGNPAR(info->tty)) {
2027 info->ignore_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2028 info->read_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2033 outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2034 outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2035 outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2036 outb(cval, info->base + UART_LCR); /* reset DLAB */
2037 outb(fcr, info->base + UART_FCR); /* set fcr */
2038 restore_flags(flags);
2044 * ------------------------------------------------------------
2045 * friends of mxser_ioctl()
2046 * ------------------------------------------------------------
2048 static int mxser_get_serial_info(struct mxser_struct *info,
2049 struct serial_struct *retinfo)
2051 struct serial_struct tmp;
2055 memset(&tmp, 0, sizeof(tmp));
2056 tmp.type = info->type;
2057 tmp.line = info->port;
2058 tmp.port = info->base;
2059 tmp.irq = info->irq;
2060 tmp.flags = info->flags;
2061 tmp.baud_base = info->baud_base;
2062 tmp.close_delay = info->close_delay;
2063 tmp.closing_wait = info->closing_wait;
2064 tmp.custom_divisor = info->custom_divisor;
2066 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2069 static int mxser_set_serial_info(struct mxser_struct *info,
2070 struct serial_struct *new_info)
2072 struct serial_struct new_serial;
2076 if (!new_info || !info->base)
2078 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2081 if ((new_serial.irq != info->irq) ||
2082 (new_serial.port != info->base) ||
2083 (new_serial.type != info->type) ||
2084 (new_serial.custom_divisor != info->custom_divisor) ||
2085 (new_serial.baud_base != info->baud_base))
2088 flags = info->flags & ASYNC_SPD_MASK;
2090 if (!capable(CAP_SYS_ADMIN)) {
2091 if ((new_serial.baud_base != info->baud_base) ||
2092 (new_serial.close_delay != info->close_delay) ||
2093 ((new_serial.flags & ~ASYNC_USR_MASK) !=
2094 (info->flags & ~ASYNC_USR_MASK)))
2096 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2097 (new_serial.flags & ASYNC_USR_MASK));
2100 * OK, past this point, all the error checking has been done.
2101 * At this point, we start making changes.....
2103 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2104 (new_serial.flags & ASYNC_FLAGS));
2105 info->close_delay = new_serial.close_delay * HZ / 100;
2106 info->closing_wait = new_serial.closing_wait * HZ / 100;
2109 if (info->flags & ASYNC_INITIALIZED) {
2110 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2111 mxser_change_speed(info, 0);
2114 retval = mxser_startup(info);
2119 * mxser_get_lsr_info - get line status register info
2121 * Purpose: Let user call ioctl() to get info when the UART physically
2122 * is emptied. On bus types like RS485, the transmitter must
2123 * release the bus after transmitting. This must be done when
2124 * the transmit shift register is empty, not be done when the
2125 * transmit holding register is empty. This functionality
2126 * allows an RS485 driver to be written in user space.
2128 static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int *value)
2130 unsigned char status;
2131 unsigned int result;
2132 unsigned long flags;
2136 status = inb(info->base + UART_LSR);
2137 restore_flags(flags);
2138 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2139 return put_user(result, value);
2143 * This routine sends a break character out the serial port.
2145 static void mxser_send_break(struct mxser_struct *info, int duration)
2147 unsigned long flags;
2150 set_current_state(TASK_INTERRUPTIBLE);
2153 outb(inb(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
2154 schedule_timeout(duration);
2155 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
2156 restore_flags(flags);
2159 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2161 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
2162 unsigned char control, status;
2163 unsigned long flags;
2165 if (PORTNO(tty) == MXSER_PORTS)
2166 return (-ENOIOCTLCMD);
2167 if (tty->flags & (1 << TTY_IO_ERROR))
2170 control = info->MCR;
2173 status = inb(info->base + UART_MSR);
2174 if (status & UART_MSR_ANY_DELTA)
2175 mxser_check_modem_status(info, status);
2176 restore_flags(flags);
2177 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2178 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2179 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2180 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2181 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2182 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2185 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
2186 unsigned int set, unsigned int clear)
2188 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
2189 unsigned long flags;
2191 if (PORTNO(tty) == MXSER_PORTS)
2192 return (-ENOIOCTLCMD);
2193 if (tty->flags & (1 << TTY_IO_ERROR))
2198 if (set & TIOCM_RTS)
2199 info->MCR |= UART_MCR_RTS;
2200 if (set & TIOCM_DTR)
2201 info->MCR |= UART_MCR_DTR;
2203 if (clear & TIOCM_RTS)
2204 info->MCR &= ~UART_MCR_RTS;
2205 if (clear & TIOCM_DTR)
2206 info->MCR &= ~UART_MCR_DTR;
2208 outb(info->MCR, info->base + UART_MCR);
2209 restore_flags(flags);
2213 static int mxser_read_register(int, unsigned short *);
2214 static int mxser_program_mode(int);
2215 static void mxser_normal_mode(int);
2217 static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2220 unsigned short regs[16], irq;
2221 unsigned char scratch, scratch2;
2223 id = mxser_read_register(cap, regs);
2224 if (id == C168_ASIC_ID)
2225 hwconf->board_type = MXSER_BOARD_C168_ISA;
2226 else if (id == C104_ASIC_ID)
2227 hwconf->board_type = MXSER_BOARD_C104_ISA;
2228 else if (id == C102_ASIC_ID)
2229 hwconf->board_type = MXSER_BOARD_C102_ISA;
2230 else if (id == CI132_ASIC_ID)
2231 hwconf->board_type = MXSER_BOARD_CI132;
2232 else if (id == CI134_ASIC_ID)
2233 hwconf->board_type = MXSER_BOARD_CI134;
2234 else if (id == CI104J_ASIC_ID)
2235 hwconf->board_type = MXSER_BOARD_CI104J;
2238 irq = regs[9] & 0x0F;
2239 irq = irq | (irq << 4);
2240 irq = irq | (irq << 8);
2241 if ((irq != regs[9]) || ((id == 1) && (irq != regs[10]))) {
2242 return (MXSER_ERR_IRQ_CONFLIT);
2245 return (MXSER_ERR_IRQ);
2247 for (i = 0; i < 8; i++)
2248 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
2249 hwconf->irq = (int) (irq & 0x0F);
2250 if ((regs[12] & 0x80) == 0) {
2251 return (MXSER_ERR_VECTOR);
2253 hwconf->vector = (int) regs[11]; /* interrupt vector */
2255 hwconf->vector_mask = 0x00FF;
2257 hwconf->vector_mask = 0x000F;
2258 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2259 if (regs[12] & bits)
2260 hwconf->baud_base[i] = 921600;
2262 hwconf->baud_base[i] = 115200;
2264 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2265 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2266 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2267 outb(scratch2, cap + UART_LCR);
2268 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2269 scratch = inb(cap + UART_IIR);
2271 hwconf->uart_type = PORT_16550A;
2273 hwconf->uart_type = PORT_16450;
2278 return (hwconf->ports);
2281 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
2282 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
2283 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
2284 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
2285 #define EN_CCMD 0x000 /* Chip's command register */
2286 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
2287 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
2288 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
2289 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
2290 #define EN0_DCFG 0x00E /* Data configuration reg WR */
2291 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
2292 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
2293 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
2294 static int mxser_read_register(int port, unsigned short *regs)
2296 int i, k, value, id;
2299 id = mxser_program_mode(port);
2302 for (i = 0; i < 14; i++) {
2303 k = (i & 0x3F) | 0x180;
2304 for (j = 0x100; j > 0; j >>= 1) {
2305 outb(CHIP_CS, port);
2307 outb(CHIP_CS | CHIP_DO, port);
2308 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
2310 outb(CHIP_CS, port);
2311 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
2316 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
2317 outb(CHIP_CS, port);
2318 outb(CHIP_CS | CHIP_SK, port);
2319 if (inb(port) & CHIP_DI)
2325 mxser_normal_mode(port);
2329 static int mxser_program_mode(int port)
2332 unsigned long flags;
2343 restore_flags(flags);
2344 id = inb(port + 1) & 0x1F;
2345 if ((id != C168_ASIC_ID) && (id != C104_ASIC_ID) && (id != CI104J_ASIC_ID) &&
2346 (id != C102_ASIC_ID) && (id != CI132_ASIC_ID) && (id != CI134_ASIC_ID))
2348 for (i = 0, j = 0; i < 4; i++) {
2352 } else if ((j == 1) && (n == 1)) {
2363 static void mxser_normal_mode(int port)
2367 outb(0xA5, port + 1);
2368 outb(0x80, port + 3);
2369 outb(12, port + 0); /* 9600 bps */
2371 outb(0x03, port + 3); /* 8 data bits */
2372 outb(0x13, port + 4); /* loop back mode */
2373 for (i = 0; i < 16; i++) {
2375 if ((n & 0x61) == 0x60)
2380 outb(0x00, port + 4);
2383 module_init(mxser_module_init);
2384 module_exit(mxser_module_exit);