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>
40 #include <linux/config.h>
41 #include <linux/module.h>
42 #include <linux/errno.h>
43 #include <linux/signal.h>
44 #include <linux/sched.h>
45 #include <linux/timer.h>
46 #include <linux/interrupt.h>
47 #include <linux/tty.h>
48 #include <linux/tty_flip.h>
49 #include <linux/serial.h>
50 #include <linux/serial_reg.h>
51 #include <linux/major.h>
52 #include <linux/string.h>
53 #include <linux/fcntl.h>
54 #include <linux/ptrace.h>
55 #include <linux/ioport.h>
57 #include <linux/smp_lock.h>
58 #include <linux/pci.h>
59 #include <linux/init.h>
61 #include <asm/system.h>
64 #include <asm/bitops.h>
65 #include <asm/uaccess.h>
67 #define MXSER_VERSION "1.2.1"
69 #define MXSERMAJOR 174
70 #define MXSERCUMAJOR 175
73 #define MXSER_EVENT_TXLOW 1
74 #define MXSER_EVENT_HANGUP 2
77 #define SERIAL_DO_RESTART
79 #define MXSER_BOARDS 4 /* Max. boards */
80 #define MXSER_PORTS 32 /* Max. ports */
81 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
82 #define MXSER_ISR_PASS_LIMIT 256
84 #define MXSER_ERR_IOADDR -1
85 #define MXSER_ERR_IRQ -2
86 #define MXSER_ERR_IRQ_CONFLIT -3
87 #define MXSER_ERR_VECTOR -4
89 #define SERIAL_TYPE_NORMAL 1
91 #define WAKEUP_CHARS 256
93 #define UART_MCR_AFE 0x20
94 #define UART_LSR_SPECIAL 0x1E
96 #define PORTNO(x) ((x)->index)
98 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
100 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
103 #define MIN(a,b) ((a) < (b) ? (a) : (b))
107 * Define the Moxa PCI vendor and device IDs.
110 #ifndef PCI_VENDOR_ID_MOXA
111 #define PCI_VENDOR_ID_MOXA 0x1393
113 #ifndef PCI_DEVICE_ID_C168
114 #define PCI_DEVICE_ID_C168 0x1680
116 #ifndef PCI_DEVICE_ID_C104
117 #define PCI_DEVICE_ID_C104 0x1040
119 #ifndef PCI_DEVICE_ID_CP132
120 #define PCI_DEVICE_ID_CP132 0x1320
122 #ifndef PCI_DEVICE_ID_CP114
123 #define PCI_DEVICE_ID_CP114 0x1141
125 #ifndef PCI_DEVICE_ID_CT114
126 #define PCI_DEVICE_ID_CT114 0x1140
129 #define C168_ASIC_ID 1
130 #define C104_ASIC_ID 2
131 #define CI134_ASIC_ID 3
132 #define CI132_ASIC_ID 4
133 #define CI104J_ASIC_ID 5
134 #define C102_ASIC_ID 0xB
137 MXSER_BOARD_C168_ISA = 0,
138 MXSER_BOARD_C104_ISA,
140 MXSER_BOARD_C168_PCI,
141 MXSER_BOARD_C104_PCI,
142 MXSER_BOARD_C102_ISA,
145 MXSER_BOARD_CP132_PCI,
146 MXSER_BOARD_CP114_PCI,
147 MXSER_BOARD_CT114_PCI
150 static char *mxser_brdname[] =
165 static int mxser_numports[] =
184 #define MOXA_GETDATACOUNT (MOXA + 23)
185 #define MOXA_GET_CONF (MOXA + 35)
186 #define MOXA_DIAGNOSE (MOXA + 50)
187 #define MOXA_CHKPORTENABLE (MOXA + 60)
188 #define MOXA_HighSpeedOn (MOXA + 61)
189 #define MOXA_GET_MAJOR (MOXA + 63)
190 #define MOXA_GET_CUMAJOR (MOXA + 64)
191 #define MOXA_GETMSTATUS (MOXA + 65)
193 static struct pci_device_id mxser_pcibrds[] = {
194 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C168, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
195 MXSER_BOARD_C168_PCI },
196 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C104, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
197 MXSER_BOARD_C104_PCI },
198 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP132, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
199 MXSER_BOARD_CP132_PCI },
200 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP114, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
201 MXSER_BOARD_CP114_PCI },
202 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CT114, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
203 MXSER_BOARD_CT114_PCI },
206 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
208 static int ioaddr[MXSER_BOARDS];
209 static int ttymajor = MXSERMAJOR;
212 /* Variables for insmod */
214 MODULE_AUTHOR("William Chen");
215 MODULE_DESCRIPTION("MOXA Smartio Family Multiport Board Device Driver");
216 MODULE_LICENSE("GPL");
217 MODULE_PARM(ioaddr, "1-4i");
218 MODULE_PARM(ttymajor, "i");
219 MODULE_PARM(verbose, "i");
221 struct mxser_hwconf {
228 int ioaddr[MXSER_PORTS_PER_BOARD];
229 int baud_base[MXSER_PORTS_PER_BOARD];
230 struct pci_dev *pdev;
233 struct mxser_struct {
235 int base; /* port base address */
236 int irq; /* port using irq no. */
237 int vector; /* port irq vector */
238 int vectormask; /* port vector mask */
239 int rx_trigger; /* Rx fifo trigger level */
240 int baud_base; /* max. speed */
241 int flags; /* defined in tty.h */
242 int type; /* UART type */
243 struct tty_struct *tty;
244 int read_status_mask;
245 int ignore_status_mask;
248 int x_char; /* xon/xoff character */
250 unsigned short closing_wait;
251 int IER; /* Interrupt Enable Register */
252 int MCR; /* Modem control register */
254 int count; /* # of fd on device */
255 int blocked_open; /* # of blocked opens */
256 unsigned char *xmit_buf;
260 struct work_struct tqueue;
262 wait_queue_head_t open_wait;
263 wait_queue_head_t close_wait;
264 wait_queue_head_t delta_msr_wait;
265 struct async_icount icount; /* kernel counters for the 4 input interrupts */
270 int rxcnt[MXSER_PORTS];
271 int txcnt[MXSER_PORTS];
274 struct mxser_mstatus {
282 static struct mxser_mstatus GMStatus[MXSER_PORTS];
284 static int mxserBoardCAP[MXSER_BOARDS] =
287 /* 0x180, 0x280, 0x200, 0x320 */
291 static struct tty_driver *mxvar_sdriver;
292 static struct mxser_struct mxvar_table[MXSER_PORTS];
293 static struct mxser_log mxvar_log;
294 static int mxvar_diagflag;
296 * mxvar_tmp_buf is used as a temporary buffer by serial_write. We need
297 * to lock it in case the memcpy_fromfs blocks while swapping in a page,
298 * and some other program tries to do a serial write at the same time.
299 * Since the lock will only come under contention when the system is
300 * swapping and available memory is low, it makes sense to share one
301 * buffer across all the serial ports, since it significantly saves
302 * memory if large numbers of serial ports are open.
304 static unsigned char *mxvar_tmp_buf;
305 static struct semaphore mxvar_tmp_buf_sem;
308 * This is used to figure out the divisor speeds and the timeouts
310 static int mxvar_baud_table[] =
312 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
313 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 0};
315 struct mxser_hwconf mxsercfg[MXSER_BOARDS];
321 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
322 static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
323 static int mxser_get_PCI_conf(struct pci_dev *, int, struct mxser_hwconf *);
324 static void mxser_do_softint(void *);
325 static int mxser_open(struct tty_struct *, struct file *);
326 static void mxser_close(struct tty_struct *, struct file *);
327 static int mxser_write(struct tty_struct *, int, const unsigned char *, int);
328 static int mxser_write_room(struct tty_struct *);
329 static void mxser_flush_buffer(struct tty_struct *);
330 static int mxser_chars_in_buffer(struct tty_struct *);
331 static void mxser_flush_chars(struct tty_struct *);
332 static void mxser_put_char(struct tty_struct *, unsigned char);
333 static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
334 static int mxser_ioctl_special(unsigned int, unsigned long);
335 static void mxser_throttle(struct tty_struct *);
336 static void mxser_unthrottle(struct tty_struct *);
337 static void mxser_set_termios(struct tty_struct *, struct termios *);
338 static void mxser_stop(struct tty_struct *);
339 static void mxser_start(struct tty_struct *);
340 static void mxser_hangup(struct tty_struct *);
341 static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
342 static inline void mxser_receive_chars(struct mxser_struct *, int *);
343 static inline void mxser_transmit_chars(struct mxser_struct *);
344 static inline void mxser_check_modem_status(struct mxser_struct *, int);
345 static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
346 static int mxser_startup(struct mxser_struct *);
347 static void mxser_shutdown(struct mxser_struct *);
348 static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
349 static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct *);
350 static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct *);
351 static int mxser_get_lsr_info(struct mxser_struct *, unsigned int *);
352 static void mxser_send_break(struct mxser_struct *, int);
353 static int mxser_tiocmget(struct tty_struct *, struct file *);
354 static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
357 * The MOXA C168/C104 serial driver boot-time initialization code!
360 static void __exit mxser_module_exit(void)
366 printk("Unloading module mxser ...\n");
367 if ((err |= tty_unregister_driver(mxvar_sdriver)))
368 printk("Couldn't unregister MOXA Smartio family serial driver\n");
369 put_tty_driver(mxvar_sdriver);
371 for (i = 0; i < MXSER_BOARDS; i++) {
372 if (mxsercfg[i].board_type == -1)
375 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
384 int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
386 struct mxser_struct *info;
391 init_MUTEX(&mxvar_tmp_buf_sem);
393 n = board * MXSER_PORTS_PER_BOARD;
394 info = &mxvar_table[n];
395 for (i = 0; i < hwconf->ports; i++, n++, info++) {
397 printk(" ttyM%d/cum%d at 0x%04x ", n, n, hwconf->ioaddr[i]);
398 if (hwconf->baud_base[i] == 115200)
399 printk(" max. baud rate up to 115200 bps.\n");
401 printk(" max. baud rate up to 921600 bps.\n");
404 info->base = hwconf->ioaddr[i];
405 info->irq = hwconf->irq;
406 info->vector = hwconf->vector;
407 info->vectormask = hwconf->vector_mask;
408 info->rx_trigger = 14;
409 info->baud_base = hwconf->baud_base[i];
410 info->flags = ASYNC_SHARE_IRQ;
411 info->type = hwconf->uart_type;
412 if ((info->type == PORT_16450) || (info->type == PORT_8250))
413 info->xmit_fifo_size = 1;
415 info->xmit_fifo_size = 16;
416 info->custom_divisor = hwconf->baud_base[i] * 16;
417 info->close_delay = 5 * HZ / 10;
418 info->closing_wait = 30 * HZ;
419 INIT_WORK(&info->tqueue, mxser_do_softint, info);
420 info->cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
421 init_waitqueue_head(&info->open_wait);
422 init_waitqueue_head(&info->close_wait);
423 init_waitqueue_head(&info->delta_msr_wait);
427 * Allocate the IRQ if necessary
431 n = board * MXSER_PORTS_PER_BOARD;
432 info = &mxvar_table[n];
435 retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
438 restore_flags(flags);
439 printk("Board %d: %s", board, mxser_brdname[hwconf->board_type]);
440 printk(" Request irq fail,IRQ (%d) may be conflit with another device.\n", info->irq);
443 restore_flags(flags);
449 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
451 mxsercfg[board] = *hwconf;
454 static int mxser_get_PCI_conf(struct pci_dev *pdev, int board_type, struct mxser_hwconf *hwconf)
457 unsigned int ioaddress;
459 hwconf->board_type = board_type;
460 hwconf->ports = mxser_numports[board_type];
461 ioaddress = pci_resource_start (pdev, 2);
462 for (i = 0; i < hwconf->ports; i++)
463 hwconf->ioaddr[i] = ioaddress + 8 * i;
465 ioaddress = pci_resource_start (pdev, 3);
466 hwconf->vector = ioaddress;
468 hwconf->irq = pdev->irq;
470 hwconf->uart_type = PORT_16550A;
471 hwconf->vector_mask = 0;
472 for (i = 0; i < hwconf->ports; i++) {
473 hwconf->vector_mask |= (1 << i);
474 hwconf->baud_base[i] = 921600;
479 static struct tty_operations mxser_ops = {
481 .close = mxser_close,
482 .write = mxser_write,
483 .put_char = mxser_put_char,
484 .flush_chars = mxser_flush_chars,
485 .write_room = mxser_write_room,
486 .chars_in_buffer = mxser_chars_in_buffer,
487 .flush_buffer = mxser_flush_buffer,
488 .ioctl = mxser_ioctl,
489 .throttle = mxser_throttle,
490 .unthrottle = mxser_unthrottle,
491 .set_termios = mxser_set_termios,
493 .start = mxser_start,
494 .hangup = mxser_hangup,
495 .tiocmget = mxser_tiocmget,
496 .tiocmset = mxser_tiocmset,
499 static int __init mxser_module_init(void)
502 struct mxser_hwconf hwconf;
504 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
508 printk("MOXA Smartio family driver version %s\n", MXSER_VERSION);
510 /* Initialize the tty_driver structure */
512 mxvar_sdriver->owner = THIS_MODULE;
513 mxvar_sdriver->name = "ttyM";
514 mxvar_sdriver->devfs_name = "tts/M";
515 mxvar_sdriver->major = ttymajor;
516 mxvar_sdriver->minor_start = 0;
517 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
518 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
519 mxvar_sdriver->init_termios = tty_std_termios;
520 mxvar_sdriver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
521 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
522 tty_set_operations(mxvar_sdriver, &mxser_ops);
523 printk("Tty devices major number = %d\n", ttymajor);
526 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
527 memset(&mxvar_log, 0, sizeof(struct mxser_log));
531 /* Start finding ISA boards here */
532 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
534 if (!(cap = mxserBoardCAP[b]))
537 retval = mxser_get_ISA_conf(cap, &hwconf);
540 printk("Found MOXA %s board (CAP=0x%x)\n",
541 mxser_brdname[hwconf.board_type],
545 if (retval == MXSER_ERR_IRQ)
546 printk("Invalid interrupt number,board not configured\n");
547 else if (retval == MXSER_ERR_IRQ_CONFLIT)
548 printk("Invalid interrupt number,board not configured\n");
549 else if (retval == MXSER_ERR_VECTOR)
550 printk("Invalid interrupt vector,board not configured\n");
551 else if (retval == MXSER_ERR_IOADDR)
552 printk("Invalid I/O address,board not configured\n");
558 if (mxser_initbrd(m, &hwconf) < 0)
561 mxser_getcfg(m, &hwconf);
566 /* Start finding ISA boards from module arg */
567 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
569 if (!(cap = ioaddr[b]))
572 retval = mxser_get_ISA_conf(cap, &hwconf);
575 printk("Found MOXA %s board (CAP=0x%x)\n",
576 mxser_brdname[hwconf.board_type],
580 if (retval == MXSER_ERR_IRQ)
581 printk("Invalid interrupt number,board not configured\n");
582 else if (retval == MXSER_ERR_IRQ_CONFLIT)
583 printk("Invalid interrupt number,board not configured\n");
584 else if (retval == MXSER_ERR_VECTOR)
585 printk("Invalid interrupt vector,board not configured\n");
586 else if (retval == MXSER_ERR_IOADDR)
587 printk("Invalid I/O address,board not configured\n");
593 if (mxser_initbrd(m, &hwconf) < 0)
596 mxser_getcfg(m, &hwconf);
601 /* start finding PCI board here */
605 struct pci_dev *pdev = NULL;
606 int n = (sizeof(mxser_pcibrds) / sizeof(mxser_pcibrds[0])) - 1;
608 for (b = 0; b < n; b++) {
609 while ((pdev = pci_find_device(mxser_pcibrds[b].vendor, mxser_pcibrds[b].device, pdev)))
611 if (pci_enable_device(pdev))
614 printk("Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
615 mxser_brdname[mxser_pcibrds[b].driver_data],
616 pdev->bus->number, PCI_SLOT(pdev->devfn));
617 if (m >= MXSER_BOARDS) {
618 printk("Too many Smartio family boards found (maximum %d),board not configured\n", MXSER_BOARDS);
620 retval = mxser_get_PCI_conf(pdev, mxser_pcibrds[b].driver_data, &hwconf);
622 if (retval == MXSER_ERR_IRQ)
623 printk("Invalid interrupt number,board not configured\n");
624 else if (retval == MXSER_ERR_IRQ_CONFLIT)
625 printk("Invalid interrupt number,board not configured\n");
626 else if (retval == MXSER_ERR_VECTOR)
627 printk("Invalid interrupt vector,board not configured\n");
628 else if (retval == MXSER_ERR_IOADDR)
629 printk("Invalid I/O address,board not configured\n");
632 if (mxser_initbrd(m, &hwconf) < 0)
634 mxser_getcfg(m, &hwconf);
642 for (i = m; i < MXSER_BOARDS; i++) {
643 mxsercfg[i].board_type = -1;
647 if (!tty_register_driver(mxvar_sdriver))
650 put_tty_driver(mxvar_sdriver);
651 printk("Couldn't install MOXA Smartio family driver !\n");
653 for (i = 0; i < MXSER_BOARDS; i++) {
654 if (mxsercfg[i].board_type == -1)
656 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
661 static void mxser_do_softint(void *private_)
663 struct mxser_struct *info = (struct mxser_struct *) private_;
664 struct tty_struct *tty;
668 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event)) {
669 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
670 tty->ldisc.write_wakeup)
671 (tty->ldisc.write_wakeup) (tty);
672 wake_up_interruptible(&tty->write_wait);
674 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event)) {
675 tty_hangup(tty); /* FIXME: module removal race here - AKPM */
681 * This routine is called whenever a serial port is opened. It
682 * enables interrupts for a serial port, linking in its async structure into
683 * the IRQ chain. It also performs the serial-specific
684 * initialization for the tty structure.
687 static int mxser_open(struct tty_struct *tty, struct file *filp)
689 struct mxser_struct *info;
694 if (line == MXSER_PORTS)
696 if ((line < 0) || (line > MXSER_PORTS))
698 info = mxvar_table + line;
703 tty->driver_data = info;
706 if (!mxvar_tmp_buf) {
707 page = get_zeroed_page(GFP_KERNEL);
713 mxvar_tmp_buf = (unsigned char *) page;
716 * Start up serial port
718 retval = mxser_startup(info);
722 return mxser_block_til_ready(tty, filp, info);
726 * This routine is called when the serial port gets closed. First, we
727 * wait for the last remaining data to be sent. Then, we unlink its
728 * async structure from the interrupt chain if necessary, and we free
729 * that IRQ if nothing is left in the chain.
732 static void mxser_close(struct tty_struct *tty, struct file *filp)
734 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
736 unsigned long timeout;
738 if (PORTNO(tty) == MXSER_PORTS)
746 if (tty_hung_up_p(filp)) {
747 restore_flags(flags);
750 if ((tty->count == 1) && (info->count != 1)) {
752 * Uh, oh. tty->count is 1, which means that the tty
753 * structure will be freed. Info->count should always
754 * be one in these conditions. If it's greater than
755 * one, we've got real problems, since it means the
756 * serial port won't be shutdown.
758 printk("mxser_close: bad serial port count; tty->count is 1, "
759 "info->count is %d\n", info->count);
762 if (--info->count < 0) {
763 printk("mxser_close: bad serial port count for ttys%d: %d\n",
764 info->port, info->count);
768 restore_flags(flags);
771 info->flags |= ASYNC_CLOSING;
772 info->cflag = tty->termios->c_cflag;
774 * Now we wait for the transmit buffer to clear; and we notify
775 * the line discipline to only process XON/XOFF characters.
778 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
779 tty_wait_until_sent(tty, info->closing_wait);
781 * At this point we stop accepting input. To do this, we
782 * disable the receive line status interrupts, and tell the
783 * interrupt driver to stop checking the data ready bit in the
784 * line status register.
786 info->IER &= ~UART_IER_RLSI;
788 info->read_status_mask &= ~UART_LSR_DR;
790 if (info->flags & ASYNC_INITIALIZED) {
791 outb(info->IER, info->base + UART_IER);
793 * Before we drop DTR, make sure the UART transmitter
794 * has completely drained; this is especially
795 * important if there is a transmit FIFO!
797 timeout = jiffies + HZ;
798 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
799 set_current_state(TASK_INTERRUPTIBLE);
801 if (time_after(jiffies, timeout))
805 mxser_shutdown(info);
806 if (tty->driver->flush_buffer)
807 tty->driver->flush_buffer(tty);
808 if (tty->ldisc.flush_buffer)
809 tty->ldisc.flush_buffer(tty);
813 if (info->blocked_open) {
814 if (info->close_delay) {
815 set_current_state(TASK_INTERRUPTIBLE);
816 schedule_timeout(info->close_delay);
818 wake_up_interruptible(&info->open_wait);
820 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
821 wake_up_interruptible(&info->close_wait);
822 restore_flags(flags);
826 static int mxser_write(struct tty_struct *tty, int from_user,
827 const unsigned char *buf, int count)
830 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
833 if (!tty || !info->xmit_buf || !mxvar_tmp_buf)
838 down(&mxvar_tmp_buf_sem);
840 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
841 SERIAL_XMIT_SIZE - info->xmit_head));
845 c -= copy_from_user(mxvar_tmp_buf, buf, c);
853 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
854 SERIAL_XMIT_SIZE - info->xmit_head));
855 memcpy(info->xmit_buf + info->xmit_head, mxvar_tmp_buf, c);
856 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
858 restore_flags(flags);
864 up(&mxvar_tmp_buf_sem);
868 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
869 SERIAL_XMIT_SIZE - info->xmit_head));
871 restore_flags(flags);
875 memcpy(info->xmit_buf + info->xmit_head, buf, c);
876 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
878 restore_flags(flags);
887 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
888 !(info->IER & UART_IER_THRI)) {
889 info->IER |= UART_IER_THRI;
890 outb(info->IER, info->base + UART_IER);
892 restore_flags(flags);
896 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
898 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
901 if (!tty || !info->xmit_buf)
906 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
907 restore_flags(flags);
910 info->xmit_buf[info->xmit_head++] = ch;
911 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
913 /********************************************** why ??? ***********
914 if ( !tty->stopped && !tty->hw_stopped &&
915 !(info->IER & UART_IER_THRI) ) {
916 info->IER |= UART_IER_THRI;
917 outb(info->IER, info->base + UART_IER);
919 *****************************************************************/
920 restore_flags(flags);
923 static void mxser_flush_chars(struct tty_struct *tty)
925 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
928 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
934 info->IER |= UART_IER_THRI;
935 outb(info->IER, info->base + UART_IER);
936 restore_flags(flags);
939 static int mxser_write_room(struct tty_struct *tty)
941 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
944 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
950 static int mxser_chars_in_buffer(struct tty_struct *tty)
952 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
954 return (info->xmit_cnt);
957 static void mxser_flush_buffer(struct tty_struct *tty)
959 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
964 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
965 restore_flags(flags);
966 wake_up_interruptible(&tty->write_wait);
967 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
968 tty->ldisc.write_wakeup)
969 (tty->ldisc.write_wakeup) (tty);
972 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
973 unsigned int cmd, unsigned long arg)
976 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
978 struct async_icount cprev, cnow; /* kernel counter temps */
979 struct serial_icounter_struct *p_cuser; /* user space */
982 if (PORTNO(tty) == MXSER_PORTS)
983 return (mxser_ioctl_special(cmd, arg));
984 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) &&
985 (cmd != TIOCGICOUNT)) {
986 if (tty->flags & (1 << TTY_IO_ERROR))
990 case TCSBRK: /* SVID version: non-zero arg --> no break */
991 retval = tty_check_change(tty);
994 tty_wait_until_sent(tty, 0);
996 mxser_send_break(info, HZ / 4); /* 1/4 second */
998 case TCSBRKP: /* support for POSIX tcsendbreak() */
999 retval = tty_check_change(tty);
1002 tty_wait_until_sent(tty, 0);
1003 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1006 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
1008 if(get_user(templ, (unsigned long *) arg))
1011 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1012 (arg ? CLOCAL : 0));
1015 return (mxser_get_serial_info(info, (struct serial_struct *) arg));
1017 return (mxser_set_serial_info(info, (struct serial_struct *) arg));
1018 case TIOCSERGETLSR: /* Get line status register */
1019 return (mxser_get_lsr_info(info, (unsigned int *) arg));
1021 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1022 * - mask passed in arg for lines of interest
1023 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1024 * Caller should use TIOCGICOUNT to see which one it was
1029 cprev = info->icount; /* note the counters on entry */
1030 restore_flags(flags);
1032 interruptible_sleep_on(&info->delta_msr_wait);
1033 /* see if a signal did it */
1034 if (signal_pending(current))
1035 return (-ERESTARTSYS);
1038 cnow = info->icount; /* atomic copy */
1039 restore_flags(flags);
1040 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1041 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1042 return (-EIO); /* no change => error */
1043 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1044 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1045 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1046 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1053 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1054 * Return: write counters to the user passed counter struct
1055 * NB: both 1->0 and 0->1 transitions are counted except for
1056 * RI where only 0->1 is counted.
1061 cnow = info->icount;
1062 restore_flags(flags);
1063 p_cuser = (struct serial_icounter_struct *) arg;
1064 if(put_user(cnow.cts, &p_cuser->cts))
1066 if(put_user(cnow.dsr, &p_cuser->dsr))
1068 if(put_user(cnow.rng, &p_cuser->rng))
1070 return put_user(cnow.dcd, &p_cuser->dcd);
1071 case MOXA_HighSpeedOn:
1072 return put_user(info->baud_base != 115200 ? 1 : 0, (int *) arg);
1074 return (-ENOIOCTLCMD);
1079 static int mxser_ioctl_special(unsigned int cmd, unsigned long arg)
1081 int i, result, status;
1085 if(copy_to_user((struct mxser_hwconf *) arg, mxsercfg,
1086 sizeof(struct mxser_hwconf) * 4))
1089 case MOXA_GET_MAJOR:
1090 if(copy_to_user((int *) arg, &ttymajor, sizeof(int)))
1094 case MOXA_GET_CUMAJOR:
1096 if(copy_to_user((int *) arg, &result, sizeof(int)))
1100 case MOXA_CHKPORTENABLE:
1102 for (i = 0; i < MXSER_PORTS; i++) {
1103 if (mxvar_table[i].base)
1106 return put_user(result, (unsigned long *) arg);
1107 case MOXA_GETDATACOUNT:
1108 if(copy_to_user((struct mxser_log *) arg, &mxvar_log, sizeof(mxvar_log)))
1111 case MOXA_GETMSTATUS:
1112 for (i = 0; i < MXSER_PORTS; i++) {
1114 if (!mxvar_table[i].base) {
1115 GMStatus[i].dcd = 0;
1116 GMStatus[i].dsr = 0;
1117 GMStatus[i].cts = 0;
1120 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1121 GMStatus[i].cflag = mxvar_table[i].cflag;
1123 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1125 status = inb(mxvar_table[i].base + UART_MSR);
1126 if (status & 0x80 /*UART_MSR_DCD */ )
1127 GMStatus[i].dcd = 1;
1129 GMStatus[i].dcd = 0;
1131 if (status & 0x20 /*UART_MSR_DSR */ )
1132 GMStatus[i].dsr = 1;
1134 GMStatus[i].dsr = 0;
1137 if (status & 0x10 /*UART_MSR_CTS */ )
1138 GMStatus[i].cts = 1;
1140 GMStatus[i].cts = 0;
1142 if(copy_to_user((struct mxser_mstatus *) arg, GMStatus,
1143 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1147 return (-ENOIOCTLCMD);
1153 * This routine is called by the upper-layer tty layer to signal that
1154 * incoming characters should be throttled.
1156 static void mxser_throttle(struct tty_struct *tty)
1158 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1159 unsigned long flags;
1162 info->x_char = STOP_CHAR(tty);
1166 info->IER |= UART_IER_THRI;
1167 outb(info->IER, info->base + UART_IER); /* force Tx interrupt */
1168 restore_flags(flags);
1170 if (info->tty->termios->c_cflag & CRTSCTS) {
1171 info->MCR &= ~UART_MCR_RTS;
1174 outb(info->MCR, info->base + UART_MCR);
1175 restore_flags(flags);
1179 static void mxser_unthrottle(struct tty_struct *tty)
1181 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1182 unsigned long flags;
1188 info->x_char = START_CHAR(tty);
1192 info->IER |= UART_IER_THRI; /* force Tx interrupt */
1193 outb(info->IER, info->base + UART_IER);
1194 restore_flags(flags);
1197 if (info->tty->termios->c_cflag & CRTSCTS) {
1198 info->MCR |= UART_MCR_RTS;
1201 outb(info->MCR, info->base + UART_MCR);
1202 restore_flags(flags);
1206 static void mxser_set_termios(struct tty_struct *tty,
1207 struct termios *old_termios)
1209 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1211 /* 8-2-99 by William
1212 if ( (tty->termios->c_cflag == old_termios->c_cflag) &&
1213 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
1214 RELEVANT_IFLAG(old_termios->c_iflag)) )
1217 mxser_change_speed(info, old_termios);
1219 if ( (old_termios->c_cflag & CRTSCTS) &&
1220 !(tty->termios->c_cflag & CRTSCTS) ) {
1221 tty->hw_stopped = 0;
1225 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1226 (RELEVANT_IFLAG(tty->termios->c_iflag) !=
1227 RELEVANT_IFLAG(old_termios->c_iflag))) {
1229 mxser_change_speed(info, old_termios);
1231 if ((old_termios->c_cflag & CRTSCTS) &&
1232 !(tty->termios->c_cflag & CRTSCTS)) {
1233 tty->hw_stopped = 0;
1237 /* Handle sw stopped */
1238 if ((old_termios->c_iflag & IXON) &&
1239 !(tty->termios->c_iflag & IXON)) {
1246 * mxser_stop() and mxser_start()
1248 * This routines are called before setting or resetting tty->stopped.
1249 * They enable or disable transmitter interrupts, as necessary.
1251 static void mxser_stop(struct tty_struct *tty)
1253 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1254 unsigned long flags;
1258 if (info->IER & UART_IER_THRI) {
1259 info->IER &= ~UART_IER_THRI;
1260 outb(info->IER, info->base + UART_IER);
1262 restore_flags(flags);
1265 static void mxser_start(struct tty_struct *tty)
1267 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1268 unsigned long flags;
1272 if (info->xmit_cnt && info->xmit_buf &&
1273 !(info->IER & UART_IER_THRI)) {
1274 info->IER |= UART_IER_THRI;
1275 outb(info->IER, info->base + UART_IER);
1277 restore_flags(flags);
1281 * This routine is called by tty_hangup() when a hangup is signaled.
1283 void mxser_hangup(struct tty_struct *tty)
1285 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1287 mxser_flush_buffer(tty);
1288 mxser_shutdown(info);
1291 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1293 wake_up_interruptible(&info->open_wait);
1297 * This is the serial driver's generic interrupt routine
1299 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1302 struct mxser_struct *info;
1303 struct mxser_struct *port;
1304 int max, irqbits, bits, msr;
1305 int pass_counter = 0;
1309 for (i = 0; i < MXSER_BOARDS; i++) {
1310 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1316 if (i == MXSER_BOARDS)
1320 max = mxser_numports[mxsercfg[i].board_type];
1323 irqbits = inb(port->vector) & port->vectormask;
1324 if (irqbits == port->vectormask)
1327 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1328 if (irqbits == port->vectormask)
1334 (inb(info->base + UART_IIR) & UART_IIR_NO_INT))
1336 status = inb(info->base + UART_LSR) & info->read_status_mask;
1337 if (status & UART_LSR_DR)
1338 mxser_receive_chars(info, &status);
1339 msr = inb(info->base + UART_MSR);
1340 if (msr & UART_MSR_ANY_DELTA)
1341 mxser_check_modem_status(info, msr);
1342 if (status & UART_LSR_THRE) {
1343 /* 8-2-99 by William
1344 if ( info->x_char || (info->xmit_cnt > 0) )
1346 mxser_transmit_chars(info);
1349 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
1351 printk("MOXA Smartio/Indusrtio family driver interrupt loop break\n");
1353 break; /* Prevent infinite loops */
1356 return IRQ_RETVAL(handled);
1359 static inline void mxser_receive_chars(struct mxser_struct *info,
1362 struct tty_struct *tty = info->tty;
1368 ch = inb(info->base + UART_RX);
1369 if (*status & info->ignore_status_mask) {
1370 if (++ignored > 100)
1373 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1376 if (*status & UART_LSR_SPECIAL) {
1377 if (*status & UART_LSR_BI) {
1378 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
1379 if (info->flags & ASYNC_SAK)
1381 } else if (*status & UART_LSR_PE) {
1382 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
1383 } else if (*status & UART_LSR_FE) {
1384 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
1385 } else if (*status & UART_LSR_OE) {
1386 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
1388 *tty->flip.flag_buf_ptr++ = 0;
1390 *tty->flip.flag_buf_ptr++ = 0;
1391 *tty->flip.char_buf_ptr++ = ch;
1394 *status = inb(info->base + UART_LSR) & info->read_status_mask;
1395 } while (*status & UART_LSR_DR);
1396 mxvar_log.rxcnt[info->port] += cnt;
1397 schedule_delayed_work(&tty->flip.work, 1);
1401 static inline void mxser_transmit_chars(struct mxser_struct *info)
1406 outb(info->x_char, info->base + UART_TX);
1408 mxvar_log.txcnt[info->port]++;
1411 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
1412 info->tty->hw_stopped) {
1413 info->IER &= ~UART_IER_THRI;
1414 outb(info->IER, info->base + UART_IER);
1417 cnt = info->xmit_cnt;
1418 count = info->xmit_fifo_size;
1420 outb(info->xmit_buf[info->xmit_tail++], info->base + UART_TX);
1421 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1422 if (--info->xmit_cnt <= 0)
1424 } while (--count > 0);
1425 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
1427 if (info->xmit_cnt < WAKEUP_CHARS) {
1428 set_bit(MXSER_EVENT_TXLOW, &info->event);
1429 schedule_work(&info->tqueue);
1431 if (info->xmit_cnt <= 0) {
1432 info->IER &= ~UART_IER_THRI;
1433 outb(info->IER, info->base + UART_IER);
1437 static inline void mxser_check_modem_status(struct mxser_struct *info,
1441 /* update input line counters */
1442 if (status & UART_MSR_TERI)
1444 if (status & UART_MSR_DDSR)
1446 if (status & UART_MSR_DDCD)
1448 if (status & UART_MSR_DCTS)
1450 wake_up_interruptible(&info->delta_msr_wait);
1452 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
1453 if (status & UART_MSR_DCD)
1454 wake_up_interruptible(&info->open_wait);
1456 set_bit(MXSER_EVENT_HANGUP, &info->event);
1457 schedule_work(&info->tqueue);
1459 if (info->flags & ASYNC_CTS_FLOW) {
1460 if (info->tty->hw_stopped) {
1461 if (status & UART_MSR_CTS) {
1462 info->tty->hw_stopped = 0;
1463 info->IER |= UART_IER_THRI;
1464 outb(info->IER, info->base + UART_IER);
1466 set_bit(MXSER_EVENT_TXLOW, &info->event);
1467 schedule_work(&info->tqueue);
1470 if (!(status & UART_MSR_CTS)) {
1471 info->tty->hw_stopped = 1;
1472 info->IER &= ~UART_IER_THRI;
1473 outb(info->IER, info->base + UART_IER);
1479 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
1480 struct mxser_struct *info)
1482 DECLARE_WAITQUEUE(wait, current);
1483 unsigned long flags;
1488 * If the device is in the middle of being closed, then block
1489 * until it's done, and then try again.
1491 if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
1492 if (info->flags & ASYNC_CLOSING)
1493 interruptible_sleep_on(&info->close_wait);
1494 #ifdef SERIAL_DO_RESTART
1495 if (info->flags & ASYNC_HUP_NOTIFY)
1498 return (-ERESTARTSYS);
1504 * If non-blocking mode is set, or the port is not enabled,
1505 * then make the check up front and then exit.
1507 if ((filp->f_flags & O_NONBLOCK) ||
1508 (tty->flags & (1 << TTY_IO_ERROR))) {
1509 info->flags |= ASYNC_NORMAL_ACTIVE;
1512 if (tty->termios->c_cflag & CLOCAL)
1516 * Block waiting for the carrier detect and the line to become
1517 * free (i.e., not in use by the callout). While we are in
1518 * this loop, info->count is dropped by one, so that
1519 * mxser_close() knows when to free things. We restore it upon
1520 * exit, either normal or abnormal.
1523 add_wait_queue(&info->open_wait, &wait);
1526 if (!tty_hung_up_p(filp))
1528 restore_flags(flags);
1529 info->blocked_open++;
1533 outb(inb(info->base + UART_MCR) | UART_MCR_DTR | UART_MCR_RTS,
1534 info->base + UART_MCR);
1535 restore_flags(flags);
1536 set_current_state(TASK_INTERRUPTIBLE);
1537 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
1538 #ifdef SERIAL_DO_RESTART
1539 if (info->flags & ASYNC_HUP_NOTIFY)
1542 retval = -ERESTARTSYS;
1548 if (!(info->flags & ASYNC_CLOSING) &&
1549 (do_clocal || (inb(info->base + UART_MSR) & UART_MSR_DCD)))
1551 if (signal_pending(current)) {
1552 retval = -ERESTARTSYS;
1557 set_current_state(TASK_RUNNING);
1558 remove_wait_queue(&info->open_wait, &wait);
1559 if (!tty_hung_up_p(filp))
1561 info->blocked_open--;
1564 info->flags |= ASYNC_NORMAL_ACTIVE;
1568 static int mxser_startup(struct mxser_struct *info)
1570 unsigned long flags;
1573 page = get_zeroed_page(GFP_KERNEL);
1580 if (info->flags & ASYNC_INITIALIZED) {
1582 restore_flags(flags);
1585 if (!info->base || !info->type) {
1587 set_bit(TTY_IO_ERROR, &info->tty->flags);
1589 restore_flags(flags);
1595 info->xmit_buf = (unsigned char *) page;
1598 * Clear the FIFO buffers and disable them
1599 * (they will be reenabled in mxser_change_speed())
1601 if (info->xmit_fifo_size == 16)
1602 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1603 info->base + UART_FCR);
1606 * At this point there's no way the LSR could still be 0xFF;
1607 * if it is, then bail out, because there's likely no UART
1610 if (inb(info->base + UART_LSR) == 0xff) {
1611 restore_flags(flags);
1612 if (capable(CAP_SYS_ADMIN)) {
1614 set_bit(TTY_IO_ERROR, &info->tty->flags);
1620 * Clear the interrupt registers.
1622 (void) inb(info->base + UART_LSR);
1623 (void) inb(info->base + UART_RX);
1624 (void) inb(info->base + UART_IIR);
1625 (void) inb(info->base + UART_MSR);
1628 * Now, initialize the UART
1630 outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
1631 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
1632 outb(info->MCR, info->base + UART_MCR);
1635 * Finally, enable interrupts
1637 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
1638 outb(info->IER, info->base + UART_IER); /* enable interrupts */
1641 * And clear the interrupt registers again for luck.
1643 (void) inb(info->base + UART_LSR);
1644 (void) inb(info->base + UART_RX);
1645 (void) inb(info->base + UART_IIR);
1646 (void) inb(info->base + UART_MSR);
1649 test_and_clear_bit(TTY_IO_ERROR, &info->tty->flags);
1651 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1654 * and set the speed of the serial port
1656 mxser_change_speed(info, 0);
1658 info->flags |= ASYNC_INITIALIZED;
1659 restore_flags(flags);
1664 * This routine will shutdown a serial port; interrupts maybe disabled, and
1665 * DTR is dropped if the hangup on close termio flag is on.
1667 static void mxser_shutdown(struct mxser_struct *info)
1669 unsigned long flags;
1671 if (!(info->flags & ASYNC_INITIALIZED))
1675 cli(); /* Disable interrupts */
1678 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1679 * here so the queue might never be waken up
1681 wake_up_interruptible(&info->delta_msr_wait);
1684 * Free the IRQ, if necessary
1686 if (info->xmit_buf) {
1687 free_page((unsigned long) info->xmit_buf);
1691 outb(0x00, info->base + UART_IER); /* disable all intrs */
1693 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1694 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1695 outb(info->MCR, info->base + UART_MCR);
1697 /* clear Rx/Tx FIFO's */
1698 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
1699 /* read data port to reset things */
1700 (void) inb(info->base + UART_RX);
1703 set_bit(TTY_IO_ERROR, &info->tty->flags);
1705 info->flags &= ~ASYNC_INITIALIZED;
1706 restore_flags(flags);
1710 * This routine is called to set the UART divisor registers to match
1711 * the specified baud rate for a serial port.
1713 static int mxser_change_speed(struct mxser_struct *info,
1714 struct termios *old_termios)
1717 unsigned cflag, cval, fcr;
1720 unsigned long flags;
1722 if (!info->tty || !info->tty->termios)
1724 cflag = info->tty->termios->c_cflag;
1729 #define B921600 (B460800 +1)
1731 switch (cflag & (CBAUD | CBAUDEX)) {
1798 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1799 i = 16; /* 57600 bps */
1800 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1801 i = 17; /* 115200 bps */
1803 #ifdef ASYNC_SPD_SHI
1804 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1808 #ifdef ASYNC_SPD_WARP
1809 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1813 if (mxvar_baud_table[i] == 134) {
1814 quot = (2 * info->baud_base / 269);
1815 } else if (mxvar_baud_table[i]) {
1816 quot = info->baud_base / mxvar_baud_table[i];
1817 if (!quot && old_termios) {
1819 info->tty->termios->c_cflag &= ~CBAUD;
1820 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1821 switch (info->tty->termios->c_cflag & (CBAUD | CBAUDEX)) {
1887 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1888 i = 16; /* 57600 bps */
1889 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1890 i = 17; /* 115200 bps */
1891 #ifdef ASYNC_SPD_SHI
1892 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1895 #ifdef ASYNC_SPD_WARP
1896 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1900 if (mxvar_baud_table[i] == 134) {
1901 quot = (2 * info->baud_base / 269);
1902 } else if (mxvar_baud_table[i]) {
1903 quot = info->baud_base / mxvar_baud_table[i];
1909 } else if (quot == 0)
1916 info->MCR |= UART_MCR_DTR;
1919 outb(info->MCR, info->base + UART_MCR);
1920 restore_flags(flags);
1922 info->MCR &= ~UART_MCR_DTR;
1925 outb(info->MCR, info->base + UART_MCR);
1926 restore_flags(flags);
1929 /* byte size and parity */
1930 switch (cflag & CSIZE) {
1945 break; /* too keep GCC shut... */
1950 cval |= UART_LCR_PARITY;
1951 if (!(cflag & PARODD))
1952 cval |= UART_LCR_EPAR;
1953 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
1956 fcr = UART_FCR_ENABLE_FIFO;
1957 switch (info->rx_trigger) {
1959 fcr |= UART_FCR_TRIGGER_1;
1962 fcr |= UART_FCR_TRIGGER_4;
1965 fcr |= UART_FCR_TRIGGER_8;
1968 fcr |= UART_FCR_TRIGGER_14;
1972 /* CTS flow control flag and modem status interrupts */
1973 info->IER &= ~UART_IER_MSI;
1974 info->MCR &= ~UART_MCR_AFE;
1975 if (cflag & CRTSCTS) {
1976 info->flags |= ASYNC_CTS_FLOW;
1977 info->IER |= UART_IER_MSI;
1978 if (info->type == PORT_16550A)
1979 info->MCR |= UART_MCR_AFE;
1981 info->flags &= ~ASYNC_CTS_FLOW;
1983 outb(info->MCR, info->base + UART_MCR);
1985 info->flags &= ~ASYNC_CHECK_CD;
1987 info->flags |= ASYNC_CHECK_CD;
1988 info->IER |= UART_IER_MSI;
1990 outb(info->IER, info->base + UART_IER);
1993 * Set up parity check flag
1995 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1996 if (I_INPCK(info->tty))
1997 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1998 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1999 info->read_status_mask |= UART_LSR_BI;
2001 info->ignore_status_mask = 0;
2003 /* This should be safe, but for some broken bits of hardware... */
2004 if (I_IGNPAR(info->tty)) {
2005 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2006 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
2009 if (I_IGNBRK(info->tty)) {
2010 info->ignore_status_mask |= UART_LSR_BI;
2011 info->read_status_mask |= UART_LSR_BI;
2013 * If we're ignore parity and break indicators, ignore
2014 * overruns too. (For real raw support).
2016 if (I_IGNPAR(info->tty)) {
2017 info->ignore_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2018 info->read_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2023 outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2024 outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2025 outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2026 outb(cval, info->base + UART_LCR); /* reset DLAB */
2027 outb(fcr, info->base + UART_FCR); /* set fcr */
2028 restore_flags(flags);
2034 * ------------------------------------------------------------
2035 * friends of mxser_ioctl()
2036 * ------------------------------------------------------------
2038 static int mxser_get_serial_info(struct mxser_struct *info,
2039 struct serial_struct *retinfo)
2041 struct serial_struct tmp;
2045 memset(&tmp, 0, sizeof(tmp));
2046 tmp.type = info->type;
2047 tmp.line = info->port;
2048 tmp.port = info->base;
2049 tmp.irq = info->irq;
2050 tmp.flags = info->flags;
2051 tmp.baud_base = info->baud_base;
2052 tmp.close_delay = info->close_delay;
2053 tmp.closing_wait = info->closing_wait;
2054 tmp.custom_divisor = info->custom_divisor;
2056 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2059 static int mxser_set_serial_info(struct mxser_struct *info,
2060 struct serial_struct *new_info)
2062 struct serial_struct new_serial;
2066 if (!new_info || !info->base)
2068 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2071 if ((new_serial.irq != info->irq) ||
2072 (new_serial.port != info->base) ||
2073 (new_serial.type != info->type) ||
2074 (new_serial.custom_divisor != info->custom_divisor) ||
2075 (new_serial.baud_base != info->baud_base))
2078 flags = info->flags & ASYNC_SPD_MASK;
2080 if (!capable(CAP_SYS_ADMIN)) {
2081 if ((new_serial.baud_base != info->baud_base) ||
2082 (new_serial.close_delay != info->close_delay) ||
2083 ((new_serial.flags & ~ASYNC_USR_MASK) !=
2084 (info->flags & ~ASYNC_USR_MASK)))
2086 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2087 (new_serial.flags & ASYNC_USR_MASK));
2090 * OK, past this point, all the error checking has been done.
2091 * At this point, we start making changes.....
2093 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2094 (new_serial.flags & ASYNC_FLAGS));
2095 info->close_delay = new_serial.close_delay * HZ / 100;
2096 info->closing_wait = new_serial.closing_wait * HZ / 100;
2099 if (info->flags & ASYNC_INITIALIZED) {
2100 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2101 mxser_change_speed(info, 0);
2104 retval = mxser_startup(info);
2109 * mxser_get_lsr_info - get line status register info
2111 * Purpose: Let user call ioctl() to get info when the UART physically
2112 * is emptied. On bus types like RS485, the transmitter must
2113 * release the bus after transmitting. This must be done when
2114 * the transmit shift register is empty, not be done when the
2115 * transmit holding register is empty. This functionality
2116 * allows an RS485 driver to be written in user space.
2118 static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int *value)
2120 unsigned char status;
2121 unsigned int result;
2122 unsigned long flags;
2126 status = inb(info->base + UART_LSR);
2127 restore_flags(flags);
2128 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2129 return put_user(result, value);
2133 * This routine sends a break character out the serial port.
2135 static void mxser_send_break(struct mxser_struct *info, int duration)
2137 unsigned long flags;
2140 set_current_state(TASK_INTERRUPTIBLE);
2143 outb(inb(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
2144 schedule_timeout(duration);
2145 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
2146 restore_flags(flags);
2149 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2151 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
2152 unsigned char control, status;
2153 unsigned long flags;
2155 if (PORTNO(tty) == MXSER_PORTS)
2156 return (-ENOIOCTLCMD);
2157 if (tty->flags & (1 << TTY_IO_ERROR))
2160 control = info->MCR;
2163 status = inb(info->base + UART_MSR);
2164 if (status & UART_MSR_ANY_DELTA)
2165 mxser_check_modem_status(info, status);
2166 restore_flags(flags);
2167 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2168 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2169 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2170 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2171 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2172 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2175 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
2176 unsigned int set, unsigned int clear)
2178 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
2179 unsigned long flags;
2181 if (PORTNO(tty) == MXSER_PORTS)
2182 return (-ENOIOCTLCMD);
2183 if (tty->flags & (1 << TTY_IO_ERROR))
2188 if (set & TIOCM_RTS)
2189 info->MCR |= UART_MCR_RTS;
2190 if (set & TIOCM_DTR)
2191 info->MCR |= UART_MCR_DTR;
2193 if (clear & TIOCM_RTS)
2194 info->MCR &= ~UART_MCR_RTS;
2195 if (clear & TIOCM_DTR)
2196 info->MCR &= ~UART_MCR_DTR;
2198 outb(info->MCR, info->base + UART_MCR);
2199 restore_flags(flags);
2203 static int mxser_read_register(int, unsigned short *);
2204 static int mxser_program_mode(int);
2205 static void mxser_normal_mode(int);
2207 static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2210 unsigned short regs[16], irq;
2211 unsigned char scratch, scratch2;
2213 id = mxser_read_register(cap, regs);
2214 if (id == C168_ASIC_ID)
2215 hwconf->board_type = MXSER_BOARD_C168_ISA;
2216 else if (id == C104_ASIC_ID)
2217 hwconf->board_type = MXSER_BOARD_C104_ISA;
2218 else if (id == C102_ASIC_ID)
2219 hwconf->board_type = MXSER_BOARD_C102_ISA;
2220 else if (id == CI132_ASIC_ID)
2221 hwconf->board_type = MXSER_BOARD_CI132;
2222 else if (id == CI134_ASIC_ID)
2223 hwconf->board_type = MXSER_BOARD_CI134;
2224 else if (id == CI104J_ASIC_ID)
2225 hwconf->board_type = MXSER_BOARD_CI104J;
2228 irq = regs[9] & 0x0F;
2229 irq = irq | (irq << 4);
2230 irq = irq | (irq << 8);
2231 if ((irq != regs[9]) || ((id == 1) && (irq != regs[10]))) {
2232 return (MXSER_ERR_IRQ_CONFLIT);
2235 return (MXSER_ERR_IRQ);
2237 for (i = 0; i < 8; i++)
2238 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
2239 hwconf->irq = (int) (irq & 0x0F);
2240 if ((regs[12] & 0x80) == 0) {
2241 return (MXSER_ERR_VECTOR);
2243 hwconf->vector = (int) regs[11]; /* interrupt vector */
2245 hwconf->vector_mask = 0x00FF;
2247 hwconf->vector_mask = 0x000F;
2248 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2249 if (regs[12] & bits)
2250 hwconf->baud_base[i] = 921600;
2252 hwconf->baud_base[i] = 115200;
2254 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2255 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2256 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2257 outb(scratch2, cap + UART_LCR);
2258 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2259 scratch = inb(cap + UART_IIR);
2261 hwconf->uart_type = PORT_16550A;
2263 hwconf->uart_type = PORT_16450;
2268 return (hwconf->ports);
2271 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
2272 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
2273 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
2274 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
2275 #define EN_CCMD 0x000 /* Chip's command register */
2276 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
2277 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
2278 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
2279 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
2280 #define EN0_DCFG 0x00E /* Data configuration reg WR */
2281 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
2282 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
2283 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
2284 static int mxser_read_register(int port, unsigned short *regs)
2286 int i, k, value, id;
2289 id = mxser_program_mode(port);
2292 for (i = 0; i < 14; i++) {
2293 k = (i & 0x3F) | 0x180;
2294 for (j = 0x100; j > 0; j >>= 1) {
2295 outb(CHIP_CS, port);
2297 outb(CHIP_CS | CHIP_DO, port);
2298 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
2300 outb(CHIP_CS, port);
2301 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
2306 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
2307 outb(CHIP_CS, port);
2308 outb(CHIP_CS | CHIP_SK, port);
2309 if (inb(port) & CHIP_DI)
2315 mxser_normal_mode(port);
2319 static int mxser_program_mode(int port)
2322 unsigned long flags;
2333 restore_flags(flags);
2334 id = inb(port + 1) & 0x1F;
2335 if ((id != C168_ASIC_ID) && (id != C104_ASIC_ID) && (id != CI104J_ASIC_ID) &&
2336 (id != C102_ASIC_ID) && (id != CI132_ASIC_ID) && (id != CI134_ASIC_ID))
2338 for (i = 0, j = 0; i < 4; i++) {
2342 } else if ((j == 1) && (n == 1)) {
2353 static void mxser_normal_mode(int port)
2357 outb(0xA5, port + 1);
2358 outb(0x80, port + 3);
2359 outb(12, port + 0); /* 9600 bps */
2361 outb(0x03, port + 3); /* 8 data bits */
2362 outb(0x13, port + 4); /* loop back mode */
2363 for (i = 0; i < 16; i++) {
2365 if ((n & 0x61) == 0x60)
2370 outb(0x00, port + 4);
2373 module_init(mxser_module_init);
2374 module_exit(mxser_module_exit);