1 /*****************************************************************************/
3 * mxser.c -- MOXA Smartio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * MOXA Smartio Family Serial Driver
28 * Copyright (C) 1999,2000 Moxa Technologies Co., LTD.
30 * for : LINUX 2.0.X, 2.2.X, 2.4.X
34 * Fixes for C104H/PCI by Tim Hockin <thockin@sun.com>
35 * Added support for: C102, CI-132, CI-134, CP-132, CP-114, CT-114 cards
36 * by Damian Wrobel <dwrobel@ertel.com.pl>
38 * Added support for serial card CP104
39 * by James Nelson Provident Solutions <linux-info@provident-solutions.com>
42 #include <linux/config.h>
43 #include <linux/module.h>
44 #include <linux/errno.h>
45 #include <linux/signal.h>
46 #include <linux/sched.h>
47 #include <linux/timer.h>
48 #include <linux/interrupt.h>
49 #include <linux/tty.h>
50 #include <linux/tty_flip.h>
51 #include <linux/serial.h>
52 #include <linux/serial_reg.h>
53 #include <linux/major.h>
54 #include <linux/string.h>
55 #include <linux/fcntl.h>
56 #include <linux/ptrace.h>
57 #include <linux/ioport.h>
59 #include <linux/smp_lock.h>
60 #include <linux/pci.h>
61 #include <linux/init.h>
63 #include <asm/system.h>
66 #include <asm/bitops.h>
67 #include <asm/uaccess.h>
69 #define MXSER_VERSION "1.2.1"
71 #define MXSERMAJOR 174
72 #define MXSERCUMAJOR 175
75 #define MXSER_EVENT_TXLOW 1
76 #define MXSER_EVENT_HANGUP 2
79 #define SERIAL_DO_RESTART
81 #define MXSER_BOARDS 4 /* Max. boards */
82 #define MXSER_PORTS 32 /* Max. ports */
83 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
84 #define MXSER_ISR_PASS_LIMIT 256
86 #define MXSER_ERR_IOADDR -1
87 #define MXSER_ERR_IRQ -2
88 #define MXSER_ERR_IRQ_CONFLIT -3
89 #define MXSER_ERR_VECTOR -4
91 #define SERIAL_TYPE_NORMAL 1
93 #define WAKEUP_CHARS 256
95 #define UART_MCR_AFE 0x20
96 #define UART_LSR_SPECIAL 0x1E
98 #define PORTNO(x) ((x)->index)
100 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
102 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
105 #define MIN(a,b) ((a) < (b) ? (a) : (b))
109 * Define the Moxa PCI vendor and device IDs.
112 #ifndef PCI_VENDOR_ID_MOXA
113 #define PCI_VENDOR_ID_MOXA 0x1393
115 #ifndef PCI_DEVICE_ID_C168
116 #define PCI_DEVICE_ID_C168 0x1680
118 #ifndef PCI_DEVICE_ID_C104
119 #define PCI_DEVICE_ID_C104 0x1040
121 #ifndef PCI_DEVICE_ID_CP104
122 #define PCI_DEVICE_ID_CP104 0x1041
124 #ifndef PCI_DEVICE_ID_CP132
125 #define PCI_DEVICE_ID_CP132 0x1320
127 #ifndef PCI_DEVICE_ID_CP114
128 #define PCI_DEVICE_ID_CP114 0x1141
130 #ifndef PCI_DEVICE_ID_CT114
131 #define PCI_DEVICE_ID_CT114 0x1140
134 #define C168_ASIC_ID 1
135 #define C104_ASIC_ID 2
136 #define CI134_ASIC_ID 3
137 #define CI132_ASIC_ID 4
138 #define CI104J_ASIC_ID 5
139 #define C102_ASIC_ID 0xB
142 MXSER_BOARD_C168_ISA = 0,
143 MXSER_BOARD_C104_ISA,
145 MXSER_BOARD_C168_PCI,
146 MXSER_BOARD_C104_PCI,
147 MXSER_BOARD_CP104_PCI,
148 MXSER_BOARD_C102_ISA,
151 MXSER_BOARD_CP132_PCI,
152 MXSER_BOARD_CP114_PCI,
153 MXSER_BOARD_CT114_PCI
156 static char *mxser_brdname[] =
172 static int mxser_numports[] =
192 #define MOXA_GETDATACOUNT (MOXA + 23)
193 #define MOXA_GET_CONF (MOXA + 35)
194 #define MOXA_DIAGNOSE (MOXA + 50)
195 #define MOXA_CHKPORTENABLE (MOXA + 60)
196 #define MOXA_HighSpeedOn (MOXA + 61)
197 #define MOXA_GET_MAJOR (MOXA + 63)
198 #define MOXA_GET_CUMAJOR (MOXA + 64)
199 #define MOXA_GETMSTATUS (MOXA + 65)
201 static struct pci_device_id mxser_pcibrds[] = {
202 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C168, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
203 MXSER_BOARD_C168_PCI },
204 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C104, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
205 MXSER_BOARD_C104_PCI },
206 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP104, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
207 MXSER_BOARD_CP104_PCI },
208 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP132, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
209 MXSER_BOARD_CP132_PCI },
210 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP114, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
211 MXSER_BOARD_CP114_PCI },
212 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CT114, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
213 MXSER_BOARD_CT114_PCI },
216 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
218 static int ioaddr[MXSER_BOARDS];
219 static int ttymajor = MXSERMAJOR;
222 /* Variables for insmod */
224 MODULE_AUTHOR("William Chen");
225 MODULE_DESCRIPTION("MOXA Smartio Family Multiport Board Device Driver");
226 MODULE_LICENSE("GPL");
227 MODULE_PARM(ioaddr, "1-4i");
228 MODULE_PARM(ttymajor, "i");
229 MODULE_PARM(verbose, "i");
231 struct mxser_hwconf {
238 int ioaddr[MXSER_PORTS_PER_BOARD];
239 int baud_base[MXSER_PORTS_PER_BOARD];
240 struct pci_dev *pdev;
243 struct mxser_struct {
245 int base; /* port base address */
246 int irq; /* port using irq no. */
247 int vector; /* port irq vector */
248 int vectormask; /* port vector mask */
249 int rx_trigger; /* Rx fifo trigger level */
250 int baud_base; /* max. speed */
251 int flags; /* defined in tty.h */
252 int type; /* UART type */
253 struct tty_struct *tty;
254 int read_status_mask;
255 int ignore_status_mask;
258 int x_char; /* xon/xoff character */
260 unsigned short closing_wait;
261 int IER; /* Interrupt Enable Register */
262 int MCR; /* Modem control register */
264 int count; /* # of fd on device */
265 int blocked_open; /* # of blocked opens */
266 unsigned char *xmit_buf;
270 struct work_struct tqueue;
272 wait_queue_head_t open_wait;
273 wait_queue_head_t close_wait;
274 wait_queue_head_t delta_msr_wait;
275 struct async_icount icount; /* kernel counters for the 4 input interrupts */
280 int rxcnt[MXSER_PORTS];
281 int txcnt[MXSER_PORTS];
284 struct mxser_mstatus {
292 static struct mxser_mstatus GMStatus[MXSER_PORTS];
294 static int mxserBoardCAP[MXSER_BOARDS] =
297 /* 0x180, 0x280, 0x200, 0x320 */
301 static struct tty_driver *mxvar_sdriver;
302 static struct mxser_struct mxvar_table[MXSER_PORTS];
303 static struct mxser_log mxvar_log;
304 static int mxvar_diagflag;
306 * mxvar_tmp_buf is used as a temporary buffer by serial_write. We need
307 * to lock it in case the memcpy_fromfs blocks while swapping in a page,
308 * and some other program tries to do a serial write at the same time.
309 * Since the lock will only come under contention when the system is
310 * swapping and available memory is low, it makes sense to share one
311 * buffer across all the serial ports, since it significantly saves
312 * memory if large numbers of serial ports are open.
314 static unsigned char *mxvar_tmp_buf;
315 static struct semaphore mxvar_tmp_buf_sem;
318 * This is used to figure out the divisor speeds and the timeouts
320 static int mxvar_baud_table[] =
322 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
323 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 0};
325 struct mxser_hwconf mxsercfg[MXSER_BOARDS];
331 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
332 static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
333 static int mxser_get_PCI_conf(struct pci_dev *, int, struct mxser_hwconf *);
334 static void mxser_do_softint(void *);
335 static int mxser_open(struct tty_struct *, struct file *);
336 static void mxser_close(struct tty_struct *, struct file *);
337 static int mxser_write(struct tty_struct *, int, const unsigned char *, int);
338 static int mxser_write_room(struct tty_struct *);
339 static void mxser_flush_buffer(struct tty_struct *);
340 static int mxser_chars_in_buffer(struct tty_struct *);
341 static void mxser_flush_chars(struct tty_struct *);
342 static void mxser_put_char(struct tty_struct *, unsigned char);
343 static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
344 static int mxser_ioctl_special(unsigned int, unsigned long);
345 static void mxser_throttle(struct tty_struct *);
346 static void mxser_unthrottle(struct tty_struct *);
347 static void mxser_set_termios(struct tty_struct *, struct termios *);
348 static void mxser_stop(struct tty_struct *);
349 static void mxser_start(struct tty_struct *);
350 static void mxser_hangup(struct tty_struct *);
351 static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
352 static inline void mxser_receive_chars(struct mxser_struct *, int *);
353 static inline void mxser_transmit_chars(struct mxser_struct *);
354 static inline void mxser_check_modem_status(struct mxser_struct *, int);
355 static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
356 static int mxser_startup(struct mxser_struct *);
357 static void mxser_shutdown(struct mxser_struct *);
358 static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
359 static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct *);
360 static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct *);
361 static int mxser_get_lsr_info(struct mxser_struct *, unsigned int *);
362 static void mxser_send_break(struct mxser_struct *, int);
363 static int mxser_tiocmget(struct tty_struct *, struct file *);
364 static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
367 * The MOXA C168/C104 serial driver boot-time initialization code!
370 static void __exit mxser_module_exit(void)
376 printk("Unloading module mxser ...\n");
377 if ((err |= tty_unregister_driver(mxvar_sdriver)))
378 printk("Couldn't unregister MOXA Smartio family serial driver\n");
379 put_tty_driver(mxvar_sdriver);
381 for (i = 0; i < MXSER_BOARDS; i++) {
382 if (mxsercfg[i].board_type == -1)
385 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
394 int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
396 struct mxser_struct *info;
401 init_MUTEX(&mxvar_tmp_buf_sem);
403 n = board * MXSER_PORTS_PER_BOARD;
404 info = &mxvar_table[n];
405 for (i = 0; i < hwconf->ports; i++, n++, info++) {
407 printk(" ttyM%d/cum%d at 0x%04x ", n, n, hwconf->ioaddr[i]);
408 if (hwconf->baud_base[i] == 115200)
409 printk(" max. baud rate up to 115200 bps.\n");
411 printk(" max. baud rate up to 921600 bps.\n");
414 info->base = hwconf->ioaddr[i];
415 info->irq = hwconf->irq;
416 info->vector = hwconf->vector;
417 info->vectormask = hwconf->vector_mask;
418 info->rx_trigger = 14;
419 info->baud_base = hwconf->baud_base[i];
420 info->flags = ASYNC_SHARE_IRQ;
421 info->type = hwconf->uart_type;
422 if ((info->type == PORT_16450) || (info->type == PORT_8250))
423 info->xmit_fifo_size = 1;
425 info->xmit_fifo_size = 16;
426 info->custom_divisor = hwconf->baud_base[i] * 16;
427 info->close_delay = 5 * HZ / 10;
428 info->closing_wait = 30 * HZ;
429 INIT_WORK(&info->tqueue, mxser_do_softint, info);
430 info->cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
431 init_waitqueue_head(&info->open_wait);
432 init_waitqueue_head(&info->close_wait);
433 init_waitqueue_head(&info->delta_msr_wait);
437 * Allocate the IRQ if necessary
441 n = board * MXSER_PORTS_PER_BOARD;
442 info = &mxvar_table[n];
445 retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
448 restore_flags(flags);
449 printk("Board %d: %s", board, mxser_brdname[hwconf->board_type]);
450 printk(" Request irq fail,IRQ (%d) may be conflit with another device.\n", info->irq);
453 restore_flags(flags);
459 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
461 mxsercfg[board] = *hwconf;
464 static int mxser_get_PCI_conf(struct pci_dev *pdev, int board_type, struct mxser_hwconf *hwconf)
467 unsigned int ioaddress;
469 hwconf->board_type = board_type;
470 hwconf->ports = mxser_numports[board_type];
471 ioaddress = pci_resource_start (pdev, 2);
472 for (i = 0; i < hwconf->ports; i++)
473 hwconf->ioaddr[i] = ioaddress + 8 * i;
475 ioaddress = pci_resource_start (pdev, 3);
476 hwconf->vector = ioaddress;
478 hwconf->irq = pdev->irq;
480 hwconf->uart_type = PORT_16550A;
481 hwconf->vector_mask = 0;
482 for (i = 0; i < hwconf->ports; i++) {
483 hwconf->vector_mask |= (1 << i);
484 hwconf->baud_base[i] = 921600;
489 static struct tty_operations mxser_ops = {
491 .close = mxser_close,
492 .write = mxser_write,
493 .put_char = mxser_put_char,
494 .flush_chars = mxser_flush_chars,
495 .write_room = mxser_write_room,
496 .chars_in_buffer = mxser_chars_in_buffer,
497 .flush_buffer = mxser_flush_buffer,
498 .ioctl = mxser_ioctl,
499 .throttle = mxser_throttle,
500 .unthrottle = mxser_unthrottle,
501 .set_termios = mxser_set_termios,
503 .start = mxser_start,
504 .hangup = mxser_hangup,
505 .tiocmget = mxser_tiocmget,
506 .tiocmset = mxser_tiocmset,
509 static int __init mxser_module_init(void)
512 struct mxser_hwconf hwconf;
514 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
518 printk("MOXA Smartio family driver version %s\n", MXSER_VERSION);
520 /* Initialize the tty_driver structure */
522 mxvar_sdriver->owner = THIS_MODULE;
523 mxvar_sdriver->name = "ttyM";
524 mxvar_sdriver->devfs_name = "tts/M";
525 mxvar_sdriver->major = ttymajor;
526 mxvar_sdriver->minor_start = 0;
527 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
528 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
529 mxvar_sdriver->init_termios = tty_std_termios;
530 mxvar_sdriver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
531 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
532 tty_set_operations(mxvar_sdriver, &mxser_ops);
533 printk("Tty devices major number = %d\n", ttymajor);
536 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
537 memset(&mxvar_log, 0, sizeof(struct mxser_log));
541 /* Start finding ISA boards here */
542 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
544 if (!(cap = mxserBoardCAP[b]))
547 retval = mxser_get_ISA_conf(cap, &hwconf);
550 printk("Found MOXA %s board (CAP=0x%x)\n",
551 mxser_brdname[hwconf.board_type],
555 if (retval == MXSER_ERR_IRQ)
556 printk("Invalid interrupt number,board not configured\n");
557 else if (retval == MXSER_ERR_IRQ_CONFLIT)
558 printk("Invalid interrupt number,board not configured\n");
559 else if (retval == MXSER_ERR_VECTOR)
560 printk("Invalid interrupt vector,board not configured\n");
561 else if (retval == MXSER_ERR_IOADDR)
562 printk("Invalid I/O address,board not configured\n");
568 if (mxser_initbrd(m, &hwconf) < 0)
571 mxser_getcfg(m, &hwconf);
576 /* Start finding ISA boards from module arg */
577 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
579 if (!(cap = ioaddr[b]))
582 retval = mxser_get_ISA_conf(cap, &hwconf);
585 printk("Found MOXA %s board (CAP=0x%x)\n",
586 mxser_brdname[hwconf.board_type],
590 if (retval == MXSER_ERR_IRQ)
591 printk("Invalid interrupt number,board not configured\n");
592 else if (retval == MXSER_ERR_IRQ_CONFLIT)
593 printk("Invalid interrupt number,board not configured\n");
594 else if (retval == MXSER_ERR_VECTOR)
595 printk("Invalid interrupt vector,board not configured\n");
596 else if (retval == MXSER_ERR_IOADDR)
597 printk("Invalid I/O address,board not configured\n");
603 if (mxser_initbrd(m, &hwconf) < 0)
606 mxser_getcfg(m, &hwconf);
611 /* start finding PCI board here */
615 struct pci_dev *pdev = NULL;
616 int n = (sizeof(mxser_pcibrds) / sizeof(mxser_pcibrds[0])) - 1;
617 for (b = 0; b < n; b++) {
618 while ((pdev = pci_find_device(mxser_pcibrds[b].vendor, mxser_pcibrds[b].device, pdev)))
620 if (pci_enable_device(pdev))
623 printk("Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
624 mxser_brdname[mxser_pcibrds[b].driver_data],
625 pdev->bus->number, PCI_SLOT(pdev->devfn));
626 if (m >= MXSER_BOARDS) {
627 printk("Too many Smartio family boards found (maximum %d),board not configured\n", MXSER_BOARDS);
629 retval = mxser_get_PCI_conf(pdev, mxser_pcibrds[b].driver_data, &hwconf);
631 if (retval == MXSER_ERR_IRQ)
632 printk("Invalid interrupt number,board not configured\n");
633 else if (retval == MXSER_ERR_IRQ_CONFLIT)
634 printk("Invalid interrupt number,board not configured\n");
635 else if (retval == MXSER_ERR_VECTOR)
636 printk("Invalid interrupt vector,board not configured\n");
637 else if (retval == MXSER_ERR_IOADDR)
638 printk("Invalid I/O address,board not configured\n");
641 if (mxser_initbrd(m, &hwconf) < 0)
643 mxser_getcfg(m, &hwconf);
651 for (i = m; i < MXSER_BOARDS; i++) {
652 mxsercfg[i].board_type = -1;
656 if (!tty_register_driver(mxvar_sdriver))
659 put_tty_driver(mxvar_sdriver);
660 printk("Couldn't install MOXA Smartio family driver !\n");
662 for (i = 0; i < MXSER_BOARDS; i++) {
663 if (mxsercfg[i].board_type == -1)
665 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
670 static void mxser_do_softint(void *private_)
672 struct mxser_struct *info = (struct mxser_struct *) private_;
673 struct tty_struct *tty;
677 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event)) {
678 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
679 tty->ldisc.write_wakeup)
680 (tty->ldisc.write_wakeup) (tty);
681 wake_up_interruptible(&tty->write_wait);
683 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event)) {
684 tty_hangup(tty); /* FIXME: module removal race here - AKPM */
690 * This routine is called whenever a serial port is opened. It
691 * enables interrupts for a serial port, linking in its async structure into
692 * the IRQ chain. It also performs the serial-specific
693 * initialization for the tty structure.
696 static int mxser_open(struct tty_struct *tty, struct file *filp)
698 struct mxser_struct *info;
703 if (line == MXSER_PORTS)
705 if ((line < 0) || (line > MXSER_PORTS))
707 info = mxvar_table + line;
712 tty->driver_data = info;
715 if (!mxvar_tmp_buf) {
716 page = get_zeroed_page(GFP_KERNEL);
722 mxvar_tmp_buf = (unsigned char *) page;
725 * Start up serial port
727 retval = mxser_startup(info);
731 return mxser_block_til_ready(tty, filp, info);
735 * This routine is called when the serial port gets closed. First, we
736 * wait for the last remaining data to be sent. Then, we unlink its
737 * async structure from the interrupt chain if necessary, and we free
738 * that IRQ if nothing is left in the chain.
741 static void mxser_close(struct tty_struct *tty, struct file *filp)
743 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
745 unsigned long timeout;
747 if (PORTNO(tty) == MXSER_PORTS)
755 if (tty_hung_up_p(filp)) {
756 restore_flags(flags);
759 if ((tty->count == 1) && (info->count != 1)) {
761 * Uh, oh. tty->count is 1, which means that the tty
762 * structure will be freed. Info->count should always
763 * be one in these conditions. If it's greater than
764 * one, we've got real problems, since it means the
765 * serial port won't be shutdown.
767 printk("mxser_close: bad serial port count; tty->count is 1, "
768 "info->count is %d\n", info->count);
771 if (--info->count < 0) {
772 printk("mxser_close: bad serial port count for ttys%d: %d\n",
773 info->port, info->count);
777 restore_flags(flags);
780 info->flags |= ASYNC_CLOSING;
781 info->cflag = tty->termios->c_cflag;
783 * Now we wait for the transmit buffer to clear; and we notify
784 * the line discipline to only process XON/XOFF characters.
787 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
788 tty_wait_until_sent(tty, info->closing_wait);
790 * At this point we stop accepting input. To do this, we
791 * disable the receive line status interrupts, and tell the
792 * interrupt driver to stop checking the data ready bit in the
793 * line status register.
795 info->IER &= ~UART_IER_RLSI;
797 info->read_status_mask &= ~UART_LSR_DR;
799 if (info->flags & ASYNC_INITIALIZED) {
800 outb(info->IER, info->base + UART_IER);
802 * Before we drop DTR, make sure the UART transmitter
803 * has completely drained; this is especially
804 * important if there is a transmit FIFO!
806 timeout = jiffies + HZ;
807 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
808 set_current_state(TASK_INTERRUPTIBLE);
810 if (time_after(jiffies, timeout))
814 mxser_shutdown(info);
815 if (tty->driver->flush_buffer)
816 tty->driver->flush_buffer(tty);
817 if (tty->ldisc.flush_buffer)
818 tty->ldisc.flush_buffer(tty);
822 if (info->blocked_open) {
823 if (info->close_delay) {
824 set_current_state(TASK_INTERRUPTIBLE);
825 schedule_timeout(info->close_delay);
827 wake_up_interruptible(&info->open_wait);
829 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
830 wake_up_interruptible(&info->close_wait);
831 restore_flags(flags);
835 static int mxser_write(struct tty_struct *tty, int from_user,
836 const unsigned char *buf, int count)
839 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
842 if (!tty || !info->xmit_buf || !mxvar_tmp_buf)
847 down(&mxvar_tmp_buf_sem);
849 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
850 SERIAL_XMIT_SIZE - info->xmit_head));
854 c -= copy_from_user(mxvar_tmp_buf, buf, c);
862 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
863 SERIAL_XMIT_SIZE - info->xmit_head));
864 memcpy(info->xmit_buf + info->xmit_head, mxvar_tmp_buf, c);
865 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
867 restore_flags(flags);
873 up(&mxvar_tmp_buf_sem);
877 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
878 SERIAL_XMIT_SIZE - info->xmit_head));
880 restore_flags(flags);
884 memcpy(info->xmit_buf + info->xmit_head, buf, c);
885 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
887 restore_flags(flags);
896 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
897 !(info->IER & UART_IER_THRI)) {
898 info->IER |= UART_IER_THRI;
899 outb(info->IER, info->base + UART_IER);
901 restore_flags(flags);
905 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
907 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
910 if (!tty || !info->xmit_buf)
915 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
916 restore_flags(flags);
919 info->xmit_buf[info->xmit_head++] = ch;
920 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
922 /********************************************** why ??? ***********
923 if ( !tty->stopped && !tty->hw_stopped &&
924 !(info->IER & UART_IER_THRI) ) {
925 info->IER |= UART_IER_THRI;
926 outb(info->IER, info->base + UART_IER);
928 *****************************************************************/
929 restore_flags(flags);
932 static void mxser_flush_chars(struct tty_struct *tty)
934 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
937 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
943 info->IER |= UART_IER_THRI;
944 outb(info->IER, info->base + UART_IER);
945 restore_flags(flags);
948 static int mxser_write_room(struct tty_struct *tty)
950 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
953 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
959 static int mxser_chars_in_buffer(struct tty_struct *tty)
961 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
963 return (info->xmit_cnt);
966 static void mxser_flush_buffer(struct tty_struct *tty)
968 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
973 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
974 restore_flags(flags);
975 wake_up_interruptible(&tty->write_wait);
976 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
977 tty->ldisc.write_wakeup)
978 (tty->ldisc.write_wakeup) (tty);
981 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
982 unsigned int cmd, unsigned long arg)
985 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
987 struct async_icount cprev, cnow; /* kernel counter temps */
988 struct serial_icounter_struct *p_cuser; /* user space */
991 if (PORTNO(tty) == MXSER_PORTS)
992 return (mxser_ioctl_special(cmd, arg));
993 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) &&
994 (cmd != TIOCGICOUNT)) {
995 if (tty->flags & (1 << TTY_IO_ERROR))
999 case TCSBRK: /* SVID version: non-zero arg --> no break */
1000 retval = tty_check_change(tty);
1003 tty_wait_until_sent(tty, 0);
1005 mxser_send_break(info, HZ / 4); /* 1/4 second */
1007 case TCSBRKP: /* support for POSIX tcsendbreak() */
1008 retval = tty_check_change(tty);
1011 tty_wait_until_sent(tty, 0);
1012 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1015 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
1017 if(get_user(templ, (unsigned long *) arg))
1020 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1021 (arg ? CLOCAL : 0));
1024 return (mxser_get_serial_info(info, (struct serial_struct *) arg));
1026 return (mxser_set_serial_info(info, (struct serial_struct *) arg));
1027 case TIOCSERGETLSR: /* Get line status register */
1028 return (mxser_get_lsr_info(info, (unsigned int *) arg));
1030 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1031 * - mask passed in arg for lines of interest
1032 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1033 * Caller should use TIOCGICOUNT to see which one it was
1038 cprev = info->icount; /* note the counters on entry */
1039 restore_flags(flags);
1041 interruptible_sleep_on(&info->delta_msr_wait);
1042 /* see if a signal did it */
1043 if (signal_pending(current))
1044 return (-ERESTARTSYS);
1047 cnow = info->icount; /* atomic copy */
1048 restore_flags(flags);
1049 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1050 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1051 return (-EIO); /* no change => error */
1052 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1053 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1054 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1055 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1062 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1063 * Return: write counters to the user passed counter struct
1064 * NB: both 1->0 and 0->1 transitions are counted except for
1065 * RI where only 0->1 is counted.
1070 cnow = info->icount;
1071 restore_flags(flags);
1072 p_cuser = (struct serial_icounter_struct *) arg;
1073 if(put_user(cnow.cts, &p_cuser->cts))
1075 if(put_user(cnow.dsr, &p_cuser->dsr))
1077 if(put_user(cnow.rng, &p_cuser->rng))
1079 return put_user(cnow.dcd, &p_cuser->dcd);
1080 case MOXA_HighSpeedOn:
1081 return put_user(info->baud_base != 115200 ? 1 : 0, (int *) arg);
1083 return (-ENOIOCTLCMD);
1088 static int mxser_ioctl_special(unsigned int cmd, unsigned long arg)
1090 int i, result, status;
1094 if(copy_to_user((struct mxser_hwconf *) arg, mxsercfg,
1095 sizeof(struct mxser_hwconf) * 4))
1098 case MOXA_GET_MAJOR:
1099 if(copy_to_user((int *) arg, &ttymajor, sizeof(int)))
1103 case MOXA_GET_CUMAJOR:
1105 if(copy_to_user((int *) arg, &result, sizeof(int)))
1109 case MOXA_CHKPORTENABLE:
1111 for (i = 0; i < MXSER_PORTS; i++) {
1112 if (mxvar_table[i].base)
1115 return put_user(result, (unsigned long *) arg);
1116 case MOXA_GETDATACOUNT:
1117 if(copy_to_user((struct mxser_log *) arg, &mxvar_log, sizeof(mxvar_log)))
1120 case MOXA_GETMSTATUS:
1121 for (i = 0; i < MXSER_PORTS; i++) {
1123 if (!mxvar_table[i].base) {
1124 GMStatus[i].dcd = 0;
1125 GMStatus[i].dsr = 0;
1126 GMStatus[i].cts = 0;
1129 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1130 GMStatus[i].cflag = mxvar_table[i].cflag;
1132 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1134 status = inb(mxvar_table[i].base + UART_MSR);
1135 if (status & 0x80 /*UART_MSR_DCD */ )
1136 GMStatus[i].dcd = 1;
1138 GMStatus[i].dcd = 0;
1140 if (status & 0x20 /*UART_MSR_DSR */ )
1141 GMStatus[i].dsr = 1;
1143 GMStatus[i].dsr = 0;
1146 if (status & 0x10 /*UART_MSR_CTS */ )
1147 GMStatus[i].cts = 1;
1149 GMStatus[i].cts = 0;
1151 if(copy_to_user((struct mxser_mstatus *) arg, GMStatus,
1152 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1156 return (-ENOIOCTLCMD);
1162 * This routine is called by the upper-layer tty layer to signal that
1163 * incoming characters should be throttled.
1165 static void mxser_throttle(struct tty_struct *tty)
1167 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1168 unsigned long flags;
1171 info->x_char = STOP_CHAR(tty);
1175 info->IER |= UART_IER_THRI;
1176 outb(info->IER, info->base + UART_IER); /* force Tx interrupt */
1177 restore_flags(flags);
1179 if (info->tty->termios->c_cflag & CRTSCTS) {
1180 info->MCR &= ~UART_MCR_RTS;
1183 outb(info->MCR, info->base + UART_MCR);
1184 restore_flags(flags);
1188 static void mxser_unthrottle(struct tty_struct *tty)
1190 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1191 unsigned long flags;
1197 info->x_char = START_CHAR(tty);
1201 info->IER |= UART_IER_THRI; /* force Tx interrupt */
1202 outb(info->IER, info->base + UART_IER);
1203 restore_flags(flags);
1206 if (info->tty->termios->c_cflag & CRTSCTS) {
1207 info->MCR |= UART_MCR_RTS;
1210 outb(info->MCR, info->base + UART_MCR);
1211 restore_flags(flags);
1215 static void mxser_set_termios(struct tty_struct *tty,
1216 struct termios *old_termios)
1218 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1220 /* 8-2-99 by William
1221 if ( (tty->termios->c_cflag == old_termios->c_cflag) &&
1222 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
1223 RELEVANT_IFLAG(old_termios->c_iflag)) )
1226 mxser_change_speed(info, old_termios);
1228 if ( (old_termios->c_cflag & CRTSCTS) &&
1229 !(tty->termios->c_cflag & CRTSCTS) ) {
1230 tty->hw_stopped = 0;
1234 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1235 (RELEVANT_IFLAG(tty->termios->c_iflag) !=
1236 RELEVANT_IFLAG(old_termios->c_iflag))) {
1238 mxser_change_speed(info, old_termios);
1240 if ((old_termios->c_cflag & CRTSCTS) &&
1241 !(tty->termios->c_cflag & CRTSCTS)) {
1242 tty->hw_stopped = 0;
1246 /* Handle sw stopped */
1247 if ((old_termios->c_iflag & IXON) &&
1248 !(tty->termios->c_iflag & IXON)) {
1255 * mxser_stop() and mxser_start()
1257 * This routines are called before setting or resetting tty->stopped.
1258 * They enable or disable transmitter interrupts, as necessary.
1260 static void mxser_stop(struct tty_struct *tty)
1262 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1263 unsigned long flags;
1267 if (info->IER & UART_IER_THRI) {
1268 info->IER &= ~UART_IER_THRI;
1269 outb(info->IER, info->base + UART_IER);
1271 restore_flags(flags);
1274 static void mxser_start(struct tty_struct *tty)
1276 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1277 unsigned long flags;
1281 if (info->xmit_cnt && info->xmit_buf &&
1282 !(info->IER & UART_IER_THRI)) {
1283 info->IER |= UART_IER_THRI;
1284 outb(info->IER, info->base + UART_IER);
1286 restore_flags(flags);
1290 * This routine is called by tty_hangup() when a hangup is signaled.
1292 void mxser_hangup(struct tty_struct *tty)
1294 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1296 mxser_flush_buffer(tty);
1297 mxser_shutdown(info);
1300 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1302 wake_up_interruptible(&info->open_wait);
1306 * This is the serial driver's generic interrupt routine
1308 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1311 struct mxser_struct *info;
1312 struct mxser_struct *port;
1313 int max, irqbits, bits, msr;
1314 int pass_counter = 0;
1318 for (i = 0; i < MXSER_BOARDS; i++) {
1319 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1325 if (i == MXSER_BOARDS)
1329 max = mxser_numports[mxsercfg[i].board_type];
1332 irqbits = inb(port->vector) & port->vectormask;
1333 if (irqbits == port->vectormask)
1336 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1337 if (irqbits == port->vectormask)
1343 (inb(info->base + UART_IIR) & UART_IIR_NO_INT))
1345 status = inb(info->base + UART_LSR) & info->read_status_mask;
1346 if (status & UART_LSR_DR)
1347 mxser_receive_chars(info, &status);
1348 msr = inb(info->base + UART_MSR);
1349 if (msr & UART_MSR_ANY_DELTA)
1350 mxser_check_modem_status(info, msr);
1351 if (status & UART_LSR_THRE) {
1352 /* 8-2-99 by William
1353 if ( info->x_char || (info->xmit_cnt > 0) )
1355 mxser_transmit_chars(info);
1358 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
1360 printk("MOXA Smartio/Indusrtio family driver interrupt loop break\n");
1362 break; /* Prevent infinite loops */
1365 return IRQ_RETVAL(handled);
1368 static inline void mxser_receive_chars(struct mxser_struct *info,
1371 struct tty_struct *tty = info->tty;
1377 ch = inb(info->base + UART_RX);
1378 if (*status & info->ignore_status_mask) {
1379 if (++ignored > 100)
1382 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1385 if (*status & UART_LSR_SPECIAL) {
1386 if (*status & UART_LSR_BI) {
1387 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
1388 if (info->flags & ASYNC_SAK)
1390 } else if (*status & UART_LSR_PE) {
1391 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
1392 } else if (*status & UART_LSR_FE) {
1393 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
1394 } else if (*status & UART_LSR_OE) {
1395 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
1397 *tty->flip.flag_buf_ptr++ = 0;
1399 *tty->flip.flag_buf_ptr++ = 0;
1400 *tty->flip.char_buf_ptr++ = ch;
1403 *status = inb(info->base + UART_LSR) & info->read_status_mask;
1404 } while (*status & UART_LSR_DR);
1405 mxvar_log.rxcnt[info->port] += cnt;
1406 schedule_delayed_work(&tty->flip.work, 1);
1410 static inline void mxser_transmit_chars(struct mxser_struct *info)
1415 outb(info->x_char, info->base + UART_TX);
1417 mxvar_log.txcnt[info->port]++;
1420 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
1421 info->tty->hw_stopped) {
1422 info->IER &= ~UART_IER_THRI;
1423 outb(info->IER, info->base + UART_IER);
1426 cnt = info->xmit_cnt;
1427 count = info->xmit_fifo_size;
1429 outb(info->xmit_buf[info->xmit_tail++], info->base + UART_TX);
1430 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1431 if (--info->xmit_cnt <= 0)
1433 } while (--count > 0);
1434 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
1436 if (info->xmit_cnt < WAKEUP_CHARS) {
1437 set_bit(MXSER_EVENT_TXLOW, &info->event);
1438 schedule_work(&info->tqueue);
1440 if (info->xmit_cnt <= 0) {
1441 info->IER &= ~UART_IER_THRI;
1442 outb(info->IER, info->base + UART_IER);
1446 static inline void mxser_check_modem_status(struct mxser_struct *info,
1450 /* update input line counters */
1451 if (status & UART_MSR_TERI)
1453 if (status & UART_MSR_DDSR)
1455 if (status & UART_MSR_DDCD)
1457 if (status & UART_MSR_DCTS)
1459 wake_up_interruptible(&info->delta_msr_wait);
1461 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
1462 if (status & UART_MSR_DCD)
1463 wake_up_interruptible(&info->open_wait);
1465 set_bit(MXSER_EVENT_HANGUP, &info->event);
1466 schedule_work(&info->tqueue);
1468 if (info->flags & ASYNC_CTS_FLOW) {
1469 if (info->tty->hw_stopped) {
1470 if (status & UART_MSR_CTS) {
1471 info->tty->hw_stopped = 0;
1472 info->IER |= UART_IER_THRI;
1473 outb(info->IER, info->base + UART_IER);
1475 set_bit(MXSER_EVENT_TXLOW, &info->event);
1476 schedule_work(&info->tqueue);
1479 if (!(status & UART_MSR_CTS)) {
1480 info->tty->hw_stopped = 1;
1481 info->IER &= ~UART_IER_THRI;
1482 outb(info->IER, info->base + UART_IER);
1488 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
1489 struct mxser_struct *info)
1491 DECLARE_WAITQUEUE(wait, current);
1492 unsigned long flags;
1497 * If the device is in the middle of being closed, then block
1498 * until it's done, and then try again.
1500 if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
1501 if (info->flags & ASYNC_CLOSING)
1502 interruptible_sleep_on(&info->close_wait);
1503 #ifdef SERIAL_DO_RESTART
1504 if (info->flags & ASYNC_HUP_NOTIFY)
1507 return (-ERESTARTSYS);
1513 * If non-blocking mode is set, or the port is not enabled,
1514 * then make the check up front and then exit.
1516 if ((filp->f_flags & O_NONBLOCK) ||
1517 (tty->flags & (1 << TTY_IO_ERROR))) {
1518 info->flags |= ASYNC_NORMAL_ACTIVE;
1521 if (tty->termios->c_cflag & CLOCAL)
1525 * Block waiting for the carrier detect and the line to become
1526 * free (i.e., not in use by the callout). While we are in
1527 * this loop, info->count is dropped by one, so that
1528 * mxser_close() knows when to free things. We restore it upon
1529 * exit, either normal or abnormal.
1532 add_wait_queue(&info->open_wait, &wait);
1535 if (!tty_hung_up_p(filp))
1537 restore_flags(flags);
1538 info->blocked_open++;
1542 outb(inb(info->base + UART_MCR) | UART_MCR_DTR | UART_MCR_RTS,
1543 info->base + UART_MCR);
1544 restore_flags(flags);
1545 set_current_state(TASK_INTERRUPTIBLE);
1546 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
1547 #ifdef SERIAL_DO_RESTART
1548 if (info->flags & ASYNC_HUP_NOTIFY)
1551 retval = -ERESTARTSYS;
1557 if (!(info->flags & ASYNC_CLOSING) &&
1558 (do_clocal || (inb(info->base + UART_MSR) & UART_MSR_DCD)))
1560 if (signal_pending(current)) {
1561 retval = -ERESTARTSYS;
1566 set_current_state(TASK_RUNNING);
1567 remove_wait_queue(&info->open_wait, &wait);
1568 if (!tty_hung_up_p(filp))
1570 info->blocked_open--;
1573 info->flags |= ASYNC_NORMAL_ACTIVE;
1577 static int mxser_startup(struct mxser_struct *info)
1579 unsigned long flags;
1582 page = get_zeroed_page(GFP_KERNEL);
1589 if (info->flags & ASYNC_INITIALIZED) {
1591 restore_flags(flags);
1594 if (!info->base || !info->type) {
1596 set_bit(TTY_IO_ERROR, &info->tty->flags);
1598 restore_flags(flags);
1604 info->xmit_buf = (unsigned char *) page;
1607 * Clear the FIFO buffers and disable them
1608 * (they will be reenabled in mxser_change_speed())
1610 if (info->xmit_fifo_size == 16)
1611 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1612 info->base + UART_FCR);
1615 * At this point there's no way the LSR could still be 0xFF;
1616 * if it is, then bail out, because there's likely no UART
1619 if (inb(info->base + UART_LSR) == 0xff) {
1620 restore_flags(flags);
1621 if (capable(CAP_SYS_ADMIN)) {
1623 set_bit(TTY_IO_ERROR, &info->tty->flags);
1629 * Clear the interrupt registers.
1631 (void) inb(info->base + UART_LSR);
1632 (void) inb(info->base + UART_RX);
1633 (void) inb(info->base + UART_IIR);
1634 (void) inb(info->base + UART_MSR);
1637 * Now, initialize the UART
1639 outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
1640 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
1641 outb(info->MCR, info->base + UART_MCR);
1644 * Finally, enable interrupts
1646 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
1647 outb(info->IER, info->base + UART_IER); /* enable interrupts */
1650 * And clear the interrupt registers again for luck.
1652 (void) inb(info->base + UART_LSR);
1653 (void) inb(info->base + UART_RX);
1654 (void) inb(info->base + UART_IIR);
1655 (void) inb(info->base + UART_MSR);
1658 test_and_clear_bit(TTY_IO_ERROR, &info->tty->flags);
1660 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1663 * and set the speed of the serial port
1665 mxser_change_speed(info, 0);
1667 info->flags |= ASYNC_INITIALIZED;
1668 restore_flags(flags);
1673 * This routine will shutdown a serial port; interrupts maybe disabled, and
1674 * DTR is dropped if the hangup on close termio flag is on.
1676 static void mxser_shutdown(struct mxser_struct *info)
1678 unsigned long flags;
1680 if (!(info->flags & ASYNC_INITIALIZED))
1684 cli(); /* Disable interrupts */
1687 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1688 * here so the queue might never be waken up
1690 wake_up_interruptible(&info->delta_msr_wait);
1693 * Free the IRQ, if necessary
1695 if (info->xmit_buf) {
1696 free_page((unsigned long) info->xmit_buf);
1700 outb(0x00, info->base + UART_IER); /* disable all intrs */
1702 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1703 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1704 outb(info->MCR, info->base + UART_MCR);
1706 /* clear Rx/Tx FIFO's */
1707 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
1708 /* read data port to reset things */
1709 (void) inb(info->base + UART_RX);
1712 set_bit(TTY_IO_ERROR, &info->tty->flags);
1714 info->flags &= ~ASYNC_INITIALIZED;
1715 restore_flags(flags);
1719 * This routine is called to set the UART divisor registers to match
1720 * the specified baud rate for a serial port.
1722 static int mxser_change_speed(struct mxser_struct *info,
1723 struct termios *old_termios)
1726 unsigned cflag, cval, fcr;
1729 unsigned long flags;
1731 if (!info->tty || !info->tty->termios)
1733 cflag = info->tty->termios->c_cflag;
1738 #define B921600 (B460800 +1)
1740 switch (cflag & (CBAUD | CBAUDEX)) {
1807 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1808 i = 16; /* 57600 bps */
1809 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1810 i = 17; /* 115200 bps */
1812 #ifdef ASYNC_SPD_SHI
1813 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1817 #ifdef ASYNC_SPD_WARP
1818 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1822 if (mxvar_baud_table[i] == 134) {
1823 quot = (2 * info->baud_base / 269);
1824 } else if (mxvar_baud_table[i]) {
1825 quot = info->baud_base / mxvar_baud_table[i];
1826 if (!quot && old_termios) {
1828 info->tty->termios->c_cflag &= ~CBAUD;
1829 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1830 switch (info->tty->termios->c_cflag & (CBAUD | CBAUDEX)) {
1896 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1897 i = 16; /* 57600 bps */
1898 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1899 i = 17; /* 115200 bps */
1900 #ifdef ASYNC_SPD_SHI
1901 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1904 #ifdef ASYNC_SPD_WARP
1905 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1909 if (mxvar_baud_table[i] == 134) {
1910 quot = (2 * info->baud_base / 269);
1911 } else if (mxvar_baud_table[i]) {
1912 quot = info->baud_base / mxvar_baud_table[i];
1918 } else if (quot == 0)
1925 info->MCR |= UART_MCR_DTR;
1928 outb(info->MCR, info->base + UART_MCR);
1929 restore_flags(flags);
1931 info->MCR &= ~UART_MCR_DTR;
1934 outb(info->MCR, info->base + UART_MCR);
1935 restore_flags(flags);
1938 /* byte size and parity */
1939 switch (cflag & CSIZE) {
1954 break; /* too keep GCC shut... */
1959 cval |= UART_LCR_PARITY;
1960 if (!(cflag & PARODD))
1961 cval |= UART_LCR_EPAR;
1962 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
1965 fcr = UART_FCR_ENABLE_FIFO;
1966 switch (info->rx_trigger) {
1968 fcr |= UART_FCR_TRIGGER_1;
1971 fcr |= UART_FCR_TRIGGER_4;
1974 fcr |= UART_FCR_TRIGGER_8;
1977 fcr |= UART_FCR_TRIGGER_14;
1981 /* CTS flow control flag and modem status interrupts */
1982 info->IER &= ~UART_IER_MSI;
1983 info->MCR &= ~UART_MCR_AFE;
1984 if (cflag & CRTSCTS) {
1985 info->flags |= ASYNC_CTS_FLOW;
1986 info->IER |= UART_IER_MSI;
1987 if (info->type == PORT_16550A)
1988 info->MCR |= UART_MCR_AFE;
1990 info->flags &= ~ASYNC_CTS_FLOW;
1992 outb(info->MCR, info->base + UART_MCR);
1994 info->flags &= ~ASYNC_CHECK_CD;
1996 info->flags |= ASYNC_CHECK_CD;
1997 info->IER |= UART_IER_MSI;
1999 outb(info->IER, info->base + UART_IER);
2002 * Set up parity check flag
2004 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2005 if (I_INPCK(info->tty))
2006 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2007 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2008 info->read_status_mask |= UART_LSR_BI;
2010 info->ignore_status_mask = 0;
2012 /* This should be safe, but for some broken bits of hardware... */
2013 if (I_IGNPAR(info->tty)) {
2014 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2015 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
2018 if (I_IGNBRK(info->tty)) {
2019 info->ignore_status_mask |= UART_LSR_BI;
2020 info->read_status_mask |= UART_LSR_BI;
2022 * If we're ignore parity and break indicators, ignore
2023 * overruns too. (For real raw support).
2025 if (I_IGNPAR(info->tty)) {
2026 info->ignore_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2027 info->read_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2032 outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2033 outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2034 outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2035 outb(cval, info->base + UART_LCR); /* reset DLAB */
2036 outb(fcr, info->base + UART_FCR); /* set fcr */
2037 restore_flags(flags);
2043 * ------------------------------------------------------------
2044 * friends of mxser_ioctl()
2045 * ------------------------------------------------------------
2047 static int mxser_get_serial_info(struct mxser_struct *info,
2048 struct serial_struct *retinfo)
2050 struct serial_struct tmp;
2054 memset(&tmp, 0, sizeof(tmp));
2055 tmp.type = info->type;
2056 tmp.line = info->port;
2057 tmp.port = info->base;
2058 tmp.irq = info->irq;
2059 tmp.flags = info->flags;
2060 tmp.baud_base = info->baud_base;
2061 tmp.close_delay = info->close_delay;
2062 tmp.closing_wait = info->closing_wait;
2063 tmp.custom_divisor = info->custom_divisor;
2065 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2068 static int mxser_set_serial_info(struct mxser_struct *info,
2069 struct serial_struct *new_info)
2071 struct serial_struct new_serial;
2075 if (!new_info || !info->base)
2077 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2080 if ((new_serial.irq != info->irq) ||
2081 (new_serial.port != info->base) ||
2082 (new_serial.type != info->type) ||
2083 (new_serial.custom_divisor != info->custom_divisor) ||
2084 (new_serial.baud_base != info->baud_base))
2087 flags = info->flags & ASYNC_SPD_MASK;
2089 if (!capable(CAP_SYS_ADMIN)) {
2090 if ((new_serial.baud_base != info->baud_base) ||
2091 (new_serial.close_delay != info->close_delay) ||
2092 ((new_serial.flags & ~ASYNC_USR_MASK) !=
2093 (info->flags & ~ASYNC_USR_MASK)))
2095 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2096 (new_serial.flags & ASYNC_USR_MASK));
2099 * OK, past this point, all the error checking has been done.
2100 * At this point, we start making changes.....
2102 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2103 (new_serial.flags & ASYNC_FLAGS));
2104 info->close_delay = new_serial.close_delay * HZ / 100;
2105 info->closing_wait = new_serial.closing_wait * HZ / 100;
2108 if (info->flags & ASYNC_INITIALIZED) {
2109 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2110 mxser_change_speed(info, 0);
2113 retval = mxser_startup(info);
2118 * mxser_get_lsr_info - get line status register info
2120 * Purpose: Let user call ioctl() to get info when the UART physically
2121 * is emptied. On bus types like RS485, the transmitter must
2122 * release the bus after transmitting. This must be done when
2123 * the transmit shift register is empty, not be done when the
2124 * transmit holding register is empty. This functionality
2125 * allows an RS485 driver to be written in user space.
2127 static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int *value)
2129 unsigned char status;
2130 unsigned int result;
2131 unsigned long flags;
2135 status = inb(info->base + UART_LSR);
2136 restore_flags(flags);
2137 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2138 return put_user(result, value);
2142 * This routine sends a break character out the serial port.
2144 static void mxser_send_break(struct mxser_struct *info, int duration)
2146 unsigned long flags;
2149 set_current_state(TASK_INTERRUPTIBLE);
2152 outb(inb(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
2153 schedule_timeout(duration);
2154 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
2155 restore_flags(flags);
2158 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2160 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
2161 unsigned char control, status;
2162 unsigned long flags;
2164 if (PORTNO(tty) == MXSER_PORTS)
2165 return (-ENOIOCTLCMD);
2166 if (tty->flags & (1 << TTY_IO_ERROR))
2169 control = info->MCR;
2172 status = inb(info->base + UART_MSR);
2173 if (status & UART_MSR_ANY_DELTA)
2174 mxser_check_modem_status(info, status);
2175 restore_flags(flags);
2176 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2177 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2178 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2179 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2180 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2181 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2184 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
2185 unsigned int set, unsigned int clear)
2187 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
2188 unsigned long flags;
2190 if (PORTNO(tty) == MXSER_PORTS)
2191 return (-ENOIOCTLCMD);
2192 if (tty->flags & (1 << TTY_IO_ERROR))
2197 if (set & TIOCM_RTS)
2198 info->MCR |= UART_MCR_RTS;
2199 if (set & TIOCM_DTR)
2200 info->MCR |= UART_MCR_DTR;
2202 if (clear & TIOCM_RTS)
2203 info->MCR &= ~UART_MCR_RTS;
2204 if (clear & TIOCM_DTR)
2205 info->MCR &= ~UART_MCR_DTR;
2207 outb(info->MCR, info->base + UART_MCR);
2208 restore_flags(flags);
2212 static int mxser_read_register(int, unsigned short *);
2213 static int mxser_program_mode(int);
2214 static void mxser_normal_mode(int);
2216 static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2219 unsigned short regs[16], irq;
2220 unsigned char scratch, scratch2;
2222 id = mxser_read_register(cap, regs);
2223 if (id == C168_ASIC_ID)
2224 hwconf->board_type = MXSER_BOARD_C168_ISA;
2225 else if (id == C104_ASIC_ID)
2226 hwconf->board_type = MXSER_BOARD_C104_ISA;
2227 else if (id == C102_ASIC_ID)
2228 hwconf->board_type = MXSER_BOARD_C102_ISA;
2229 else if (id == CI132_ASIC_ID)
2230 hwconf->board_type = MXSER_BOARD_CI132;
2231 else if (id == CI134_ASIC_ID)
2232 hwconf->board_type = MXSER_BOARD_CI134;
2233 else if (id == CI104J_ASIC_ID)
2234 hwconf->board_type = MXSER_BOARD_CI104J;
2237 irq = regs[9] & 0x0F;
2238 irq = irq | (irq << 4);
2239 irq = irq | (irq << 8);
2240 if ((irq != regs[9]) || ((id == 1) && (irq != regs[10]))) {
2241 return (MXSER_ERR_IRQ_CONFLIT);
2244 return (MXSER_ERR_IRQ);
2246 for (i = 0; i < 8; i++)
2247 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
2248 hwconf->irq = (int) (irq & 0x0F);
2249 if ((regs[12] & 0x80) == 0) {
2250 return (MXSER_ERR_VECTOR);
2252 hwconf->vector = (int) regs[11]; /* interrupt vector */
2254 hwconf->vector_mask = 0x00FF;
2256 hwconf->vector_mask = 0x000F;
2257 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2258 if (regs[12] & bits)
2259 hwconf->baud_base[i] = 921600;
2261 hwconf->baud_base[i] = 115200;
2263 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2264 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2265 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2266 outb(scratch2, cap + UART_LCR);
2267 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2268 scratch = inb(cap + UART_IIR);
2270 hwconf->uart_type = PORT_16550A;
2272 hwconf->uart_type = PORT_16450;
2277 return (hwconf->ports);
2280 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
2281 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
2282 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
2283 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
2284 #define EN_CCMD 0x000 /* Chip's command register */
2285 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
2286 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
2287 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
2288 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
2289 #define EN0_DCFG 0x00E /* Data configuration reg WR */
2290 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
2291 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
2292 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
2293 static int mxser_read_register(int port, unsigned short *regs)
2295 int i, k, value, id;
2298 id = mxser_program_mode(port);
2301 for (i = 0; i < 14; i++) {
2302 k = (i & 0x3F) | 0x180;
2303 for (j = 0x100; j > 0; j >>= 1) {
2304 outb(CHIP_CS, port);
2306 outb(CHIP_CS | CHIP_DO, port);
2307 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
2309 outb(CHIP_CS, port);
2310 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
2315 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
2316 outb(CHIP_CS, port);
2317 outb(CHIP_CS | CHIP_SK, port);
2318 if (inb(port) & CHIP_DI)
2324 mxser_normal_mode(port);
2328 static int mxser_program_mode(int port)
2331 unsigned long flags;
2342 restore_flags(flags);
2343 id = inb(port + 1) & 0x1F;
2344 if ((id != C168_ASIC_ID) && (id != C104_ASIC_ID) && (id != CI104J_ASIC_ID) &&
2345 (id != C102_ASIC_ID) && (id != CI132_ASIC_ID) && (id != CI134_ASIC_ID))
2347 for (i = 0, j = 0; i < 4; i++) {
2351 } else if ((j == 1) && (n == 1)) {
2362 static void mxser_normal_mode(int port)
2366 outb(0xA5, port + 1);
2367 outb(0x80, port + 3);
2368 outb(12, port + 0); /* 9600 bps */
2370 outb(0x03, port + 3); /* 8 data bits */
2371 outb(0x13, port + 4); /* loop back mode */
2372 for (i = 0; i < 16; i++) {
2374 if ((n & 0x61) == 0x60)
2379 outb(0x00, port + 4);
2382 module_init(mxser_module_init);
2383 module_exit(mxser_module_exit);