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 the Moxa PCI vendor and device IDs.
108 #ifndef PCI_VENDOR_ID_MOXA
109 #define PCI_VENDOR_ID_MOXA 0x1393
111 #ifndef PCI_DEVICE_ID_C168
112 #define PCI_DEVICE_ID_C168 0x1680
114 #ifndef PCI_DEVICE_ID_C104
115 #define PCI_DEVICE_ID_C104 0x1040
117 #ifndef PCI_DEVICE_ID_CP104
118 #define PCI_DEVICE_ID_CP104 0x1041
120 #ifndef PCI_DEVICE_ID_CP132
121 #define PCI_DEVICE_ID_CP132 0x1320
123 #ifndef PCI_DEVICE_ID_CP114
124 #define PCI_DEVICE_ID_CP114 0x1141
126 #ifndef PCI_DEVICE_ID_CT114
127 #define PCI_DEVICE_ID_CT114 0x1140
130 #define C168_ASIC_ID 1
131 #define C104_ASIC_ID 2
132 #define CI134_ASIC_ID 3
133 #define CI132_ASIC_ID 4
134 #define CI104J_ASIC_ID 5
135 #define C102_ASIC_ID 0xB
138 MXSER_BOARD_C168_ISA = 0,
139 MXSER_BOARD_C104_ISA,
141 MXSER_BOARD_C168_PCI,
142 MXSER_BOARD_C104_PCI,
143 MXSER_BOARD_CP104_PCI,
144 MXSER_BOARD_C102_ISA,
147 MXSER_BOARD_CP132_PCI,
148 MXSER_BOARD_CP114_PCI,
149 MXSER_BOARD_CT114_PCI
152 static char *mxser_brdname[] =
168 static int mxser_numports[] =
188 #define MOXA_GETDATACOUNT (MOXA + 23)
189 #define MOXA_GET_CONF (MOXA + 35)
190 #define MOXA_DIAGNOSE (MOXA + 50)
191 #define MOXA_CHKPORTENABLE (MOXA + 60)
192 #define MOXA_HighSpeedOn (MOXA + 61)
193 #define MOXA_GET_MAJOR (MOXA + 63)
194 #define MOXA_GET_CUMAJOR (MOXA + 64)
195 #define MOXA_GETMSTATUS (MOXA + 65)
198 static struct pci_device_id mxser_pcibrds[] = {
199 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C168, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
200 MXSER_BOARD_C168_PCI },
201 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C104, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
202 MXSER_BOARD_C104_PCI },
203 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP104, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
204 MXSER_BOARD_CP104_PCI },
205 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP132, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
206 MXSER_BOARD_CP132_PCI },
207 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP114, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
208 MXSER_BOARD_CP114_PCI },
209 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CT114, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
210 MXSER_BOARD_CT114_PCI },
213 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
214 #endif /* CONFIG_PCI */
216 static int ioaddr[MXSER_BOARDS];
217 static int ttymajor = MXSERMAJOR;
220 /* Variables for insmod */
222 MODULE_AUTHOR("William Chen");
223 MODULE_DESCRIPTION("MOXA Smartio Family Multiport Board Device Driver");
224 MODULE_LICENSE("GPL");
225 MODULE_PARM(ioaddr, "1-4i");
226 MODULE_PARM(ttymajor, "i");
227 MODULE_PARM(verbose, "i");
229 struct mxser_hwconf {
236 int ioaddr[MXSER_PORTS_PER_BOARD];
237 int baud_base[MXSER_PORTS_PER_BOARD];
238 struct pci_dev *pdev;
241 struct mxser_struct {
243 int base; /* port base address */
244 int irq; /* port using irq no. */
245 int vector; /* port irq vector */
246 int vectormask; /* port vector mask */
247 int rx_trigger; /* Rx fifo trigger level */
248 int baud_base; /* max. speed */
249 int flags; /* defined in tty.h */
250 int type; /* UART type */
251 struct tty_struct *tty;
252 int read_status_mask;
253 int ignore_status_mask;
256 int x_char; /* xon/xoff character */
258 unsigned short closing_wait;
259 int IER; /* Interrupt Enable Register */
260 int MCR; /* Modem control register */
262 int count; /* # of fd on device */
263 int blocked_open; /* # of blocked opens */
264 unsigned char *xmit_buf;
268 struct work_struct tqueue;
270 wait_queue_head_t open_wait;
271 wait_queue_head_t close_wait;
272 wait_queue_head_t delta_msr_wait;
273 struct async_icount icount; /* kernel counters for the 4 input interrupts */
278 int rxcnt[MXSER_PORTS];
279 int txcnt[MXSER_PORTS];
282 struct mxser_mstatus {
290 static struct mxser_mstatus GMStatus[MXSER_PORTS];
292 static int mxserBoardCAP[MXSER_BOARDS] =
295 /* 0x180, 0x280, 0x200, 0x320 */
299 static struct tty_driver *mxvar_sdriver;
300 static struct mxser_struct mxvar_table[MXSER_PORTS];
301 static struct mxser_log mxvar_log;
302 static int mxvar_diagflag;
304 * mxvar_tmp_buf is used as a temporary buffer by serial_write. We need
305 * to lock it in case the memcpy_fromfs blocks while swapping in a page,
306 * and some other program tries to do a serial write at the same time.
307 * Since the lock will only come under contention when the system is
308 * swapping and available memory is low, it makes sense to share one
309 * buffer across all the serial ports, since it significantly saves
310 * memory if large numbers of serial ports are open.
312 static unsigned char *mxvar_tmp_buf;
313 static struct semaphore mxvar_tmp_buf_sem;
316 * This is used to figure out the divisor speeds and the timeouts
318 static int mxvar_baud_table[] =
320 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
321 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 0};
323 struct mxser_hwconf mxsercfg[MXSER_BOARDS];
329 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
330 static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
331 static void mxser_do_softint(void *);
332 static int mxser_open(struct tty_struct *, struct file *);
333 static void mxser_close(struct tty_struct *, struct file *);
334 static int mxser_write(struct tty_struct *, int, const unsigned char *, int);
335 static int mxser_write_room(struct tty_struct *);
336 static void mxser_flush_buffer(struct tty_struct *);
337 static int mxser_chars_in_buffer(struct tty_struct *);
338 static void mxser_flush_chars(struct tty_struct *);
339 static void mxser_put_char(struct tty_struct *, unsigned char);
340 static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
341 static int mxser_ioctl_special(unsigned int, unsigned long);
342 static void mxser_throttle(struct tty_struct *);
343 static void mxser_unthrottle(struct tty_struct *);
344 static void mxser_set_termios(struct tty_struct *, struct termios *);
345 static void mxser_stop(struct tty_struct *);
346 static void mxser_start(struct tty_struct *);
347 static void mxser_hangup(struct tty_struct *);
348 static inline void mxser_receive_chars(struct mxser_struct *, int *);
349 static inline void mxser_transmit_chars(struct mxser_struct *);
350 static inline void mxser_check_modem_status(struct mxser_struct *, int);
351 static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
352 static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
353 static int mxser_startup(struct mxser_struct *);
354 static void mxser_shutdown(struct mxser_struct *);
355 static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
356 static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct __user *);
357 static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct __user *);
358 static int mxser_get_lsr_info(struct mxser_struct *, unsigned int __user *);
359 static void mxser_send_break(struct mxser_struct *, int);
360 static int mxser_tiocmget(struct tty_struct *, struct file *);
361 static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
364 * The MOXA C168/C104 serial driver boot-time initialization code!
367 static void __exit mxser_module_exit(void)
373 printk("Unloading module mxser ...\n");
374 if ((err |= tty_unregister_driver(mxvar_sdriver)))
375 printk("Couldn't unregister MOXA Smartio family serial driver\n");
376 put_tty_driver(mxvar_sdriver);
378 for (i = 0; i < MXSER_BOARDS; i++) {
379 if (mxsercfg[i].board_type == -1)
382 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
391 int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
393 struct mxser_struct *info;
398 init_MUTEX(&mxvar_tmp_buf_sem);
400 n = board * MXSER_PORTS_PER_BOARD;
401 info = &mxvar_table[n];
402 for (i = 0; i < hwconf->ports; i++, n++, info++) {
404 printk(" ttyM%d/cum%d at 0x%04x ", n, n, hwconf->ioaddr[i]);
405 if (hwconf->baud_base[i] == 115200)
406 printk(" max. baud rate up to 115200 bps.\n");
408 printk(" max. baud rate up to 921600 bps.\n");
411 info->base = hwconf->ioaddr[i];
412 info->irq = hwconf->irq;
413 info->vector = hwconf->vector;
414 info->vectormask = hwconf->vector_mask;
415 info->rx_trigger = 14;
416 info->baud_base = hwconf->baud_base[i];
417 info->flags = ASYNC_SHARE_IRQ;
418 info->type = hwconf->uart_type;
419 if ((info->type == PORT_16450) || (info->type == PORT_8250))
420 info->xmit_fifo_size = 1;
422 info->xmit_fifo_size = 16;
423 info->custom_divisor = hwconf->baud_base[i] * 16;
424 info->close_delay = 5 * HZ / 10;
425 info->closing_wait = 30 * HZ;
426 INIT_WORK(&info->tqueue, mxser_do_softint, info);
427 info->cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
428 init_waitqueue_head(&info->open_wait);
429 init_waitqueue_head(&info->close_wait);
430 init_waitqueue_head(&info->delta_msr_wait);
434 * Allocate the IRQ if necessary
438 n = board * MXSER_PORTS_PER_BOARD;
439 info = &mxvar_table[n];
442 retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
445 restore_flags(flags);
446 printk("Board %d: %s", board, mxser_brdname[hwconf->board_type]);
447 printk(" Request irq fail,IRQ (%d) may be conflit with another device.\n", info->irq);
450 restore_flags(flags);
456 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
458 mxsercfg[board] = *hwconf;
462 static int mxser_get_PCI_conf(struct pci_dev *pdev, int board_type, struct mxser_hwconf *hwconf)
465 unsigned int ioaddress;
467 hwconf->board_type = board_type;
468 hwconf->ports = mxser_numports[board_type];
469 ioaddress = pci_resource_start (pdev, 2);
470 for (i = 0; i < hwconf->ports; i++)
471 hwconf->ioaddr[i] = ioaddress + 8 * i;
473 ioaddress = pci_resource_start (pdev, 3);
474 hwconf->vector = ioaddress;
476 hwconf->irq = pdev->irq;
478 hwconf->uart_type = PORT_16550A;
479 hwconf->vector_mask = 0;
480 for (i = 0; i < hwconf->ports; i++) {
481 hwconf->vector_mask |= (1 << i);
482 hwconf->baud_base[i] = 921600;
486 #endif /* CONFIG_PCI */
488 static struct tty_operations mxser_ops = {
490 .close = mxser_close,
491 .write = mxser_write,
492 .put_char = mxser_put_char,
493 .flush_chars = mxser_flush_chars,
494 .write_room = mxser_write_room,
495 .chars_in_buffer = mxser_chars_in_buffer,
496 .flush_buffer = mxser_flush_buffer,
497 .ioctl = mxser_ioctl,
498 .throttle = mxser_throttle,
499 .unthrottle = mxser_unthrottle,
500 .set_termios = mxser_set_termios,
502 .start = mxser_start,
503 .hangup = mxser_hangup,
504 .tiocmget = mxser_tiocmget,
505 .tiocmset = mxser_tiocmset,
508 static int __init mxser_module_init(void)
511 struct mxser_hwconf hwconf;
513 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
517 printk("MOXA Smartio family driver version %s\n", MXSER_VERSION);
519 /* Initialize the tty_driver structure */
521 mxvar_sdriver->owner = THIS_MODULE;
522 mxvar_sdriver->name = "ttyM";
523 mxvar_sdriver->devfs_name = "tts/M";
524 mxvar_sdriver->major = ttymajor;
525 mxvar_sdriver->minor_start = 0;
526 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
527 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
528 mxvar_sdriver->init_termios = tty_std_termios;
529 mxvar_sdriver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
530 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
531 tty_set_operations(mxvar_sdriver, &mxser_ops);
532 printk("Tty devices major number = %d\n", ttymajor);
535 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
536 memset(&mxvar_log, 0, sizeof(struct mxser_log));
540 /* Start finding ISA boards here */
541 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
543 if (!(cap = mxserBoardCAP[b]))
546 retval = mxser_get_ISA_conf(cap, &hwconf);
549 printk("Found MOXA %s board (CAP=0x%x)\n",
550 mxser_brdname[hwconf.board_type],
554 if (retval == MXSER_ERR_IRQ)
555 printk("Invalid interrupt number,board not configured\n");
556 else if (retval == MXSER_ERR_IRQ_CONFLIT)
557 printk("Invalid interrupt number,board not configured\n");
558 else if (retval == MXSER_ERR_VECTOR)
559 printk("Invalid interrupt vector,board not configured\n");
560 else if (retval == MXSER_ERR_IOADDR)
561 printk("Invalid I/O address,board not configured\n");
567 if (mxser_initbrd(m, &hwconf) < 0)
570 mxser_getcfg(m, &hwconf);
575 /* Start finding ISA boards from module arg */
576 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
578 if (!(cap = ioaddr[b]))
581 retval = mxser_get_ISA_conf(cap, &hwconf);
584 printk("Found MOXA %s board (CAP=0x%x)\n",
585 mxser_brdname[hwconf.board_type],
589 if (retval == MXSER_ERR_IRQ)
590 printk("Invalid interrupt number,board not configured\n");
591 else if (retval == MXSER_ERR_IRQ_CONFLIT)
592 printk("Invalid interrupt number,board not configured\n");
593 else if (retval == MXSER_ERR_VECTOR)
594 printk("Invalid interrupt vector,board not configured\n");
595 else if (retval == MXSER_ERR_IOADDR)
596 printk("Invalid I/O address,board not configured\n");
602 if (mxser_initbrd(m, &hwconf) < 0)
605 mxser_getcfg(m, &hwconf);
610 /* start finding PCI board here */
614 struct pci_dev *pdev = NULL;
615 int n = (sizeof(mxser_pcibrds) / sizeof(mxser_pcibrds[0])) - 1;
616 for (b = 0; b < n; b++) {
617 while ((pdev = pci_find_device(mxser_pcibrds[b].vendor, mxser_pcibrds[b].device, pdev)))
619 if (pci_enable_device(pdev))
622 printk("Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
623 mxser_brdname[mxser_pcibrds[b].driver_data],
624 pdev->bus->number, PCI_SLOT(pdev->devfn));
625 if (m >= MXSER_BOARDS) {
626 printk("Too many Smartio family boards found (maximum %d),board not configured\n", MXSER_BOARDS);
628 retval = mxser_get_PCI_conf(pdev, mxser_pcibrds[b].driver_data, &hwconf);
630 if (retval == MXSER_ERR_IRQ)
631 printk("Invalid interrupt number,board not configured\n");
632 else if (retval == MXSER_ERR_IRQ_CONFLIT)
633 printk("Invalid interrupt number,board not configured\n");
634 else if (retval == MXSER_ERR_VECTOR)
635 printk("Invalid interrupt vector,board not configured\n");
636 else if (retval == MXSER_ERR_IOADDR)
637 printk("Invalid I/O address,board not configured\n");
640 if (mxser_initbrd(m, &hwconf) < 0)
642 mxser_getcfg(m, &hwconf);
650 for (i = m; i < MXSER_BOARDS; i++) {
651 mxsercfg[i].board_type = -1;
655 if (!tty_register_driver(mxvar_sdriver))
658 put_tty_driver(mxvar_sdriver);
659 printk("Couldn't install MOXA Smartio family driver !\n");
661 for (i = 0; i < MXSER_BOARDS; i++) {
662 if (mxsercfg[i].board_type == -1)
664 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
669 static void mxser_do_softint(void *private_)
671 struct mxser_struct *info = (struct mxser_struct *) private_;
672 struct tty_struct *tty;
676 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event)) {
678 wake_up_interruptible(&tty->write_wait);
680 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event)) {
681 tty_hangup(tty); /* FIXME: module removal race here - AKPM */
687 * This routine is called whenever a serial port is opened. It
688 * enables interrupts for a serial port, linking in its async structure into
689 * the IRQ chain. It also performs the serial-specific
690 * initialization for the tty structure.
693 static int mxser_open(struct tty_struct *tty, struct file *filp)
695 struct mxser_struct *info;
700 if (line == MXSER_PORTS)
702 if ((line < 0) || (line > MXSER_PORTS))
704 info = mxvar_table + line;
709 tty->driver_data = info;
712 if (!mxvar_tmp_buf) {
713 page = get_zeroed_page(GFP_KERNEL);
719 mxvar_tmp_buf = (unsigned char *) page;
722 * Start up serial port
724 retval = mxser_startup(info);
728 return mxser_block_til_ready(tty, filp, info);
732 * This routine is called when the serial port gets closed. First, we
733 * wait for the last remaining data to be sent. Then, we unlink its
734 * async structure from the interrupt chain if necessary, and we free
735 * that IRQ if nothing is left in the chain.
738 static void mxser_close(struct tty_struct *tty, struct file *filp)
740 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
742 unsigned long timeout;
744 if (PORTNO(tty) == MXSER_PORTS)
752 if (tty_hung_up_p(filp)) {
753 restore_flags(flags);
756 if ((tty->count == 1) && (info->count != 1)) {
758 * Uh, oh. tty->count is 1, which means that the tty
759 * structure will be freed. Info->count should always
760 * be one in these conditions. If it's greater than
761 * one, we've got real problems, since it means the
762 * serial port won't be shutdown.
764 printk("mxser_close: bad serial port count; tty->count is 1, "
765 "info->count is %d\n", info->count);
768 if (--info->count < 0) {
769 printk("mxser_close: bad serial port count for ttys%d: %d\n",
770 info->port, info->count);
774 restore_flags(flags);
777 info->flags |= ASYNC_CLOSING;
778 info->cflag = tty->termios->c_cflag;
780 * Now we wait for the transmit buffer to clear; and we notify
781 * the line discipline to only process XON/XOFF characters.
784 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
785 tty_wait_until_sent(tty, info->closing_wait);
787 * At this point we stop accepting input. To do this, we
788 * disable the receive line status interrupts, and tell the
789 * interrupt driver to stop checking the data ready bit in the
790 * line status register.
792 info->IER &= ~UART_IER_RLSI;
794 info->read_status_mask &= ~UART_LSR_DR;
796 if (info->flags & ASYNC_INITIALIZED) {
797 outb(info->IER, info->base + UART_IER);
799 * Before we drop DTR, make sure the UART transmitter
800 * has completely drained; this is especially
801 * important if there is a transmit FIFO!
803 timeout = jiffies + HZ;
804 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
805 set_current_state(TASK_INTERRUPTIBLE);
807 if (time_after(jiffies, timeout))
811 mxser_shutdown(info);
812 if (tty->driver->flush_buffer)
813 tty->driver->flush_buffer(tty);
814 tty_ldisc_flush(tty);
819 if (info->blocked_open) {
820 if (info->close_delay) {
821 set_current_state(TASK_INTERRUPTIBLE);
822 schedule_timeout(info->close_delay);
824 wake_up_interruptible(&info->open_wait);
826 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
827 wake_up_interruptible(&info->close_wait);
828 restore_flags(flags);
832 static int mxser_write(struct tty_struct *tty, int from_user,
833 const unsigned char *buf, int count)
836 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
839 if (!tty || !info->xmit_buf || !mxvar_tmp_buf)
844 down(&mxvar_tmp_buf_sem);
846 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
847 SERIAL_XMIT_SIZE - info->xmit_head));
851 c -= copy_from_user(mxvar_tmp_buf, buf, c);
859 c = min_t(int, c, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
860 SERIAL_XMIT_SIZE - info->xmit_head));
861 memcpy(info->xmit_buf + info->xmit_head, mxvar_tmp_buf, c);
862 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
864 restore_flags(flags);
870 up(&mxvar_tmp_buf_sem);
874 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
875 SERIAL_XMIT_SIZE - info->xmit_head));
877 restore_flags(flags);
881 memcpy(info->xmit_buf + info->xmit_head, buf, c);
882 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
884 restore_flags(flags);
893 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
894 !(info->IER & UART_IER_THRI)) {
895 info->IER |= UART_IER_THRI;
896 outb(info->IER, info->base + UART_IER);
898 restore_flags(flags);
902 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
904 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
907 if (!tty || !info->xmit_buf)
912 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
913 restore_flags(flags);
916 info->xmit_buf[info->xmit_head++] = ch;
917 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
919 /********************************************** why ??? ***********
920 if ( !tty->stopped && !tty->hw_stopped &&
921 !(info->IER & UART_IER_THRI) ) {
922 info->IER |= UART_IER_THRI;
923 outb(info->IER, info->base + UART_IER);
925 *****************************************************************/
926 restore_flags(flags);
929 static void mxser_flush_chars(struct tty_struct *tty)
931 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
934 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
940 info->IER |= UART_IER_THRI;
941 outb(info->IER, info->base + UART_IER);
942 restore_flags(flags);
945 static int mxser_write_room(struct tty_struct *tty)
947 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
950 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
956 static int mxser_chars_in_buffer(struct tty_struct *tty)
958 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
960 return (info->xmit_cnt);
963 static void mxser_flush_buffer(struct tty_struct *tty)
965 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
970 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
971 restore_flags(flags);
972 wake_up_interruptible(&tty->write_wait);
976 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
977 unsigned int cmd, unsigned long arg)
980 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
982 struct async_icount cprev, cnow; /* kernel counter temps */
983 struct serial_icounter_struct __user *p_cuser;
985 void __user *argp = (void __user *)arg;
987 if (PORTNO(tty) == MXSER_PORTS)
988 return (mxser_ioctl_special(cmd, arg));
989 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) &&
990 (cmd != TIOCGICOUNT)) {
991 if (tty->flags & (1 << TTY_IO_ERROR))
995 case TCSBRK: /* SVID version: non-zero arg --> no break */
996 retval = tty_check_change(tty);
999 tty_wait_until_sent(tty, 0);
1001 mxser_send_break(info, HZ / 4); /* 1/4 second */
1003 case TCSBRKP: /* support for POSIX tcsendbreak() */
1004 retval = tty_check_change(tty);
1007 tty_wait_until_sent(tty, 0);
1008 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1011 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1013 if(get_user(templ, (unsigned long __user *) arg))
1016 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1017 (arg ? CLOCAL : 0));
1020 return mxser_get_serial_info(info, argp);
1022 return mxser_set_serial_info(info, argp);
1023 case TIOCSERGETLSR: /* Get line status register */
1024 return mxser_get_lsr_info(info, argp);
1026 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1027 * - mask passed in arg for lines of interest
1028 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1029 * Caller should use TIOCGICOUNT to see which one it was
1034 cprev = info->icount; /* note the counters on entry */
1035 restore_flags(flags);
1037 interruptible_sleep_on(&info->delta_msr_wait);
1038 /* see if a signal did it */
1039 if (signal_pending(current))
1040 return (-ERESTARTSYS);
1043 cnow = info->icount; /* atomic copy */
1044 restore_flags(flags);
1045 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1046 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1047 return (-EIO); /* no change => error */
1048 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1049 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1050 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1051 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1058 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1059 * Return: write counters to the user passed counter struct
1060 * NB: both 1->0 and 0->1 transitions are counted except for
1061 * RI where only 0->1 is counted.
1066 cnow = info->icount;
1067 restore_flags(flags);
1069 if(put_user(cnow.cts, &p_cuser->cts))
1071 if(put_user(cnow.dsr, &p_cuser->dsr))
1073 if(put_user(cnow.rng, &p_cuser->rng))
1075 return put_user(cnow.dcd, &p_cuser->dcd);
1076 case MOXA_HighSpeedOn:
1077 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1079 return (-ENOIOCTLCMD);
1084 static int mxser_ioctl_special(unsigned int cmd, unsigned long arg)
1086 int i, result, status;
1087 void __user *argp = (void __user *)arg;
1091 if(copy_to_user(argp, mxsercfg,
1092 sizeof(struct mxser_hwconf) * 4))
1095 case MOXA_GET_MAJOR:
1096 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1100 case MOXA_GET_CUMAJOR:
1102 if(copy_to_user(argp, &result, sizeof(int)))
1106 case MOXA_CHKPORTENABLE:
1108 for (i = 0; i < MXSER_PORTS; i++) {
1109 if (mxvar_table[i].base)
1112 return put_user(result, (unsigned long __user *) argp);
1113 case MOXA_GETDATACOUNT:
1114 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1117 case MOXA_GETMSTATUS:
1118 for (i = 0; i < MXSER_PORTS; i++) {
1120 if (!mxvar_table[i].base) {
1121 GMStatus[i].dcd = 0;
1122 GMStatus[i].dsr = 0;
1123 GMStatus[i].cts = 0;
1126 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1127 GMStatus[i].cflag = mxvar_table[i].cflag;
1129 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1131 status = inb(mxvar_table[i].base + UART_MSR);
1132 if (status & 0x80 /*UART_MSR_DCD */ )
1133 GMStatus[i].dcd = 1;
1135 GMStatus[i].dcd = 0;
1137 if (status & 0x20 /*UART_MSR_DSR */ )
1138 GMStatus[i].dsr = 1;
1140 GMStatus[i].dsr = 0;
1143 if (status & 0x10 /*UART_MSR_CTS */ )
1144 GMStatus[i].cts = 1;
1146 GMStatus[i].cts = 0;
1148 if(copy_to_user(argp, GMStatus,
1149 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1153 return (-ENOIOCTLCMD);
1159 * This routine is called by the upper-layer tty layer to signal that
1160 * incoming characters should be throttled.
1162 static void mxser_throttle(struct tty_struct *tty)
1164 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1165 unsigned long flags;
1168 info->x_char = STOP_CHAR(tty);
1172 info->IER |= UART_IER_THRI;
1173 outb(info->IER, info->base + UART_IER); /* force Tx interrupt */
1174 restore_flags(flags);
1176 if (info->tty->termios->c_cflag & CRTSCTS) {
1177 info->MCR &= ~UART_MCR_RTS;
1180 outb(info->MCR, info->base + UART_MCR);
1181 restore_flags(flags);
1185 static void mxser_unthrottle(struct tty_struct *tty)
1187 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1188 unsigned long flags;
1194 info->x_char = START_CHAR(tty);
1198 info->IER |= UART_IER_THRI; /* force Tx interrupt */
1199 outb(info->IER, info->base + UART_IER);
1200 restore_flags(flags);
1203 if (info->tty->termios->c_cflag & CRTSCTS) {
1204 info->MCR |= UART_MCR_RTS;
1207 outb(info->MCR, info->base + UART_MCR);
1208 restore_flags(flags);
1212 static void mxser_set_termios(struct tty_struct *tty,
1213 struct termios *old_termios)
1215 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1217 /* 8-2-99 by William
1218 if ( (tty->termios->c_cflag == old_termios->c_cflag) &&
1219 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
1220 RELEVANT_IFLAG(old_termios->c_iflag)) )
1223 mxser_change_speed(info, old_termios);
1225 if ( (old_termios->c_cflag & CRTSCTS) &&
1226 !(tty->termios->c_cflag & CRTSCTS) ) {
1227 tty->hw_stopped = 0;
1231 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1232 (RELEVANT_IFLAG(tty->termios->c_iflag) !=
1233 RELEVANT_IFLAG(old_termios->c_iflag))) {
1235 mxser_change_speed(info, old_termios);
1237 if ((old_termios->c_cflag & CRTSCTS) &&
1238 !(tty->termios->c_cflag & CRTSCTS)) {
1239 tty->hw_stopped = 0;
1243 /* Handle sw stopped */
1244 if ((old_termios->c_iflag & IXON) &&
1245 !(tty->termios->c_iflag & IXON)) {
1252 * mxser_stop() and mxser_start()
1254 * This routines are called before setting or resetting tty->stopped.
1255 * They enable or disable transmitter interrupts, as necessary.
1257 static void mxser_stop(struct tty_struct *tty)
1259 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1260 unsigned long flags;
1264 if (info->IER & UART_IER_THRI) {
1265 info->IER &= ~UART_IER_THRI;
1266 outb(info->IER, info->base + UART_IER);
1268 restore_flags(flags);
1271 static void mxser_start(struct tty_struct *tty)
1273 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1274 unsigned long flags;
1278 if (info->xmit_cnt && info->xmit_buf &&
1279 !(info->IER & UART_IER_THRI)) {
1280 info->IER |= UART_IER_THRI;
1281 outb(info->IER, info->base + UART_IER);
1283 restore_flags(flags);
1287 * This routine is called by tty_hangup() when a hangup is signaled.
1289 void mxser_hangup(struct tty_struct *tty)
1291 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1293 mxser_flush_buffer(tty);
1294 mxser_shutdown(info);
1297 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1299 wake_up_interruptible(&info->open_wait);
1302 static inline void mxser_receive_chars(struct mxser_struct *info,
1305 struct tty_struct *tty = info->tty;
1311 ch = inb(info->base + UART_RX);
1312 if (*status & info->ignore_status_mask) {
1313 if (++ignored > 100)
1316 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1319 if (*status & UART_LSR_SPECIAL) {
1320 if (*status & UART_LSR_BI) {
1321 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
1322 if (info->flags & ASYNC_SAK)
1324 } else if (*status & UART_LSR_PE) {
1325 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
1326 } else if (*status & UART_LSR_FE) {
1327 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
1328 } else if (*status & UART_LSR_OE) {
1329 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
1331 *tty->flip.flag_buf_ptr++ = 0;
1333 *tty->flip.flag_buf_ptr++ = 0;
1334 *tty->flip.char_buf_ptr++ = ch;
1337 *status = inb(info->base + UART_LSR) & info->read_status_mask;
1338 } while (*status & UART_LSR_DR);
1339 mxvar_log.rxcnt[info->port] += cnt;
1340 schedule_delayed_work(&tty->flip.work, 1);
1344 static inline void mxser_transmit_chars(struct mxser_struct *info)
1349 outb(info->x_char, info->base + UART_TX);
1351 mxvar_log.txcnt[info->port]++;
1354 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
1355 info->tty->hw_stopped) {
1356 info->IER &= ~UART_IER_THRI;
1357 outb(info->IER, info->base + UART_IER);
1360 cnt = info->xmit_cnt;
1361 count = info->xmit_fifo_size;
1363 outb(info->xmit_buf[info->xmit_tail++], info->base + UART_TX);
1364 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1365 if (--info->xmit_cnt <= 0)
1367 } while (--count > 0);
1368 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
1370 if (info->xmit_cnt < WAKEUP_CHARS) {
1371 set_bit(MXSER_EVENT_TXLOW, &info->event);
1372 schedule_work(&info->tqueue);
1374 if (info->xmit_cnt <= 0) {
1375 info->IER &= ~UART_IER_THRI;
1376 outb(info->IER, info->base + UART_IER);
1380 static inline void mxser_check_modem_status(struct mxser_struct *info,
1384 /* update input line counters */
1385 if (status & UART_MSR_TERI)
1387 if (status & UART_MSR_DDSR)
1389 if (status & UART_MSR_DDCD)
1391 if (status & UART_MSR_DCTS)
1393 wake_up_interruptible(&info->delta_msr_wait);
1395 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
1396 if (status & UART_MSR_DCD)
1397 wake_up_interruptible(&info->open_wait);
1399 set_bit(MXSER_EVENT_HANGUP, &info->event);
1400 schedule_work(&info->tqueue);
1402 if (info->flags & ASYNC_CTS_FLOW) {
1403 if (info->tty->hw_stopped) {
1404 if (status & UART_MSR_CTS) {
1405 info->tty->hw_stopped = 0;
1406 info->IER |= UART_IER_THRI;
1407 outb(info->IER, info->base + UART_IER);
1409 set_bit(MXSER_EVENT_TXLOW, &info->event);
1410 schedule_work(&info->tqueue);
1413 if (!(status & UART_MSR_CTS)) {
1414 info->tty->hw_stopped = 1;
1415 info->IER &= ~UART_IER_THRI;
1416 outb(info->IER, info->base + UART_IER);
1423 * This is the serial driver's generic interrupt routine
1425 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1428 struct mxser_struct *info;
1429 struct mxser_struct *port;
1430 int max, irqbits, bits, msr;
1431 int pass_counter = 0;
1435 for (i = 0; i < MXSER_BOARDS; i++) {
1436 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1442 if (i == MXSER_BOARDS)
1446 max = mxser_numports[mxsercfg[i].board_type];
1449 irqbits = inb(port->vector) & port->vectormask;
1450 if (irqbits == port->vectormask)
1453 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1454 if (irqbits == port->vectormask)
1460 (inb(info->base + UART_IIR) & UART_IIR_NO_INT))
1462 status = inb(info->base + UART_LSR) & info->read_status_mask;
1463 if (status & UART_LSR_DR)
1464 mxser_receive_chars(info, &status);
1465 msr = inb(info->base + UART_MSR);
1466 if (msr & UART_MSR_ANY_DELTA)
1467 mxser_check_modem_status(info, msr);
1468 if (status & UART_LSR_THRE) {
1469 /* 8-2-99 by William
1470 if ( info->x_char || (info->xmit_cnt > 0) )
1472 mxser_transmit_chars(info);
1475 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
1477 printk("MOXA Smartio/Indusrtio family driver interrupt loop break\n");
1479 break; /* Prevent infinite loops */
1482 return IRQ_RETVAL(handled);
1485 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
1486 struct mxser_struct *info)
1488 DECLARE_WAITQUEUE(wait, current);
1489 unsigned long flags;
1494 * If the device is in the middle of being closed, then block
1495 * until it's done, and then try again.
1497 if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
1498 if (info->flags & ASYNC_CLOSING)
1499 interruptible_sleep_on(&info->close_wait);
1500 #ifdef SERIAL_DO_RESTART
1501 if (info->flags & ASYNC_HUP_NOTIFY)
1504 return (-ERESTARTSYS);
1510 * If non-blocking mode is set, or the port is not enabled,
1511 * then make the check up front and then exit.
1513 if ((filp->f_flags & O_NONBLOCK) ||
1514 (tty->flags & (1 << TTY_IO_ERROR))) {
1515 info->flags |= ASYNC_NORMAL_ACTIVE;
1518 if (tty->termios->c_cflag & CLOCAL)
1522 * Block waiting for the carrier detect and the line to become
1523 * free (i.e., not in use by the callout). While we are in
1524 * this loop, info->count is dropped by one, so that
1525 * mxser_close() knows when to free things. We restore it upon
1526 * exit, either normal or abnormal.
1529 add_wait_queue(&info->open_wait, &wait);
1532 if (!tty_hung_up_p(filp))
1534 restore_flags(flags);
1535 info->blocked_open++;
1539 outb(inb(info->base + UART_MCR) | UART_MCR_DTR | UART_MCR_RTS,
1540 info->base + UART_MCR);
1541 restore_flags(flags);
1542 set_current_state(TASK_INTERRUPTIBLE);
1543 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
1544 #ifdef SERIAL_DO_RESTART
1545 if (info->flags & ASYNC_HUP_NOTIFY)
1548 retval = -ERESTARTSYS;
1554 if (!(info->flags & ASYNC_CLOSING) &&
1555 (do_clocal || (inb(info->base + UART_MSR) & UART_MSR_DCD)))
1557 if (signal_pending(current)) {
1558 retval = -ERESTARTSYS;
1563 set_current_state(TASK_RUNNING);
1564 remove_wait_queue(&info->open_wait, &wait);
1565 if (!tty_hung_up_p(filp))
1567 info->blocked_open--;
1570 info->flags |= ASYNC_NORMAL_ACTIVE;
1574 static int mxser_startup(struct mxser_struct *info)
1576 unsigned long flags;
1579 page = get_zeroed_page(GFP_KERNEL);
1586 if (info->flags & ASYNC_INITIALIZED) {
1588 restore_flags(flags);
1591 if (!info->base || !info->type) {
1593 set_bit(TTY_IO_ERROR, &info->tty->flags);
1595 restore_flags(flags);
1601 info->xmit_buf = (unsigned char *) page;
1604 * Clear the FIFO buffers and disable them
1605 * (they will be reenabled in mxser_change_speed())
1607 if (info->xmit_fifo_size == 16)
1608 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1609 info->base + UART_FCR);
1612 * At this point there's no way the LSR could still be 0xFF;
1613 * if it is, then bail out, because there's likely no UART
1616 if (inb(info->base + UART_LSR) == 0xff) {
1617 restore_flags(flags);
1618 if (capable(CAP_SYS_ADMIN)) {
1620 set_bit(TTY_IO_ERROR, &info->tty->flags);
1626 * Clear the interrupt registers.
1628 (void) inb(info->base + UART_LSR);
1629 (void) inb(info->base + UART_RX);
1630 (void) inb(info->base + UART_IIR);
1631 (void) inb(info->base + UART_MSR);
1634 * Now, initialize the UART
1636 outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
1637 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
1638 outb(info->MCR, info->base + UART_MCR);
1641 * Finally, enable interrupts
1643 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
1644 outb(info->IER, info->base + UART_IER); /* enable interrupts */
1647 * And clear the interrupt registers again for luck.
1649 (void) inb(info->base + UART_LSR);
1650 (void) inb(info->base + UART_RX);
1651 (void) inb(info->base + UART_IIR);
1652 (void) inb(info->base + UART_MSR);
1655 test_and_clear_bit(TTY_IO_ERROR, &info->tty->flags);
1657 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1660 * and set the speed of the serial port
1662 mxser_change_speed(info, NULL);
1664 info->flags |= ASYNC_INITIALIZED;
1665 restore_flags(flags);
1670 * This routine will shutdown a serial port; interrupts maybe disabled, and
1671 * DTR is dropped if the hangup on close termio flag is on.
1673 static void mxser_shutdown(struct mxser_struct *info)
1675 unsigned long flags;
1677 if (!(info->flags & ASYNC_INITIALIZED))
1681 cli(); /* Disable interrupts */
1684 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1685 * here so the queue might never be waken up
1687 wake_up_interruptible(&info->delta_msr_wait);
1690 * Free the IRQ, if necessary
1692 if (info->xmit_buf) {
1693 free_page((unsigned long) info->xmit_buf);
1694 info->xmit_buf = NULL;
1697 outb(0x00, info->base + UART_IER); /* disable all intrs */
1699 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1700 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1701 outb(info->MCR, info->base + UART_MCR);
1703 /* clear Rx/Tx FIFO's */
1704 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
1705 /* read data port to reset things */
1706 (void) inb(info->base + UART_RX);
1709 set_bit(TTY_IO_ERROR, &info->tty->flags);
1711 info->flags &= ~ASYNC_INITIALIZED;
1712 restore_flags(flags);
1716 * This routine is called to set the UART divisor registers to match
1717 * the specified baud rate for a serial port.
1719 static int mxser_change_speed(struct mxser_struct *info,
1720 struct termios *old_termios)
1723 unsigned cflag, cval, fcr;
1726 unsigned long flags;
1728 if (!info->tty || !info->tty->termios)
1730 cflag = info->tty->termios->c_cflag;
1735 #define B921600 (B460800 +1)
1737 switch (cflag & (CBAUD | CBAUDEX)) {
1804 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1805 i = 16; /* 57600 bps */
1806 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1807 i = 17; /* 115200 bps */
1809 #ifdef ASYNC_SPD_SHI
1810 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1814 #ifdef ASYNC_SPD_WARP
1815 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1819 if (mxvar_baud_table[i] == 134) {
1820 quot = (2 * info->baud_base / 269);
1821 } else if (mxvar_baud_table[i]) {
1822 quot = info->baud_base / mxvar_baud_table[i];
1823 if (!quot && old_termios) {
1825 info->tty->termios->c_cflag &= ~CBAUD;
1826 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1827 switch (info->tty->termios->c_cflag & (CBAUD | CBAUDEX)) {
1893 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1894 i = 16; /* 57600 bps */
1895 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1896 i = 17; /* 115200 bps */
1897 #ifdef ASYNC_SPD_SHI
1898 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1901 #ifdef ASYNC_SPD_WARP
1902 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1906 if (mxvar_baud_table[i] == 134) {
1907 quot = (2 * info->baud_base / 269);
1908 } else if (mxvar_baud_table[i]) {
1909 quot = info->baud_base / mxvar_baud_table[i];
1915 } else if (quot == 0)
1922 info->MCR |= UART_MCR_DTR;
1925 outb(info->MCR, info->base + UART_MCR);
1926 restore_flags(flags);
1928 info->MCR &= ~UART_MCR_DTR;
1931 outb(info->MCR, info->base + UART_MCR);
1932 restore_flags(flags);
1935 /* byte size and parity */
1936 switch (cflag & CSIZE) {
1951 break; /* too keep GCC shut... */
1956 cval |= UART_LCR_PARITY;
1957 if (!(cflag & PARODD))
1958 cval |= UART_LCR_EPAR;
1959 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
1962 fcr = UART_FCR_ENABLE_FIFO;
1963 switch (info->rx_trigger) {
1965 fcr |= UART_FCR_TRIGGER_1;
1968 fcr |= UART_FCR_TRIGGER_4;
1971 fcr |= UART_FCR_TRIGGER_8;
1974 fcr |= UART_FCR_TRIGGER_14;
1978 /* CTS flow control flag and modem status interrupts */
1979 info->IER &= ~UART_IER_MSI;
1980 info->MCR &= ~UART_MCR_AFE;
1981 if (cflag & CRTSCTS) {
1982 info->flags |= ASYNC_CTS_FLOW;
1983 info->IER |= UART_IER_MSI;
1984 if (info->type == PORT_16550A)
1985 info->MCR |= UART_MCR_AFE;
1987 info->flags &= ~ASYNC_CTS_FLOW;
1989 outb(info->MCR, info->base + UART_MCR);
1991 info->flags &= ~ASYNC_CHECK_CD;
1993 info->flags |= ASYNC_CHECK_CD;
1994 info->IER |= UART_IER_MSI;
1996 outb(info->IER, info->base + UART_IER);
1999 * Set up parity check flag
2001 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2002 if (I_INPCK(info->tty))
2003 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2004 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2005 info->read_status_mask |= UART_LSR_BI;
2007 info->ignore_status_mask = 0;
2009 /* This should be safe, but for some broken bits of hardware... */
2010 if (I_IGNPAR(info->tty)) {
2011 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2012 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
2015 if (I_IGNBRK(info->tty)) {
2016 info->ignore_status_mask |= UART_LSR_BI;
2017 info->read_status_mask |= UART_LSR_BI;
2019 * If we're ignore parity and break indicators, ignore
2020 * overruns too. (For real raw support).
2022 if (I_IGNPAR(info->tty)) {
2023 info->ignore_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2024 info->read_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2029 outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2030 outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2031 outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2032 outb(cval, info->base + UART_LCR); /* reset DLAB */
2033 outb(fcr, info->base + UART_FCR); /* set fcr */
2034 restore_flags(flags);
2040 * ------------------------------------------------------------
2041 * friends of mxser_ioctl()
2042 * ------------------------------------------------------------
2044 static int mxser_get_serial_info(struct mxser_struct *info,
2045 struct serial_struct __user *retinfo)
2047 struct serial_struct tmp;
2051 memset(&tmp, 0, sizeof(tmp));
2052 tmp.type = info->type;
2053 tmp.line = info->port;
2054 tmp.port = info->base;
2055 tmp.irq = info->irq;
2056 tmp.flags = info->flags;
2057 tmp.baud_base = info->baud_base;
2058 tmp.close_delay = info->close_delay;
2059 tmp.closing_wait = info->closing_wait;
2060 tmp.custom_divisor = info->custom_divisor;
2062 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2065 static int mxser_set_serial_info(struct mxser_struct *info,
2066 struct serial_struct __user *new_info)
2068 struct serial_struct new_serial;
2072 if (!new_info || !info->base)
2074 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2077 if ((new_serial.irq != info->irq) ||
2078 (new_serial.port != info->base) ||
2079 (new_serial.type != info->type) ||
2080 (new_serial.custom_divisor != info->custom_divisor) ||
2081 (new_serial.baud_base != info->baud_base))
2084 flags = info->flags & ASYNC_SPD_MASK;
2086 if (!capable(CAP_SYS_ADMIN)) {
2087 if ((new_serial.baud_base != info->baud_base) ||
2088 (new_serial.close_delay != info->close_delay) ||
2089 ((new_serial.flags & ~ASYNC_USR_MASK) !=
2090 (info->flags & ~ASYNC_USR_MASK)))
2092 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2093 (new_serial.flags & ASYNC_USR_MASK));
2096 * OK, past this point, all the error checking has been done.
2097 * At this point, we start making changes.....
2099 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2100 (new_serial.flags & ASYNC_FLAGS));
2101 info->close_delay = new_serial.close_delay * HZ / 100;
2102 info->closing_wait = new_serial.closing_wait * HZ / 100;
2105 if (info->flags & ASYNC_INITIALIZED) {
2106 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2107 mxser_change_speed(info, NULL);
2110 retval = mxser_startup(info);
2115 * mxser_get_lsr_info - get line status register info
2117 * Purpose: Let user call ioctl() to get info when the UART physically
2118 * is emptied. On bus types like RS485, the transmitter must
2119 * release the bus after transmitting. This must be done when
2120 * the transmit shift register is empty, not be done when the
2121 * transmit holding register is empty. This functionality
2122 * allows an RS485 driver to be written in user space.
2124 static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int __user *value)
2126 unsigned char status;
2127 unsigned int result;
2128 unsigned long flags;
2132 status = inb(info->base + UART_LSR);
2133 restore_flags(flags);
2134 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2135 return put_user(result, value);
2139 * This routine sends a break character out the serial port.
2141 static void mxser_send_break(struct mxser_struct *info, int duration)
2143 unsigned long flags;
2146 set_current_state(TASK_INTERRUPTIBLE);
2149 outb(inb(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
2150 schedule_timeout(duration);
2151 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
2152 restore_flags(flags);
2155 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2157 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
2158 unsigned char control, status;
2159 unsigned long flags;
2161 if (PORTNO(tty) == MXSER_PORTS)
2162 return (-ENOIOCTLCMD);
2163 if (tty->flags & (1 << TTY_IO_ERROR))
2166 control = info->MCR;
2169 status = inb(info->base + UART_MSR);
2170 if (status & UART_MSR_ANY_DELTA)
2171 mxser_check_modem_status(info, status);
2172 restore_flags(flags);
2173 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2174 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2175 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2176 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2177 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2178 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2181 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
2182 unsigned int set, unsigned int clear)
2184 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
2185 unsigned long flags;
2187 if (PORTNO(tty) == MXSER_PORTS)
2188 return (-ENOIOCTLCMD);
2189 if (tty->flags & (1 << TTY_IO_ERROR))
2194 if (set & TIOCM_RTS)
2195 info->MCR |= UART_MCR_RTS;
2196 if (set & TIOCM_DTR)
2197 info->MCR |= UART_MCR_DTR;
2199 if (clear & TIOCM_RTS)
2200 info->MCR &= ~UART_MCR_RTS;
2201 if (clear & TIOCM_DTR)
2202 info->MCR &= ~UART_MCR_DTR;
2204 outb(info->MCR, info->base + UART_MCR);
2205 restore_flags(flags);
2209 static int mxser_read_register(int, unsigned short *);
2210 static int mxser_program_mode(int);
2211 static void mxser_normal_mode(int);
2213 static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2216 unsigned short regs[16], irq;
2217 unsigned char scratch, scratch2;
2219 id = mxser_read_register(cap, regs);
2220 if (id == C168_ASIC_ID)
2221 hwconf->board_type = MXSER_BOARD_C168_ISA;
2222 else if (id == C104_ASIC_ID)
2223 hwconf->board_type = MXSER_BOARD_C104_ISA;
2224 else if (id == C102_ASIC_ID)
2225 hwconf->board_type = MXSER_BOARD_C102_ISA;
2226 else if (id == CI132_ASIC_ID)
2227 hwconf->board_type = MXSER_BOARD_CI132;
2228 else if (id == CI134_ASIC_ID)
2229 hwconf->board_type = MXSER_BOARD_CI134;
2230 else if (id == CI104J_ASIC_ID)
2231 hwconf->board_type = MXSER_BOARD_CI104J;
2234 irq = regs[9] & 0x0F;
2235 irq = irq | (irq << 4);
2236 irq = irq | (irq << 8);
2237 if ((irq != regs[9]) || ((id == 1) && (irq != regs[10]))) {
2238 return (MXSER_ERR_IRQ_CONFLIT);
2241 return (MXSER_ERR_IRQ);
2243 for (i = 0; i < 8; i++)
2244 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
2245 hwconf->irq = (int) (irq & 0x0F);
2246 if ((regs[12] & 0x80) == 0) {
2247 return (MXSER_ERR_VECTOR);
2249 hwconf->vector = (int) regs[11]; /* interrupt vector */
2251 hwconf->vector_mask = 0x00FF;
2253 hwconf->vector_mask = 0x000F;
2254 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2255 if (regs[12] & bits)
2256 hwconf->baud_base[i] = 921600;
2258 hwconf->baud_base[i] = 115200;
2260 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2261 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2262 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2263 outb(scratch2, cap + UART_LCR);
2264 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2265 scratch = inb(cap + UART_IIR);
2267 hwconf->uart_type = PORT_16550A;
2269 hwconf->uart_type = PORT_16450;
2274 return (hwconf->ports);
2277 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
2278 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
2279 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
2280 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
2281 #define EN_CCMD 0x000 /* Chip's command register */
2282 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
2283 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
2284 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
2285 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
2286 #define EN0_DCFG 0x00E /* Data configuration reg WR */
2287 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
2288 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
2289 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
2290 static int mxser_read_register(int port, unsigned short *regs)
2292 int i, k, value, id;
2295 id = mxser_program_mode(port);
2298 for (i = 0; i < 14; i++) {
2299 k = (i & 0x3F) | 0x180;
2300 for (j = 0x100; j > 0; j >>= 1) {
2301 outb(CHIP_CS, port);
2303 outb(CHIP_CS | CHIP_DO, port);
2304 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
2306 outb(CHIP_CS, port);
2307 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
2312 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
2313 outb(CHIP_CS, port);
2314 outb(CHIP_CS | CHIP_SK, port);
2315 if (inb(port) & CHIP_DI)
2321 mxser_normal_mode(port);
2325 static int mxser_program_mode(int port)
2328 unsigned long flags;
2339 restore_flags(flags);
2340 id = inb(port + 1) & 0x1F;
2341 if ((id != C168_ASIC_ID) && (id != C104_ASIC_ID) && (id != CI104J_ASIC_ID) &&
2342 (id != C102_ASIC_ID) && (id != CI132_ASIC_ID) && (id != CI134_ASIC_ID))
2344 for (i = 0, j = 0; i < 4; i++) {
2348 } else if ((j == 1) && (n == 1)) {
2359 static void mxser_normal_mode(int port)
2363 outb(0xA5, port + 1);
2364 outb(0x80, port + 3);
2365 outb(12, port + 0); /* 9600 bps */
2367 outb(0x03, port + 3); /* 8 data bits */
2368 outb(0x13, port + 4); /* loop back mode */
2369 for (i = 0; i < 16; i++) {
2371 if ((n & 0x61) == 0x60)
2376 outb(0x00, port + 4);
2379 module_init(mxser_module_init);
2380 module_exit(mxser_module_exit);