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)
202 static struct pci_device_id mxser_pcibrds[] = {
203 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C168, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
204 MXSER_BOARD_C168_PCI },
205 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C104, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
206 MXSER_BOARD_C104_PCI },
207 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP104, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
208 MXSER_BOARD_CP104_PCI },
209 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP132, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
210 MXSER_BOARD_CP132_PCI },
211 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP114, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
212 MXSER_BOARD_CP114_PCI },
213 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CT114, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
214 MXSER_BOARD_CT114_PCI },
217 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
218 #endif /* CONFIG_PCI */
220 static int ioaddr[MXSER_BOARDS];
221 static int ttymajor = MXSERMAJOR;
224 /* Variables for insmod */
226 MODULE_AUTHOR("William Chen");
227 MODULE_DESCRIPTION("MOXA Smartio Family Multiport Board Device Driver");
228 MODULE_LICENSE("GPL");
229 MODULE_PARM(ioaddr, "1-4i");
230 MODULE_PARM(ttymajor, "i");
231 MODULE_PARM(verbose, "i");
233 struct mxser_hwconf {
240 int ioaddr[MXSER_PORTS_PER_BOARD];
241 int baud_base[MXSER_PORTS_PER_BOARD];
242 struct pci_dev *pdev;
245 struct mxser_struct {
247 int base; /* port base address */
248 int irq; /* port using irq no. */
249 int vector; /* port irq vector */
250 int vectormask; /* port vector mask */
251 int rx_trigger; /* Rx fifo trigger level */
252 int baud_base; /* max. speed */
253 int flags; /* defined in tty.h */
254 int type; /* UART type */
255 struct tty_struct *tty;
256 int read_status_mask;
257 int ignore_status_mask;
260 int x_char; /* xon/xoff character */
262 unsigned short closing_wait;
263 int IER; /* Interrupt Enable Register */
264 int MCR; /* Modem control register */
266 int count; /* # of fd on device */
267 int blocked_open; /* # of blocked opens */
268 unsigned char *xmit_buf;
272 struct work_struct tqueue;
274 wait_queue_head_t open_wait;
275 wait_queue_head_t close_wait;
276 wait_queue_head_t delta_msr_wait;
277 struct async_icount icount; /* kernel counters for the 4 input interrupts */
282 int rxcnt[MXSER_PORTS];
283 int txcnt[MXSER_PORTS];
286 struct mxser_mstatus {
294 static struct mxser_mstatus GMStatus[MXSER_PORTS];
296 static int mxserBoardCAP[MXSER_BOARDS] =
299 /* 0x180, 0x280, 0x200, 0x320 */
303 static struct tty_driver *mxvar_sdriver;
304 static struct mxser_struct mxvar_table[MXSER_PORTS];
305 static struct mxser_log mxvar_log;
306 static int mxvar_diagflag;
308 * mxvar_tmp_buf is used as a temporary buffer by serial_write. We need
309 * to lock it in case the memcpy_fromfs blocks while swapping in a page,
310 * and some other program tries to do a serial write at the same time.
311 * Since the lock will only come under contention when the system is
312 * swapping and available memory is low, it makes sense to share one
313 * buffer across all the serial ports, since it significantly saves
314 * memory if large numbers of serial ports are open.
316 static unsigned char *mxvar_tmp_buf;
317 static struct semaphore mxvar_tmp_buf_sem;
320 * This is used to figure out the divisor speeds and the timeouts
322 static int mxvar_baud_table[] =
324 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
325 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 0};
327 struct mxser_hwconf mxsercfg[MXSER_BOARDS];
333 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
334 static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
335 static void mxser_do_softint(void *);
336 static int mxser_open(struct tty_struct *, struct file *);
337 static void mxser_close(struct tty_struct *, struct file *);
338 static int mxser_write(struct tty_struct *, int, const unsigned char *, int);
339 static int mxser_write_room(struct tty_struct *);
340 static void mxser_flush_buffer(struct tty_struct *);
341 static int mxser_chars_in_buffer(struct tty_struct *);
342 static void mxser_flush_chars(struct tty_struct *);
343 static void mxser_put_char(struct tty_struct *, unsigned char);
344 static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
345 static int mxser_ioctl_special(unsigned int, unsigned long);
346 static void mxser_throttle(struct tty_struct *);
347 static void mxser_unthrottle(struct tty_struct *);
348 static void mxser_set_termios(struct tty_struct *, struct termios *);
349 static void mxser_stop(struct tty_struct *);
350 static void mxser_start(struct tty_struct *);
351 static void mxser_hangup(struct tty_struct *);
352 static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
353 static inline void mxser_receive_chars(struct mxser_struct *, int *);
354 static inline void mxser_transmit_chars(struct mxser_struct *);
355 static inline void mxser_check_modem_status(struct mxser_struct *, int);
356 static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
357 static int mxser_startup(struct mxser_struct *);
358 static void mxser_shutdown(struct mxser_struct *);
359 static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
360 static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct __user *);
361 static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct __user *);
362 static int mxser_get_lsr_info(struct mxser_struct *, unsigned int __user *);
363 static void mxser_send_break(struct mxser_struct *, int);
364 static int mxser_tiocmget(struct tty_struct *, struct file *);
365 static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
368 * The MOXA C168/C104 serial driver boot-time initialization code!
371 static void __exit mxser_module_exit(void)
377 printk("Unloading module mxser ...\n");
378 if ((err |= tty_unregister_driver(mxvar_sdriver)))
379 printk("Couldn't unregister MOXA Smartio family serial driver\n");
380 put_tty_driver(mxvar_sdriver);
382 for (i = 0; i < MXSER_BOARDS; i++) {
383 if (mxsercfg[i].board_type == -1)
386 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
395 int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
397 struct mxser_struct *info;
402 init_MUTEX(&mxvar_tmp_buf_sem);
404 n = board * MXSER_PORTS_PER_BOARD;
405 info = &mxvar_table[n];
406 for (i = 0; i < hwconf->ports; i++, n++, info++) {
408 printk(" ttyM%d/cum%d at 0x%04x ", n, n, hwconf->ioaddr[i]);
409 if (hwconf->baud_base[i] == 115200)
410 printk(" max. baud rate up to 115200 bps.\n");
412 printk(" max. baud rate up to 921600 bps.\n");
415 info->base = hwconf->ioaddr[i];
416 info->irq = hwconf->irq;
417 info->vector = hwconf->vector;
418 info->vectormask = hwconf->vector_mask;
419 info->rx_trigger = 14;
420 info->baud_base = hwconf->baud_base[i];
421 info->flags = ASYNC_SHARE_IRQ;
422 info->type = hwconf->uart_type;
423 if ((info->type == PORT_16450) || (info->type == PORT_8250))
424 info->xmit_fifo_size = 1;
426 info->xmit_fifo_size = 16;
427 info->custom_divisor = hwconf->baud_base[i] * 16;
428 info->close_delay = 5 * HZ / 10;
429 info->closing_wait = 30 * HZ;
430 INIT_WORK(&info->tqueue, mxser_do_softint, info);
431 info->cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
432 init_waitqueue_head(&info->open_wait);
433 init_waitqueue_head(&info->close_wait);
434 init_waitqueue_head(&info->delta_msr_wait);
438 * Allocate the IRQ if necessary
442 n = board * MXSER_PORTS_PER_BOARD;
443 info = &mxvar_table[n];
446 retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
449 restore_flags(flags);
450 printk("Board %d: %s", board, mxser_brdname[hwconf->board_type]);
451 printk(" Request irq fail,IRQ (%d) may be conflit with another device.\n", info->irq);
454 restore_flags(flags);
460 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
462 mxsercfg[board] = *hwconf;
466 static int mxser_get_PCI_conf(struct pci_dev *pdev, int board_type, struct mxser_hwconf *hwconf)
469 unsigned int ioaddress;
471 hwconf->board_type = board_type;
472 hwconf->ports = mxser_numports[board_type];
473 ioaddress = pci_resource_start (pdev, 2);
474 for (i = 0; i < hwconf->ports; i++)
475 hwconf->ioaddr[i] = ioaddress + 8 * i;
477 ioaddress = pci_resource_start (pdev, 3);
478 hwconf->vector = ioaddress;
480 hwconf->irq = pdev->irq;
482 hwconf->uart_type = PORT_16550A;
483 hwconf->vector_mask = 0;
484 for (i = 0; i < hwconf->ports; i++) {
485 hwconf->vector_mask |= (1 << i);
486 hwconf->baud_base[i] = 921600;
490 #endif /* CONFIG_PCI */
492 static struct tty_operations mxser_ops = {
494 .close = mxser_close,
495 .write = mxser_write,
496 .put_char = mxser_put_char,
497 .flush_chars = mxser_flush_chars,
498 .write_room = mxser_write_room,
499 .chars_in_buffer = mxser_chars_in_buffer,
500 .flush_buffer = mxser_flush_buffer,
501 .ioctl = mxser_ioctl,
502 .throttle = mxser_throttle,
503 .unthrottle = mxser_unthrottle,
504 .set_termios = mxser_set_termios,
506 .start = mxser_start,
507 .hangup = mxser_hangup,
508 .tiocmget = mxser_tiocmget,
509 .tiocmset = mxser_tiocmset,
512 static int __init mxser_module_init(void)
515 struct mxser_hwconf hwconf;
517 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
521 printk("MOXA Smartio family driver version %s\n", MXSER_VERSION);
523 /* Initialize the tty_driver structure */
525 mxvar_sdriver->owner = THIS_MODULE;
526 mxvar_sdriver->name = "ttyM";
527 mxvar_sdriver->devfs_name = "tts/M";
528 mxvar_sdriver->major = ttymajor;
529 mxvar_sdriver->minor_start = 0;
530 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
531 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
532 mxvar_sdriver->init_termios = tty_std_termios;
533 mxvar_sdriver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
534 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
535 tty_set_operations(mxvar_sdriver, &mxser_ops);
536 printk("Tty devices major number = %d\n", ttymajor);
539 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
540 memset(&mxvar_log, 0, sizeof(struct mxser_log));
544 /* Start finding ISA boards here */
545 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
547 if (!(cap = mxserBoardCAP[b]))
550 retval = mxser_get_ISA_conf(cap, &hwconf);
553 printk("Found MOXA %s board (CAP=0x%x)\n",
554 mxser_brdname[hwconf.board_type],
558 if (retval == MXSER_ERR_IRQ)
559 printk("Invalid interrupt number,board not configured\n");
560 else if (retval == MXSER_ERR_IRQ_CONFLIT)
561 printk("Invalid interrupt number,board not configured\n");
562 else if (retval == MXSER_ERR_VECTOR)
563 printk("Invalid interrupt vector,board not configured\n");
564 else if (retval == MXSER_ERR_IOADDR)
565 printk("Invalid I/O address,board not configured\n");
571 if (mxser_initbrd(m, &hwconf) < 0)
574 mxser_getcfg(m, &hwconf);
579 /* Start finding ISA boards from module arg */
580 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
582 if (!(cap = ioaddr[b]))
585 retval = mxser_get_ISA_conf(cap, &hwconf);
588 printk("Found MOXA %s board (CAP=0x%x)\n",
589 mxser_brdname[hwconf.board_type],
593 if (retval == MXSER_ERR_IRQ)
594 printk("Invalid interrupt number,board not configured\n");
595 else if (retval == MXSER_ERR_IRQ_CONFLIT)
596 printk("Invalid interrupt number,board not configured\n");
597 else if (retval == MXSER_ERR_VECTOR)
598 printk("Invalid interrupt vector,board not configured\n");
599 else if (retval == MXSER_ERR_IOADDR)
600 printk("Invalid I/O address,board not configured\n");
606 if (mxser_initbrd(m, &hwconf) < 0)
609 mxser_getcfg(m, &hwconf);
614 /* start finding PCI board here */
618 struct pci_dev *pdev = NULL;
619 int n = (sizeof(mxser_pcibrds) / sizeof(mxser_pcibrds[0])) - 1;
620 for (b = 0; b < n; b++) {
621 while ((pdev = pci_find_device(mxser_pcibrds[b].vendor, mxser_pcibrds[b].device, pdev)))
623 if (pci_enable_device(pdev))
626 printk("Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
627 mxser_brdname[mxser_pcibrds[b].driver_data],
628 pdev->bus->number, PCI_SLOT(pdev->devfn));
629 if (m >= MXSER_BOARDS) {
630 printk("Too many Smartio family boards found (maximum %d),board not configured\n", MXSER_BOARDS);
632 retval = mxser_get_PCI_conf(pdev, mxser_pcibrds[b].driver_data, &hwconf);
634 if (retval == MXSER_ERR_IRQ)
635 printk("Invalid interrupt number,board not configured\n");
636 else if (retval == MXSER_ERR_IRQ_CONFLIT)
637 printk("Invalid interrupt number,board not configured\n");
638 else if (retval == MXSER_ERR_VECTOR)
639 printk("Invalid interrupt vector,board not configured\n");
640 else if (retval == MXSER_ERR_IOADDR)
641 printk("Invalid I/O address,board not configured\n");
644 if (mxser_initbrd(m, &hwconf) < 0)
646 mxser_getcfg(m, &hwconf);
654 for (i = m; i < MXSER_BOARDS; i++) {
655 mxsercfg[i].board_type = -1;
659 if (!tty_register_driver(mxvar_sdriver))
662 put_tty_driver(mxvar_sdriver);
663 printk("Couldn't install MOXA Smartio family driver !\n");
665 for (i = 0; i < MXSER_BOARDS; i++) {
666 if (mxsercfg[i].board_type == -1)
668 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
673 static void mxser_do_softint(void *private_)
675 struct mxser_struct *info = (struct mxser_struct *) private_;
676 struct tty_struct *tty;
680 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event)) {
681 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
682 tty->ldisc.write_wakeup)
683 (tty->ldisc.write_wakeup) (tty);
684 wake_up_interruptible(&tty->write_wait);
686 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event)) {
687 tty_hangup(tty); /* FIXME: module removal race here - AKPM */
693 * This routine is called whenever a serial port is opened. It
694 * enables interrupts for a serial port, linking in its async structure into
695 * the IRQ chain. It also performs the serial-specific
696 * initialization for the tty structure.
699 static int mxser_open(struct tty_struct *tty, struct file *filp)
701 struct mxser_struct *info;
706 if (line == MXSER_PORTS)
708 if ((line < 0) || (line > MXSER_PORTS))
710 info = mxvar_table + line;
715 tty->driver_data = info;
718 if (!mxvar_tmp_buf) {
719 page = get_zeroed_page(GFP_KERNEL);
725 mxvar_tmp_buf = (unsigned char *) page;
728 * Start up serial port
730 retval = mxser_startup(info);
734 return mxser_block_til_ready(tty, filp, info);
738 * This routine is called when the serial port gets closed. First, we
739 * wait for the last remaining data to be sent. Then, we unlink its
740 * async structure from the interrupt chain if necessary, and we free
741 * that IRQ if nothing is left in the chain.
744 static void mxser_close(struct tty_struct *tty, struct file *filp)
746 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
748 unsigned long timeout;
750 if (PORTNO(tty) == MXSER_PORTS)
758 if (tty_hung_up_p(filp)) {
759 restore_flags(flags);
762 if ((tty->count == 1) && (info->count != 1)) {
764 * Uh, oh. tty->count is 1, which means that the tty
765 * structure will be freed. Info->count should always
766 * be one in these conditions. If it's greater than
767 * one, we've got real problems, since it means the
768 * serial port won't be shutdown.
770 printk("mxser_close: bad serial port count; tty->count is 1, "
771 "info->count is %d\n", info->count);
774 if (--info->count < 0) {
775 printk("mxser_close: bad serial port count for ttys%d: %d\n",
776 info->port, info->count);
780 restore_flags(flags);
783 info->flags |= ASYNC_CLOSING;
784 info->cflag = tty->termios->c_cflag;
786 * Now we wait for the transmit buffer to clear; and we notify
787 * the line discipline to only process XON/XOFF characters.
790 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
791 tty_wait_until_sent(tty, info->closing_wait);
793 * At this point we stop accepting input. To do this, we
794 * disable the receive line status interrupts, and tell the
795 * interrupt driver to stop checking the data ready bit in the
796 * line status register.
798 info->IER &= ~UART_IER_RLSI;
800 info->read_status_mask &= ~UART_LSR_DR;
802 if (info->flags & ASYNC_INITIALIZED) {
803 outb(info->IER, info->base + UART_IER);
805 * Before we drop DTR, make sure the UART transmitter
806 * has completely drained; this is especially
807 * important if there is a transmit FIFO!
809 timeout = jiffies + HZ;
810 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
811 set_current_state(TASK_INTERRUPTIBLE);
813 if (time_after(jiffies, timeout))
817 mxser_shutdown(info);
818 if (tty->driver->flush_buffer)
819 tty->driver->flush_buffer(tty);
820 if (tty->ldisc.flush_buffer)
821 tty->ldisc.flush_buffer(tty);
825 if (info->blocked_open) {
826 if (info->close_delay) {
827 set_current_state(TASK_INTERRUPTIBLE);
828 schedule_timeout(info->close_delay);
830 wake_up_interruptible(&info->open_wait);
832 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
833 wake_up_interruptible(&info->close_wait);
834 restore_flags(flags);
838 static int mxser_write(struct tty_struct *tty, int from_user,
839 const unsigned char *buf, int count)
842 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
845 if (!tty || !info->xmit_buf || !mxvar_tmp_buf)
850 down(&mxvar_tmp_buf_sem);
852 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
853 SERIAL_XMIT_SIZE - info->xmit_head));
857 c -= copy_from_user(mxvar_tmp_buf, buf, c);
865 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
866 SERIAL_XMIT_SIZE - info->xmit_head));
867 memcpy(info->xmit_buf + info->xmit_head, mxvar_tmp_buf, c);
868 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
870 restore_flags(flags);
876 up(&mxvar_tmp_buf_sem);
880 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
881 SERIAL_XMIT_SIZE - info->xmit_head));
883 restore_flags(flags);
887 memcpy(info->xmit_buf + info->xmit_head, buf, c);
888 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
890 restore_flags(flags);
899 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
900 !(info->IER & UART_IER_THRI)) {
901 info->IER |= UART_IER_THRI;
902 outb(info->IER, info->base + UART_IER);
904 restore_flags(flags);
908 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
910 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
913 if (!tty || !info->xmit_buf)
918 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
919 restore_flags(flags);
922 info->xmit_buf[info->xmit_head++] = ch;
923 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
925 /********************************************** why ??? ***********
926 if ( !tty->stopped && !tty->hw_stopped &&
927 !(info->IER & UART_IER_THRI) ) {
928 info->IER |= UART_IER_THRI;
929 outb(info->IER, info->base + UART_IER);
931 *****************************************************************/
932 restore_flags(flags);
935 static void mxser_flush_chars(struct tty_struct *tty)
937 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
940 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
946 info->IER |= UART_IER_THRI;
947 outb(info->IER, info->base + UART_IER);
948 restore_flags(flags);
951 static int mxser_write_room(struct tty_struct *tty)
953 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
956 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
962 static int mxser_chars_in_buffer(struct tty_struct *tty)
964 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
966 return (info->xmit_cnt);
969 static void mxser_flush_buffer(struct tty_struct *tty)
971 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
976 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
977 restore_flags(flags);
978 wake_up_interruptible(&tty->write_wait);
979 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
980 tty->ldisc.write_wakeup)
981 (tty->ldisc.write_wakeup) (tty);
984 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
985 unsigned int cmd, unsigned long arg)
988 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
990 struct async_icount cprev, cnow; /* kernel counter temps */
991 struct serial_icounter_struct __user *p_cuser;
993 void __user *argp = (void __user *)arg;
995 if (PORTNO(tty) == MXSER_PORTS)
996 return (mxser_ioctl_special(cmd, arg));
997 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) &&
998 (cmd != TIOCGICOUNT)) {
999 if (tty->flags & (1 << TTY_IO_ERROR))
1003 case TCSBRK: /* SVID version: non-zero arg --> no break */
1004 retval = tty_check_change(tty);
1007 tty_wait_until_sent(tty, 0);
1009 mxser_send_break(info, HZ / 4); /* 1/4 second */
1011 case TCSBRKP: /* support for POSIX tcsendbreak() */
1012 retval = tty_check_change(tty);
1015 tty_wait_until_sent(tty, 0);
1016 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1019 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1021 if(get_user(templ, (unsigned long __user *) arg))
1024 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1025 (arg ? CLOCAL : 0));
1028 return mxser_get_serial_info(info, argp);
1030 return mxser_set_serial_info(info, argp);
1031 case TIOCSERGETLSR: /* Get line status register */
1032 return mxser_get_lsr_info(info, argp);
1034 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1035 * - mask passed in arg for lines of interest
1036 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1037 * Caller should use TIOCGICOUNT to see which one it was
1042 cprev = info->icount; /* note the counters on entry */
1043 restore_flags(flags);
1045 interruptible_sleep_on(&info->delta_msr_wait);
1046 /* see if a signal did it */
1047 if (signal_pending(current))
1048 return (-ERESTARTSYS);
1051 cnow = info->icount; /* atomic copy */
1052 restore_flags(flags);
1053 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1054 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1055 return (-EIO); /* no change => error */
1056 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1057 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1058 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1059 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1066 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1067 * Return: write counters to the user passed counter struct
1068 * NB: both 1->0 and 0->1 transitions are counted except for
1069 * RI where only 0->1 is counted.
1074 cnow = info->icount;
1075 restore_flags(flags);
1077 if(put_user(cnow.cts, &p_cuser->cts))
1079 if(put_user(cnow.dsr, &p_cuser->dsr))
1081 if(put_user(cnow.rng, &p_cuser->rng))
1083 return put_user(cnow.dcd, &p_cuser->dcd);
1084 case MOXA_HighSpeedOn:
1085 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1087 return (-ENOIOCTLCMD);
1092 static int mxser_ioctl_special(unsigned int cmd, unsigned long arg)
1094 int i, result, status;
1095 void __user *argp = (void __user *)arg;
1099 if(copy_to_user(argp, mxsercfg,
1100 sizeof(struct mxser_hwconf) * 4))
1103 case MOXA_GET_MAJOR:
1104 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1108 case MOXA_GET_CUMAJOR:
1110 if(copy_to_user(argp, &result, sizeof(int)))
1114 case MOXA_CHKPORTENABLE:
1116 for (i = 0; i < MXSER_PORTS; i++) {
1117 if (mxvar_table[i].base)
1120 return put_user(result, (unsigned long __user *) argp);
1121 case MOXA_GETDATACOUNT:
1122 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1125 case MOXA_GETMSTATUS:
1126 for (i = 0; i < MXSER_PORTS; i++) {
1128 if (!mxvar_table[i].base) {
1129 GMStatus[i].dcd = 0;
1130 GMStatus[i].dsr = 0;
1131 GMStatus[i].cts = 0;
1134 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1135 GMStatus[i].cflag = mxvar_table[i].cflag;
1137 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1139 status = inb(mxvar_table[i].base + UART_MSR);
1140 if (status & 0x80 /*UART_MSR_DCD */ )
1141 GMStatus[i].dcd = 1;
1143 GMStatus[i].dcd = 0;
1145 if (status & 0x20 /*UART_MSR_DSR */ )
1146 GMStatus[i].dsr = 1;
1148 GMStatus[i].dsr = 0;
1151 if (status & 0x10 /*UART_MSR_CTS */ )
1152 GMStatus[i].cts = 1;
1154 GMStatus[i].cts = 0;
1156 if(copy_to_user(argp, GMStatus,
1157 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1161 return (-ENOIOCTLCMD);
1167 * This routine is called by the upper-layer tty layer to signal that
1168 * incoming characters should be throttled.
1170 static void mxser_throttle(struct tty_struct *tty)
1172 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1173 unsigned long flags;
1176 info->x_char = STOP_CHAR(tty);
1180 info->IER |= UART_IER_THRI;
1181 outb(info->IER, info->base + UART_IER); /* force Tx interrupt */
1182 restore_flags(flags);
1184 if (info->tty->termios->c_cflag & CRTSCTS) {
1185 info->MCR &= ~UART_MCR_RTS;
1188 outb(info->MCR, info->base + UART_MCR);
1189 restore_flags(flags);
1193 static void mxser_unthrottle(struct tty_struct *tty)
1195 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1196 unsigned long flags;
1202 info->x_char = START_CHAR(tty);
1206 info->IER |= UART_IER_THRI; /* force Tx interrupt */
1207 outb(info->IER, info->base + UART_IER);
1208 restore_flags(flags);
1211 if (info->tty->termios->c_cflag & CRTSCTS) {
1212 info->MCR |= UART_MCR_RTS;
1215 outb(info->MCR, info->base + UART_MCR);
1216 restore_flags(flags);
1220 static void mxser_set_termios(struct tty_struct *tty,
1221 struct termios *old_termios)
1223 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1225 /* 8-2-99 by William
1226 if ( (tty->termios->c_cflag == old_termios->c_cflag) &&
1227 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
1228 RELEVANT_IFLAG(old_termios->c_iflag)) )
1231 mxser_change_speed(info, old_termios);
1233 if ( (old_termios->c_cflag & CRTSCTS) &&
1234 !(tty->termios->c_cflag & CRTSCTS) ) {
1235 tty->hw_stopped = 0;
1239 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1240 (RELEVANT_IFLAG(tty->termios->c_iflag) !=
1241 RELEVANT_IFLAG(old_termios->c_iflag))) {
1243 mxser_change_speed(info, old_termios);
1245 if ((old_termios->c_cflag & CRTSCTS) &&
1246 !(tty->termios->c_cflag & CRTSCTS)) {
1247 tty->hw_stopped = 0;
1251 /* Handle sw stopped */
1252 if ((old_termios->c_iflag & IXON) &&
1253 !(tty->termios->c_iflag & IXON)) {
1260 * mxser_stop() and mxser_start()
1262 * This routines are called before setting or resetting tty->stopped.
1263 * They enable or disable transmitter interrupts, as necessary.
1265 static void mxser_stop(struct tty_struct *tty)
1267 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1268 unsigned long flags;
1272 if (info->IER & UART_IER_THRI) {
1273 info->IER &= ~UART_IER_THRI;
1274 outb(info->IER, info->base + UART_IER);
1276 restore_flags(flags);
1279 static void mxser_start(struct tty_struct *tty)
1281 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1282 unsigned long flags;
1286 if (info->xmit_cnt && info->xmit_buf &&
1287 !(info->IER & UART_IER_THRI)) {
1288 info->IER |= UART_IER_THRI;
1289 outb(info->IER, info->base + UART_IER);
1291 restore_flags(flags);
1295 * This routine is called by tty_hangup() when a hangup is signaled.
1297 void mxser_hangup(struct tty_struct *tty)
1299 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1301 mxser_flush_buffer(tty);
1302 mxser_shutdown(info);
1305 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1307 wake_up_interruptible(&info->open_wait);
1311 * This is the serial driver's generic interrupt routine
1313 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1316 struct mxser_struct *info;
1317 struct mxser_struct *port;
1318 int max, irqbits, bits, msr;
1319 int pass_counter = 0;
1323 for (i = 0; i < MXSER_BOARDS; i++) {
1324 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1330 if (i == MXSER_BOARDS)
1334 max = mxser_numports[mxsercfg[i].board_type];
1337 irqbits = inb(port->vector) & port->vectormask;
1338 if (irqbits == port->vectormask)
1341 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1342 if (irqbits == port->vectormask)
1348 (inb(info->base + UART_IIR) & UART_IIR_NO_INT))
1350 status = inb(info->base + UART_LSR) & info->read_status_mask;
1351 if (status & UART_LSR_DR)
1352 mxser_receive_chars(info, &status);
1353 msr = inb(info->base + UART_MSR);
1354 if (msr & UART_MSR_ANY_DELTA)
1355 mxser_check_modem_status(info, msr);
1356 if (status & UART_LSR_THRE) {
1357 /* 8-2-99 by William
1358 if ( info->x_char || (info->xmit_cnt > 0) )
1360 mxser_transmit_chars(info);
1363 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
1365 printk("MOXA Smartio/Indusrtio family driver interrupt loop break\n");
1367 break; /* Prevent infinite loops */
1370 return IRQ_RETVAL(handled);
1373 static inline void mxser_receive_chars(struct mxser_struct *info,
1376 struct tty_struct *tty = info->tty;
1382 ch = inb(info->base + UART_RX);
1383 if (*status & info->ignore_status_mask) {
1384 if (++ignored > 100)
1387 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1390 if (*status & UART_LSR_SPECIAL) {
1391 if (*status & UART_LSR_BI) {
1392 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
1393 if (info->flags & ASYNC_SAK)
1395 } else if (*status & UART_LSR_PE) {
1396 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
1397 } else if (*status & UART_LSR_FE) {
1398 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
1399 } else if (*status & UART_LSR_OE) {
1400 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
1402 *tty->flip.flag_buf_ptr++ = 0;
1404 *tty->flip.flag_buf_ptr++ = 0;
1405 *tty->flip.char_buf_ptr++ = ch;
1408 *status = inb(info->base + UART_LSR) & info->read_status_mask;
1409 } while (*status & UART_LSR_DR);
1410 mxvar_log.rxcnt[info->port] += cnt;
1411 schedule_delayed_work(&tty->flip.work, 1);
1415 static inline void mxser_transmit_chars(struct mxser_struct *info)
1420 outb(info->x_char, info->base + UART_TX);
1422 mxvar_log.txcnt[info->port]++;
1425 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
1426 info->tty->hw_stopped) {
1427 info->IER &= ~UART_IER_THRI;
1428 outb(info->IER, info->base + UART_IER);
1431 cnt = info->xmit_cnt;
1432 count = info->xmit_fifo_size;
1434 outb(info->xmit_buf[info->xmit_tail++], info->base + UART_TX);
1435 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1436 if (--info->xmit_cnt <= 0)
1438 } while (--count > 0);
1439 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
1441 if (info->xmit_cnt < WAKEUP_CHARS) {
1442 set_bit(MXSER_EVENT_TXLOW, &info->event);
1443 schedule_work(&info->tqueue);
1445 if (info->xmit_cnt <= 0) {
1446 info->IER &= ~UART_IER_THRI;
1447 outb(info->IER, info->base + UART_IER);
1451 static inline void mxser_check_modem_status(struct mxser_struct *info,
1455 /* update input line counters */
1456 if (status & UART_MSR_TERI)
1458 if (status & UART_MSR_DDSR)
1460 if (status & UART_MSR_DDCD)
1462 if (status & UART_MSR_DCTS)
1464 wake_up_interruptible(&info->delta_msr_wait);
1466 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
1467 if (status & UART_MSR_DCD)
1468 wake_up_interruptible(&info->open_wait);
1470 set_bit(MXSER_EVENT_HANGUP, &info->event);
1471 schedule_work(&info->tqueue);
1473 if (info->flags & ASYNC_CTS_FLOW) {
1474 if (info->tty->hw_stopped) {
1475 if (status & UART_MSR_CTS) {
1476 info->tty->hw_stopped = 0;
1477 info->IER |= UART_IER_THRI;
1478 outb(info->IER, info->base + UART_IER);
1480 set_bit(MXSER_EVENT_TXLOW, &info->event);
1481 schedule_work(&info->tqueue);
1484 if (!(status & UART_MSR_CTS)) {
1485 info->tty->hw_stopped = 1;
1486 info->IER &= ~UART_IER_THRI;
1487 outb(info->IER, info->base + UART_IER);
1493 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
1494 struct mxser_struct *info)
1496 DECLARE_WAITQUEUE(wait, current);
1497 unsigned long flags;
1502 * If the device is in the middle of being closed, then block
1503 * until it's done, and then try again.
1505 if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
1506 if (info->flags & ASYNC_CLOSING)
1507 interruptible_sleep_on(&info->close_wait);
1508 #ifdef SERIAL_DO_RESTART
1509 if (info->flags & ASYNC_HUP_NOTIFY)
1512 return (-ERESTARTSYS);
1518 * If non-blocking mode is set, or the port is not enabled,
1519 * then make the check up front and then exit.
1521 if ((filp->f_flags & O_NONBLOCK) ||
1522 (tty->flags & (1 << TTY_IO_ERROR))) {
1523 info->flags |= ASYNC_NORMAL_ACTIVE;
1526 if (tty->termios->c_cflag & CLOCAL)
1530 * Block waiting for the carrier detect and the line to become
1531 * free (i.e., not in use by the callout). While we are in
1532 * this loop, info->count is dropped by one, so that
1533 * mxser_close() knows when to free things. We restore it upon
1534 * exit, either normal or abnormal.
1537 add_wait_queue(&info->open_wait, &wait);
1540 if (!tty_hung_up_p(filp))
1542 restore_flags(flags);
1543 info->blocked_open++;
1547 outb(inb(info->base + UART_MCR) | UART_MCR_DTR | UART_MCR_RTS,
1548 info->base + UART_MCR);
1549 restore_flags(flags);
1550 set_current_state(TASK_INTERRUPTIBLE);
1551 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
1552 #ifdef SERIAL_DO_RESTART
1553 if (info->flags & ASYNC_HUP_NOTIFY)
1556 retval = -ERESTARTSYS;
1562 if (!(info->flags & ASYNC_CLOSING) &&
1563 (do_clocal || (inb(info->base + UART_MSR) & UART_MSR_DCD)))
1565 if (signal_pending(current)) {
1566 retval = -ERESTARTSYS;
1571 set_current_state(TASK_RUNNING);
1572 remove_wait_queue(&info->open_wait, &wait);
1573 if (!tty_hung_up_p(filp))
1575 info->blocked_open--;
1578 info->flags |= ASYNC_NORMAL_ACTIVE;
1582 static int mxser_startup(struct mxser_struct *info)
1584 unsigned long flags;
1587 page = get_zeroed_page(GFP_KERNEL);
1594 if (info->flags & ASYNC_INITIALIZED) {
1596 restore_flags(flags);
1599 if (!info->base || !info->type) {
1601 set_bit(TTY_IO_ERROR, &info->tty->flags);
1603 restore_flags(flags);
1609 info->xmit_buf = (unsigned char *) page;
1612 * Clear the FIFO buffers and disable them
1613 * (they will be reenabled in mxser_change_speed())
1615 if (info->xmit_fifo_size == 16)
1616 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1617 info->base + UART_FCR);
1620 * At this point there's no way the LSR could still be 0xFF;
1621 * if it is, then bail out, because there's likely no UART
1624 if (inb(info->base + UART_LSR) == 0xff) {
1625 restore_flags(flags);
1626 if (capable(CAP_SYS_ADMIN)) {
1628 set_bit(TTY_IO_ERROR, &info->tty->flags);
1634 * Clear the interrupt registers.
1636 (void) inb(info->base + UART_LSR);
1637 (void) inb(info->base + UART_RX);
1638 (void) inb(info->base + UART_IIR);
1639 (void) inb(info->base + UART_MSR);
1642 * Now, initialize the UART
1644 outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
1645 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
1646 outb(info->MCR, info->base + UART_MCR);
1649 * Finally, enable interrupts
1651 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
1652 outb(info->IER, info->base + UART_IER); /* enable interrupts */
1655 * And clear the interrupt registers again for luck.
1657 (void) inb(info->base + UART_LSR);
1658 (void) inb(info->base + UART_RX);
1659 (void) inb(info->base + UART_IIR);
1660 (void) inb(info->base + UART_MSR);
1663 test_and_clear_bit(TTY_IO_ERROR, &info->tty->flags);
1665 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1668 * and set the speed of the serial port
1670 mxser_change_speed(info, NULL);
1672 info->flags |= ASYNC_INITIALIZED;
1673 restore_flags(flags);
1678 * This routine will shutdown a serial port; interrupts maybe disabled, and
1679 * DTR is dropped if the hangup on close termio flag is on.
1681 static void mxser_shutdown(struct mxser_struct *info)
1683 unsigned long flags;
1685 if (!(info->flags & ASYNC_INITIALIZED))
1689 cli(); /* Disable interrupts */
1692 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1693 * here so the queue might never be waken up
1695 wake_up_interruptible(&info->delta_msr_wait);
1698 * Free the IRQ, if necessary
1700 if (info->xmit_buf) {
1701 free_page((unsigned long) info->xmit_buf);
1702 info->xmit_buf = NULL;
1705 outb(0x00, info->base + UART_IER); /* disable all intrs */
1707 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1708 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1709 outb(info->MCR, info->base + UART_MCR);
1711 /* clear Rx/Tx FIFO's */
1712 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
1713 /* read data port to reset things */
1714 (void) inb(info->base + UART_RX);
1717 set_bit(TTY_IO_ERROR, &info->tty->flags);
1719 info->flags &= ~ASYNC_INITIALIZED;
1720 restore_flags(flags);
1724 * This routine is called to set the UART divisor registers to match
1725 * the specified baud rate for a serial port.
1727 static int mxser_change_speed(struct mxser_struct *info,
1728 struct termios *old_termios)
1731 unsigned cflag, cval, fcr;
1734 unsigned long flags;
1736 if (!info->tty || !info->tty->termios)
1738 cflag = info->tty->termios->c_cflag;
1743 #define B921600 (B460800 +1)
1745 switch (cflag & (CBAUD | CBAUDEX)) {
1812 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1813 i = 16; /* 57600 bps */
1814 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1815 i = 17; /* 115200 bps */
1817 #ifdef ASYNC_SPD_SHI
1818 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1822 #ifdef ASYNC_SPD_WARP
1823 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1827 if (mxvar_baud_table[i] == 134) {
1828 quot = (2 * info->baud_base / 269);
1829 } else if (mxvar_baud_table[i]) {
1830 quot = info->baud_base / mxvar_baud_table[i];
1831 if (!quot && old_termios) {
1833 info->tty->termios->c_cflag &= ~CBAUD;
1834 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1835 switch (info->tty->termios->c_cflag & (CBAUD | CBAUDEX)) {
1901 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1902 i = 16; /* 57600 bps */
1903 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1904 i = 17; /* 115200 bps */
1905 #ifdef ASYNC_SPD_SHI
1906 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1909 #ifdef ASYNC_SPD_WARP
1910 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1914 if (mxvar_baud_table[i] == 134) {
1915 quot = (2 * info->baud_base / 269);
1916 } else if (mxvar_baud_table[i]) {
1917 quot = info->baud_base / mxvar_baud_table[i];
1923 } else if (quot == 0)
1930 info->MCR |= UART_MCR_DTR;
1933 outb(info->MCR, info->base + UART_MCR);
1934 restore_flags(flags);
1936 info->MCR &= ~UART_MCR_DTR;
1939 outb(info->MCR, info->base + UART_MCR);
1940 restore_flags(flags);
1943 /* byte size and parity */
1944 switch (cflag & CSIZE) {
1959 break; /* too keep GCC shut... */
1964 cval |= UART_LCR_PARITY;
1965 if (!(cflag & PARODD))
1966 cval |= UART_LCR_EPAR;
1967 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
1970 fcr = UART_FCR_ENABLE_FIFO;
1971 switch (info->rx_trigger) {
1973 fcr |= UART_FCR_TRIGGER_1;
1976 fcr |= UART_FCR_TRIGGER_4;
1979 fcr |= UART_FCR_TRIGGER_8;
1982 fcr |= UART_FCR_TRIGGER_14;
1986 /* CTS flow control flag and modem status interrupts */
1987 info->IER &= ~UART_IER_MSI;
1988 info->MCR &= ~UART_MCR_AFE;
1989 if (cflag & CRTSCTS) {
1990 info->flags |= ASYNC_CTS_FLOW;
1991 info->IER |= UART_IER_MSI;
1992 if (info->type == PORT_16550A)
1993 info->MCR |= UART_MCR_AFE;
1995 info->flags &= ~ASYNC_CTS_FLOW;
1997 outb(info->MCR, info->base + UART_MCR);
1999 info->flags &= ~ASYNC_CHECK_CD;
2001 info->flags |= ASYNC_CHECK_CD;
2002 info->IER |= UART_IER_MSI;
2004 outb(info->IER, info->base + UART_IER);
2007 * Set up parity check flag
2009 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2010 if (I_INPCK(info->tty))
2011 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2012 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2013 info->read_status_mask |= UART_LSR_BI;
2015 info->ignore_status_mask = 0;
2017 /* This should be safe, but for some broken bits of hardware... */
2018 if (I_IGNPAR(info->tty)) {
2019 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2020 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
2023 if (I_IGNBRK(info->tty)) {
2024 info->ignore_status_mask |= UART_LSR_BI;
2025 info->read_status_mask |= UART_LSR_BI;
2027 * If we're ignore parity and break indicators, ignore
2028 * overruns too. (For real raw support).
2030 if (I_IGNPAR(info->tty)) {
2031 info->ignore_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2032 info->read_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2037 outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2038 outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2039 outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2040 outb(cval, info->base + UART_LCR); /* reset DLAB */
2041 outb(fcr, info->base + UART_FCR); /* set fcr */
2042 restore_flags(flags);
2048 * ------------------------------------------------------------
2049 * friends of mxser_ioctl()
2050 * ------------------------------------------------------------
2052 static int mxser_get_serial_info(struct mxser_struct *info,
2053 struct serial_struct __user *retinfo)
2055 struct serial_struct tmp;
2059 memset(&tmp, 0, sizeof(tmp));
2060 tmp.type = info->type;
2061 tmp.line = info->port;
2062 tmp.port = info->base;
2063 tmp.irq = info->irq;
2064 tmp.flags = info->flags;
2065 tmp.baud_base = info->baud_base;
2066 tmp.close_delay = info->close_delay;
2067 tmp.closing_wait = info->closing_wait;
2068 tmp.custom_divisor = info->custom_divisor;
2070 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2073 static int mxser_set_serial_info(struct mxser_struct *info,
2074 struct serial_struct __user *new_info)
2076 struct serial_struct new_serial;
2080 if (!new_info || !info->base)
2082 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2085 if ((new_serial.irq != info->irq) ||
2086 (new_serial.port != info->base) ||
2087 (new_serial.type != info->type) ||
2088 (new_serial.custom_divisor != info->custom_divisor) ||
2089 (new_serial.baud_base != info->baud_base))
2092 flags = info->flags & ASYNC_SPD_MASK;
2094 if (!capable(CAP_SYS_ADMIN)) {
2095 if ((new_serial.baud_base != info->baud_base) ||
2096 (new_serial.close_delay != info->close_delay) ||
2097 ((new_serial.flags & ~ASYNC_USR_MASK) !=
2098 (info->flags & ~ASYNC_USR_MASK)))
2100 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2101 (new_serial.flags & ASYNC_USR_MASK));
2104 * OK, past this point, all the error checking has been done.
2105 * At this point, we start making changes.....
2107 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2108 (new_serial.flags & ASYNC_FLAGS));
2109 info->close_delay = new_serial.close_delay * HZ / 100;
2110 info->closing_wait = new_serial.closing_wait * HZ / 100;
2113 if (info->flags & ASYNC_INITIALIZED) {
2114 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2115 mxser_change_speed(info, NULL);
2118 retval = mxser_startup(info);
2123 * mxser_get_lsr_info - get line status register info
2125 * Purpose: Let user call ioctl() to get info when the UART physically
2126 * is emptied. On bus types like RS485, the transmitter must
2127 * release the bus after transmitting. This must be done when
2128 * the transmit shift register is empty, not be done when the
2129 * transmit holding register is empty. This functionality
2130 * allows an RS485 driver to be written in user space.
2132 static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int __user *value)
2134 unsigned char status;
2135 unsigned int result;
2136 unsigned long flags;
2140 status = inb(info->base + UART_LSR);
2141 restore_flags(flags);
2142 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2143 return put_user(result, value);
2147 * This routine sends a break character out the serial port.
2149 static void mxser_send_break(struct mxser_struct *info, int duration)
2151 unsigned long flags;
2154 set_current_state(TASK_INTERRUPTIBLE);
2157 outb(inb(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
2158 schedule_timeout(duration);
2159 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
2160 restore_flags(flags);
2163 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2165 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
2166 unsigned char control, status;
2167 unsigned long flags;
2169 if (PORTNO(tty) == MXSER_PORTS)
2170 return (-ENOIOCTLCMD);
2171 if (tty->flags & (1 << TTY_IO_ERROR))
2174 control = info->MCR;
2177 status = inb(info->base + UART_MSR);
2178 if (status & UART_MSR_ANY_DELTA)
2179 mxser_check_modem_status(info, status);
2180 restore_flags(flags);
2181 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2182 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2183 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2184 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2185 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2186 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2189 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
2190 unsigned int set, unsigned int clear)
2192 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
2193 unsigned long flags;
2195 if (PORTNO(tty) == MXSER_PORTS)
2196 return (-ENOIOCTLCMD);
2197 if (tty->flags & (1 << TTY_IO_ERROR))
2202 if (set & TIOCM_RTS)
2203 info->MCR |= UART_MCR_RTS;
2204 if (set & TIOCM_DTR)
2205 info->MCR |= UART_MCR_DTR;
2207 if (clear & TIOCM_RTS)
2208 info->MCR &= ~UART_MCR_RTS;
2209 if (clear & TIOCM_DTR)
2210 info->MCR &= ~UART_MCR_DTR;
2212 outb(info->MCR, info->base + UART_MCR);
2213 restore_flags(flags);
2217 static int mxser_read_register(int, unsigned short *);
2218 static int mxser_program_mode(int);
2219 static void mxser_normal_mode(int);
2221 static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2224 unsigned short regs[16], irq;
2225 unsigned char scratch, scratch2;
2227 id = mxser_read_register(cap, regs);
2228 if (id == C168_ASIC_ID)
2229 hwconf->board_type = MXSER_BOARD_C168_ISA;
2230 else if (id == C104_ASIC_ID)
2231 hwconf->board_type = MXSER_BOARD_C104_ISA;
2232 else if (id == C102_ASIC_ID)
2233 hwconf->board_type = MXSER_BOARD_C102_ISA;
2234 else if (id == CI132_ASIC_ID)
2235 hwconf->board_type = MXSER_BOARD_CI132;
2236 else if (id == CI134_ASIC_ID)
2237 hwconf->board_type = MXSER_BOARD_CI134;
2238 else if (id == CI104J_ASIC_ID)
2239 hwconf->board_type = MXSER_BOARD_CI104J;
2242 irq = regs[9] & 0x0F;
2243 irq = irq | (irq << 4);
2244 irq = irq | (irq << 8);
2245 if ((irq != regs[9]) || ((id == 1) && (irq != regs[10]))) {
2246 return (MXSER_ERR_IRQ_CONFLIT);
2249 return (MXSER_ERR_IRQ);
2251 for (i = 0; i < 8; i++)
2252 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
2253 hwconf->irq = (int) (irq & 0x0F);
2254 if ((regs[12] & 0x80) == 0) {
2255 return (MXSER_ERR_VECTOR);
2257 hwconf->vector = (int) regs[11]; /* interrupt vector */
2259 hwconf->vector_mask = 0x00FF;
2261 hwconf->vector_mask = 0x000F;
2262 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2263 if (regs[12] & bits)
2264 hwconf->baud_base[i] = 921600;
2266 hwconf->baud_base[i] = 115200;
2268 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2269 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2270 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2271 outb(scratch2, cap + UART_LCR);
2272 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2273 scratch = inb(cap + UART_IIR);
2275 hwconf->uart_type = PORT_16550A;
2277 hwconf->uart_type = PORT_16450;
2282 return (hwconf->ports);
2285 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
2286 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
2287 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
2288 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
2289 #define EN_CCMD 0x000 /* Chip's command register */
2290 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
2291 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
2292 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
2293 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
2294 #define EN0_DCFG 0x00E /* Data configuration reg WR */
2295 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
2296 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
2297 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
2298 static int mxser_read_register(int port, unsigned short *regs)
2300 int i, k, value, id;
2303 id = mxser_program_mode(port);
2306 for (i = 0; i < 14; i++) {
2307 k = (i & 0x3F) | 0x180;
2308 for (j = 0x100; j > 0; j >>= 1) {
2309 outb(CHIP_CS, port);
2311 outb(CHIP_CS | CHIP_DO, port);
2312 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
2314 outb(CHIP_CS, port);
2315 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
2320 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
2321 outb(CHIP_CS, port);
2322 outb(CHIP_CS | CHIP_SK, port);
2323 if (inb(port) & CHIP_DI)
2329 mxser_normal_mode(port);
2333 static int mxser_program_mode(int port)
2336 unsigned long flags;
2347 restore_flags(flags);
2348 id = inb(port + 1) & 0x1F;
2349 if ((id != C168_ASIC_ID) && (id != C104_ASIC_ID) && (id != CI104J_ASIC_ID) &&
2350 (id != C102_ASIC_ID) && (id != CI132_ASIC_ID) && (id != CI134_ASIC_ID))
2352 for (i = 0, j = 0; i < 4; i++) {
2356 } else if ((j == 1) && (n == 1)) {
2367 static void mxser_normal_mode(int port)
2371 outb(0xA5, port + 1);
2372 outb(0x80, port + 3);
2373 outb(12, port + 0); /* 9600 bps */
2375 outb(0x03, port + 3); /* 8 data bits */
2376 outb(0x13, port + 4); /* loop back mode */
2377 for (i = 0; i < 16; i++) {
2379 if ((n & 0x61) == 0x60)
2384 outb(0x00, port + 4);
2387 module_init(mxser_module_init);
2388 module_exit(mxser_module_exit);