Coarsly sort out 32-bit-only, 64-bit-only and ``portable'' MIPS lib/
[linux-2.6/linux-mips.git] / drivers / char / mxser.c
blob1df25a26f88de431f71ea1d61e331dac1651f15f
1 /*****************************************************************************/
2 /*
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
31 * date : 2001/05/01
32 * version : 1.2
34 * Fixes for C104H/PCI by Tim Hockin <thockin@sun.com>
35 * Added support for: C102, CI-132, CI-134, CP-132, CP-114, CT-114 cards
36 * by Damian Wrobel <dwrobel@ertel.com.pl>
40 #include <linux/config.h>
41 #include <linux/module.h>
42 #include <linux/version.h>
43 #include <linux/errno.h>
44 #include <linux/signal.h>
45 #include <linux/sched.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/tty.h>
49 #include <linux/tty_flip.h>
50 #include <linux/serial.h>
51 #include <linux/serial_reg.h>
52 #include <linux/major.h>
53 #include <linux/string.h>
54 #include <linux/fcntl.h>
55 #include <linux/ptrace.h>
56 #include <linux/ioport.h>
57 #include <linux/mm.h>
58 #include <linux/smp_lock.h>
59 #include <linux/pci.h>
61 #include <asm/system.h>
62 #include <asm/io.h>
63 #include <asm/irq.h>
64 #include <asm/bitops.h>
65 #include <asm/uaccess.h>
67 #define MXSER_VERSION "1.2.1"
69 #define MXSERMAJOR 174
70 #define MXSERCUMAJOR 175
73 #define MXSER_EVENT_TXLOW 1
74 #define MXSER_EVENT_HANGUP 2
77 #define SERIAL_DO_RESTART
79 #define MXSER_BOARDS 4 /* Max. boards */
80 #define MXSER_PORTS 32 /* Max. ports */
81 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
82 #define MXSER_ISR_PASS_LIMIT 256
84 #define MXSER_ERR_IOADDR -1
85 #define MXSER_ERR_IRQ -2
86 #define MXSER_ERR_IRQ_CONFLIT -3
87 #define MXSER_ERR_VECTOR -4
89 #define SERIAL_TYPE_NORMAL 1
91 #define WAKEUP_CHARS 256
93 #define UART_MCR_AFE 0x20
94 #define UART_LSR_SPECIAL 0x1E
96 #define PORTNO(x) ((x)->index)
98 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
100 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
102 #ifndef MIN
103 #define MIN(a,b) ((a) < (b) ? (a) : (b))
104 #endif
107 * Define the Moxa PCI vendor and device IDs.
110 #ifndef PCI_VENDOR_ID_MOXA
111 #define PCI_VENDOR_ID_MOXA 0x1393
112 #endif
113 #ifndef PCI_DEVICE_ID_C168
114 #define PCI_DEVICE_ID_C168 0x1680
115 #endif
116 #ifndef PCI_DEVICE_ID_C104
117 #define PCI_DEVICE_ID_C104 0x1040
118 #endif
119 #ifndef PCI_DEVICE_ID_CP132
120 #define PCI_DEVICE_ID_CP132 0x1320
121 #endif
122 #ifndef PCI_DEVICE_ID_CP114
123 #define PCI_DEVICE_ID_CP114 0x1141
124 #endif
125 #ifndef PCI_DEVICE_ID_CT114
126 #define PCI_DEVICE_ID_CT114 0x1140
127 #endif
129 #define C168_ASIC_ID 1
130 #define C104_ASIC_ID 2
131 #define CI134_ASIC_ID 3
132 #define CI132_ASIC_ID 4
133 #define CI104J_ASIC_ID 5
134 #define C102_ASIC_ID 0xB
136 enum {
137 MXSER_BOARD_C168_ISA = 0,
138 MXSER_BOARD_C104_ISA,
139 MXSER_BOARD_CI104J,
140 MXSER_BOARD_C168_PCI,
141 MXSER_BOARD_C104_PCI,
142 MXSER_BOARD_C102_ISA,
143 MXSER_BOARD_CI132,
144 MXSER_BOARD_CI134,
145 MXSER_BOARD_CP132_PCI,
146 MXSER_BOARD_CP114_PCI,
147 MXSER_BOARD_CT114_PCI
150 static char *mxser_brdname[] =
152 "C168 series",
153 "C104 series",
154 "CI-104J series",
155 "C168H/PCI series",
156 "C104H/PCI series",
157 "C102 series",
158 "CI-132 series",
159 "CI-134 series",
160 "CP-132 series",
161 "CP-114 series",
162 "CT-114 series"
165 static int mxser_numports[] =
181 * MOXA ioctls
183 #define MOXA 0x400
184 #define MOXA_GETDATACOUNT (MOXA + 23)
185 #define MOXA_GET_CONF (MOXA + 35)
186 #define MOXA_DIAGNOSE (MOXA + 50)
187 #define MOXA_CHKPORTENABLE (MOXA + 60)
188 #define MOXA_HighSpeedOn (MOXA + 61)
189 #define MOXA_GET_MAJOR (MOXA + 63)
190 #define MOXA_GET_CUMAJOR (MOXA + 64)
191 #define MOXA_GETMSTATUS (MOXA + 65)
193 static struct pci_device_id mxser_pcibrds[] = {
194 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C168, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
195 MXSER_BOARD_C168_PCI },
196 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C104, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
197 MXSER_BOARD_C104_PCI },
198 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP132, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
199 MXSER_BOARD_CP132_PCI },
200 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP114, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
201 MXSER_BOARD_CP114_PCI },
202 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CT114, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
203 MXSER_BOARD_CT114_PCI },
204 { 0 }
206 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
208 static int ioaddr[MXSER_BOARDS];
209 static int ttymajor = MXSERMAJOR;
210 static int verbose;
212 /* Variables for insmod */
214 MODULE_AUTHOR("William Chen");
215 MODULE_DESCRIPTION("MOXA Smartio Family Multiport Board Device Driver");
216 MODULE_LICENSE("GPL");
217 MODULE_PARM(ioaddr, "1-4i");
218 MODULE_PARM(ttymajor, "i");
219 MODULE_PARM(verbose, "i");
221 struct mxser_hwconf {
222 int board_type;
223 int ports;
224 int irq;
225 int vector;
226 int vector_mask;
227 int uart_type;
228 int ioaddr[MXSER_PORTS_PER_BOARD];
229 int baud_base[MXSER_PORTS_PER_BOARD];
230 struct pci_dev *pdev;
233 struct mxser_struct {
234 int port;
235 int base; /* port base address */
236 int irq; /* port using irq no. */
237 int vector; /* port irq vector */
238 int vectormask; /* port vector mask */
239 int rx_trigger; /* Rx fifo trigger level */
240 int baud_base; /* max. speed */
241 int flags; /* defined in tty.h */
242 int type; /* UART type */
243 struct tty_struct *tty;
244 int read_status_mask;
245 int ignore_status_mask;
246 int xmit_fifo_size;
247 int custom_divisor;
248 int x_char; /* xon/xoff character */
249 int close_delay;
250 unsigned short closing_wait;
251 int IER; /* Interrupt Enable Register */
252 int MCR; /* Modem control register */
253 unsigned long event;
254 int count; /* # of fd on device */
255 int blocked_open; /* # of blocked opens */
256 unsigned char *xmit_buf;
257 int xmit_head;
258 int xmit_tail;
259 int xmit_cnt;
260 struct work_struct tqueue;
261 int cflag;
262 wait_queue_head_t open_wait;
263 wait_queue_head_t close_wait;
264 wait_queue_head_t delta_msr_wait;
265 struct async_icount icount; /* kernel counters for the 4 input interrupts */
268 struct mxser_log {
269 int tick;
270 int rxcnt[MXSER_PORTS];
271 int txcnt[MXSER_PORTS];
274 struct mxser_mstatus {
275 tcflag_t cflag;
276 int cts;
277 int dsr;
278 int ri;
279 int dcd;
282 static struct mxser_mstatus GMStatus[MXSER_PORTS];
284 static int mxserBoardCAP[MXSER_BOARDS] =
286 0, 0, 0, 0
287 /* 0x180, 0x280, 0x200, 0x320 */
291 static struct tty_driver *mxvar_sdriver;
292 static struct mxser_struct mxvar_table[MXSER_PORTS];
293 static struct mxser_log mxvar_log;
294 static int mxvar_diagflag;
296 * mxvar_tmp_buf is used as a temporary buffer by serial_write. We need
297 * to lock it in case the memcpy_fromfs blocks while swapping in a page,
298 * and some other program tries to do a serial write at the same time.
299 * Since the lock will only come under contention when the system is
300 * swapping and available memory is low, it makes sense to share one
301 * buffer across all the serial ports, since it significantly saves
302 * memory if large numbers of serial ports are open.
304 static unsigned char *mxvar_tmp_buf;
305 static struct semaphore mxvar_tmp_buf_sem;
308 * This is used to figure out the divisor speeds and the timeouts
310 static int mxvar_baud_table[] =
312 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
313 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 0};
315 struct mxser_hwconf mxsercfg[MXSER_BOARDS];
318 * static functions:
321 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
322 static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
323 static int mxser_get_PCI_conf(struct pci_dev *, int, struct mxser_hwconf *);
324 static void mxser_do_softint(void *);
325 static int mxser_open(struct tty_struct *, struct file *);
326 static void mxser_close(struct tty_struct *, struct file *);
327 static int mxser_write(struct tty_struct *, int, const unsigned char *, int);
328 static int mxser_write_room(struct tty_struct *);
329 static void mxser_flush_buffer(struct tty_struct *);
330 static int mxser_chars_in_buffer(struct tty_struct *);
331 static void mxser_flush_chars(struct tty_struct *);
332 static void mxser_put_char(struct tty_struct *, unsigned char);
333 static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
334 static int mxser_ioctl_special(unsigned int, unsigned long);
335 static void mxser_throttle(struct tty_struct *);
336 static void mxser_unthrottle(struct tty_struct *);
337 static void mxser_set_termios(struct tty_struct *, struct termios *);
338 static void mxser_stop(struct tty_struct *);
339 static void mxser_start(struct tty_struct *);
340 static void mxser_hangup(struct tty_struct *);
341 static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
342 static inline void mxser_receive_chars(struct mxser_struct *, int *);
343 static inline void mxser_transmit_chars(struct mxser_struct *);
344 static inline void mxser_check_modem_status(struct mxser_struct *, int);
345 static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
346 static int mxser_startup(struct mxser_struct *);
347 static void mxser_shutdown(struct mxser_struct *);
348 static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
349 static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct *);
350 static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct *);
351 static int mxser_get_lsr_info(struct mxser_struct *, unsigned int *);
352 static void mxser_send_break(struct mxser_struct *, int);
353 static int mxser_get_modem_info(struct mxser_struct *, unsigned int *);
354 static int mxser_set_modem_info(struct mxser_struct *, unsigned int, unsigned int *);
357 * The MOXA C168/C104 serial driver boot-time initialization code!
360 static void __exit mxser_module_exit(void)
362 int i, err = 0;
365 if (verbose)
366 printk("Unloading module mxser ...\n");
367 if ((err |= tty_unregister_driver(mxvar_sdriver)))
368 printk("Couldn't unregister MOXA Smartio family serial driver\n");
369 put_tty_driver(mxvar_sdriver);
371 for (i = 0; i < MXSER_BOARDS; i++) {
372 if (mxsercfg[i].board_type == -1)
373 continue;
374 else {
375 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
379 if (verbose)
380 printk("Done.\n");
384 int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
386 struct mxser_struct *info;
387 unsigned long flags;
388 int retval;
389 int i, n;
391 init_MUTEX(&mxvar_tmp_buf_sem);
393 n = board * MXSER_PORTS_PER_BOARD;
394 info = &mxvar_table[n];
395 for (i = 0; i < hwconf->ports; i++, n++, info++) {
396 if (verbose) {
397 printk(" ttyM%d/cum%d at 0x%04x ", n, n, hwconf->ioaddr[i]);
398 if (hwconf->baud_base[i] == 115200)
399 printk(" max. baud rate up to 115200 bps.\n");
400 else
401 printk(" max. baud rate up to 921600 bps.\n");
403 info->port = n;
404 info->base = hwconf->ioaddr[i];
405 info->irq = hwconf->irq;
406 info->vector = hwconf->vector;
407 info->vectormask = hwconf->vector_mask;
408 info->rx_trigger = 14;
409 info->baud_base = hwconf->baud_base[i];
410 info->flags = ASYNC_SHARE_IRQ;
411 info->type = hwconf->uart_type;
412 if ((info->type == PORT_16450) || (info->type == PORT_8250))
413 info->xmit_fifo_size = 1;
414 else
415 info->xmit_fifo_size = 16;
416 info->custom_divisor = hwconf->baud_base[i] * 16;
417 info->close_delay = 5 * HZ / 10;
418 info->closing_wait = 30 * HZ;
419 INIT_WORK(&info->tqueue, mxser_do_softint, info);
420 info->cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
421 init_waitqueue_head(&info->open_wait);
422 init_waitqueue_head(&info->close_wait);
423 init_waitqueue_head(&info->delta_msr_wait);
427 * Allocate the IRQ if necessary
429 save_flags(flags);
431 n = board * MXSER_PORTS_PER_BOARD;
432 info = &mxvar_table[n];
434 cli();
435 retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
436 "mxser", info);
437 if (retval) {
438 restore_flags(flags);
439 printk("Board %d: %s", board, mxser_brdname[hwconf->board_type]);
440 printk(" Request irq fail,IRQ (%d) may be conflit with another device.\n", info->irq);
441 return (retval);
443 restore_flags(flags);
445 return 0;
449 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
451 mxsercfg[board] = *hwconf;
454 static int mxser_get_PCI_conf(struct pci_dev *pdev, int board_type, struct mxser_hwconf *hwconf)
456 int i;
457 unsigned int ioaddress;
459 hwconf->board_type = board_type;
460 hwconf->ports = mxser_numports[board_type];
461 ioaddress = pci_resource_start (pdev, 2);
462 for (i = 0; i < hwconf->ports; i++)
463 hwconf->ioaddr[i] = ioaddress + 8 * i;
465 ioaddress = pci_resource_start (pdev, 3);
466 hwconf->vector = ioaddress;
468 hwconf->irq = pdev->irq;
470 hwconf->uart_type = PORT_16550A;
471 hwconf->vector_mask = 0;
472 for (i = 0; i < hwconf->ports; i++) {
473 hwconf->vector_mask |= (1 << i);
474 hwconf->baud_base[i] = 921600;
476 return (0);
479 static struct tty_operations mxser_ops = {
480 .open = mxser_open,
481 .close = mxser_close,
482 .write = mxser_write,
483 .put_char = mxser_put_char,
484 .flush_chars = mxser_flush_chars,
485 .write_room = mxser_write_room,
486 .chars_in_buffer = mxser_chars_in_buffer,
487 .flush_buffer = mxser_flush_buffer,
488 .ioctl = mxser_ioctl,
489 .throttle = mxser_throttle,
490 .unthrottle = mxser_unthrottle,
491 .set_termios = mxser_set_termios,
492 .stop = mxser_stop,
493 .start = mxser_start,
494 .hangup = mxser_hangup,
497 static int __init mxser_module_init(void)
499 int i, m, retval, b;
500 int n, index;
501 int ret1, ret2;
502 struct mxser_hwconf hwconf;
504 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
505 if (!mxvar_sdriver)
506 return -ENOMEM;
508 printk("MOXA Smartio family driver version %s\n", MXSER_VERSION);
510 /* Initialize the tty_driver structure */
512 mxvar_sdriver->owner = THIS_MODULE;
513 mxvar_sdriver->name = "ttyM";
514 mxvar_sdriver->major = ttymajor;
515 mxvar_sdriver->minor_start = 0;
516 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
517 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
518 mxvar_sdriver->init_termios = tty_std_termios;
519 mxvar_sdriver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
520 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
521 tty_set_operations(mxvar_sdriver, &mxser_ops);
522 printk("Tty devices major number = %d\n", ttymajor);
524 mxvar_diagflag = 0;
525 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
526 memset(&mxvar_log, 0, sizeof(struct mxser_log));
529 m = 0;
530 /* Start finding ISA boards here */
531 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
532 int cap;
533 if (!(cap = mxserBoardCAP[b]))
534 continue;
536 retval = mxser_get_ISA_conf(cap, &hwconf);
538 if (retval != 0)
539 printk("Found MOXA %s board (CAP=0x%x)\n",
540 mxser_brdname[hwconf.board_type],
541 ioaddr[b]);
543 if (retval <= 0) {
544 if (retval == MXSER_ERR_IRQ)
545 printk("Invalid interrupt number,board not configured\n");
546 else if (retval == MXSER_ERR_IRQ_CONFLIT)
547 printk("Invalid interrupt number,board not configured\n");
548 else if (retval == MXSER_ERR_VECTOR)
549 printk("Invalid interrupt vector,board not configured\n");
550 else if (retval == MXSER_ERR_IOADDR)
551 printk("Invalid I/O address,board not configured\n");
553 continue;
555 hwconf.pdev = NULL;
557 if (mxser_initbrd(m, &hwconf) < 0)
558 continue;
560 mxser_getcfg(m, &hwconf);
562 m++;
565 /* Start finding ISA boards from module arg */
566 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
567 int cap;
568 if (!(cap = ioaddr[b]))
569 continue;
571 retval = mxser_get_ISA_conf(cap, &hwconf);
573 if (retval != 0)
574 printk("Found MOXA %s board (CAP=0x%x)\n",
575 mxser_brdname[hwconf.board_type],
576 ioaddr[b]);
578 if (retval <= 0) {
579 if (retval == MXSER_ERR_IRQ)
580 printk("Invalid interrupt number,board not configured\n");
581 else if (retval == MXSER_ERR_IRQ_CONFLIT)
582 printk("Invalid interrupt number,board not configured\n");
583 else if (retval == MXSER_ERR_VECTOR)
584 printk("Invalid interrupt vector,board not configured\n");
585 else if (retval == MXSER_ERR_IOADDR)
586 printk("Invalid I/O address,board not configured\n");
588 continue;
590 hwconf.pdev = NULL;
592 if (mxser_initbrd(m, &hwconf) < 0)
593 continue;
595 mxser_getcfg(m, &hwconf);
597 m++;
600 /* start finding PCI board here */
602 #ifdef CONFIG_PCI
604 struct pci_dev *pdev = NULL;
606 n = (sizeof(mxser_pcibrds) / sizeof(mxser_pcibrds[0])) - 1;
607 index = 0;
608 for (b = 0; b < n; b++) {
609 while ((pdev = pci_find_device(mxser_pcibrds[b].vendor, mxser_pcibrds[b].device, pdev)))
611 if (pci_enable_device(pdev))
612 continue;
613 hwconf.pdev = pdev;
614 printk("Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
615 mxser_brdname[mxser_pcibrds[b].driver_data],
616 pdev->bus->number, PCI_SLOT(pdev->devfn));
617 if (m >= MXSER_BOARDS) {
618 printk("Too many Smartio family boards found (maximum %d),board not configured\n", MXSER_BOARDS);
619 } else {
620 retval = mxser_get_PCI_conf(pdev, mxser_pcibrds[b].driver_data, &hwconf);
621 if (retval < 0) {
622 if (retval == MXSER_ERR_IRQ)
623 printk("Invalid interrupt number,board not configured\n");
624 else if (retval == MXSER_ERR_IRQ_CONFLIT)
625 printk("Invalid interrupt number,board not configured\n");
626 else if (retval == MXSER_ERR_VECTOR)
627 printk("Invalid interrupt vector,board not configured\n");
628 else if (retval == MXSER_ERR_IOADDR)
629 printk("Invalid I/O address,board not configured\n");
630 continue;
632 if (mxser_initbrd(m, &hwconf) < 0)
633 continue;
634 mxser_getcfg(m, &hwconf);
635 m++;
640 #endif
642 for (i = m; i < MXSER_BOARDS; i++) {
643 mxsercfg[i].board_type = -1;
647 if (!tty_register_driver(mxvar_sdriver))
648 return 0;
650 put_tty_driver(mxvar_sdriver);
651 printk("Couldn't install MOXA Smartio family driver !\n");
653 for (i = 0; i < MXSER_BOARDS; i++) {
654 if (mxsercfg[i].board_type == -1)
655 continue;
656 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
658 return -1;
661 static void mxser_do_softint(void *private_)
663 struct mxser_struct *info = (struct mxser_struct *) private_;
664 struct tty_struct *tty;
666 tty = info->tty;
667 if (tty) {
668 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event)) {
669 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
670 tty->ldisc.write_wakeup)
671 (tty->ldisc.write_wakeup) (tty);
672 wake_up_interruptible(&tty->write_wait);
674 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event)) {
675 tty_hangup(tty); /* FIXME: module removal race here - AKPM */
681 * This routine is called whenever a serial port is opened. It
682 * enables interrupts for a serial port, linking in its async structure into
683 * the IRQ chain. It also performs the serial-specific
684 * initialization for the tty structure.
687 static int mxser_open(struct tty_struct *tty, struct file *filp)
689 struct mxser_struct *info;
690 int retval, line;
691 unsigned long page;
693 line = PORTNO(tty);
694 if (line == MXSER_PORTS)
695 return (0);
696 if ((line < 0) || (line > MXSER_PORTS))
697 return (-ENODEV);
698 info = mxvar_table + line;
699 if (!info->base)
700 return (-ENODEV);
702 info->count++;
703 tty->driver_data = info;
704 info->tty = tty;
706 if (!mxvar_tmp_buf) {
707 page = get_zeroed_page(GFP_KERNEL);
708 if (!page)
709 return (-ENOMEM);
710 if (mxvar_tmp_buf)
711 free_page(page);
712 else
713 mxvar_tmp_buf = (unsigned char *) page;
716 * Start up serial port
718 retval = mxser_startup(info);
719 if (retval)
720 return (retval);
722 return mxser_block_til_ready(tty, filp, info);
726 * This routine is called when the serial port gets closed. First, we
727 * wait for the last remaining data to be sent. Then, we unlink its
728 * async structure from the interrupt chain if necessary, and we free
729 * that IRQ if nothing is left in the chain.
732 static void mxser_close(struct tty_struct *tty, struct file *filp)
734 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
735 unsigned long flags;
736 unsigned long timeout;
738 if (PORTNO(tty) == MXSER_PORTS)
739 return;
740 if (!info)
741 return;
743 save_flags(flags);
744 cli();
746 if (tty_hung_up_p(filp)) {
747 restore_flags(flags);
748 return;
750 if ((tty->count == 1) && (info->count != 1)) {
752 * Uh, oh. tty->count is 1, which means that the tty
753 * structure will be freed. Info->count should always
754 * be one in these conditions. If it's greater than
755 * one, we've got real problems, since it means the
756 * serial port won't be shutdown.
758 printk("mxser_close: bad serial port count; tty->count is 1, "
759 "info->count is %d\n", info->count);
760 info->count = 1;
762 if (--info->count < 0) {
763 printk("mxser_close: bad serial port count for ttys%d: %d\n",
764 info->port, info->count);
765 info->count = 0;
767 if (info->count) {
768 restore_flags(flags);
769 return;
771 info->flags |= ASYNC_CLOSING;
772 info->cflag = tty->termios->c_cflag;
774 * Now we wait for the transmit buffer to clear; and we notify
775 * the line discipline to only process XON/XOFF characters.
777 tty->closing = 1;
778 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
779 tty_wait_until_sent(tty, info->closing_wait);
781 * At this point we stop accepting input. To do this, we
782 * disable the receive line status interrupts, and tell the
783 * interrupt driver to stop checking the data ready bit in the
784 * line status register.
786 info->IER &= ~UART_IER_RLSI;
787 /* by William
788 info->read_status_mask &= ~UART_LSR_DR;
790 if (info->flags & ASYNC_INITIALIZED) {
791 outb(info->IER, info->base + UART_IER);
793 * Before we drop DTR, make sure the UART transmitter
794 * has completely drained; this is especially
795 * important if there is a transmit FIFO!
797 timeout = jiffies + HZ;
798 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
799 set_current_state(TASK_INTERRUPTIBLE);
800 schedule_timeout(5);
801 if (time_after(jiffies, timeout))
802 break;
805 mxser_shutdown(info);
806 if (tty->driver->flush_buffer)
807 tty->driver->flush_buffer(tty);
808 if (tty->ldisc.flush_buffer)
809 tty->ldisc.flush_buffer(tty);
810 tty->closing = 0;
811 info->event = 0;
812 info->tty = 0;
813 if (info->blocked_open) {
814 if (info->close_delay) {
815 set_current_state(TASK_INTERRUPTIBLE);
816 schedule_timeout(info->close_delay);
818 wake_up_interruptible(&info->open_wait);
820 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
821 wake_up_interruptible(&info->close_wait);
822 restore_flags(flags);
826 static int mxser_write(struct tty_struct *tty, int from_user,
827 const unsigned char *buf, int count)
829 int c, total = 0;
830 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
831 unsigned long flags;
833 if (!tty || !info->xmit_buf || !mxvar_tmp_buf)
834 return (0);
836 save_flags(flags);
837 if (from_user) {
838 down(&mxvar_tmp_buf_sem);
839 while (1) {
840 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
841 SERIAL_XMIT_SIZE - info->xmit_head));
842 if (c <= 0)
843 break;
845 c -= copy_from_user(mxvar_tmp_buf, buf, c);
846 if (!c) {
847 if (!total)
848 total = -EFAULT;
849 break;
852 cli();
853 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
854 SERIAL_XMIT_SIZE - info->xmit_head));
855 memcpy(info->xmit_buf + info->xmit_head, mxvar_tmp_buf, c);
856 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
857 info->xmit_cnt += c;
858 restore_flags(flags);
860 buf += c;
861 count -= c;
862 total += c;
864 up(&mxvar_tmp_buf_sem);
865 } else {
866 while (1) {
867 cli();
868 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
869 SERIAL_XMIT_SIZE - info->xmit_head));
870 if (c <= 0) {
871 restore_flags(flags);
872 break;
875 memcpy(info->xmit_buf + info->xmit_head, buf, c);
876 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
877 info->xmit_cnt += c;
878 restore_flags(flags);
880 buf += c;
881 count -= c;
882 total += c;
886 cli();
887 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
888 !(info->IER & UART_IER_THRI)) {
889 info->IER |= UART_IER_THRI;
890 outb(info->IER, info->base + UART_IER);
892 restore_flags(flags);
893 return (total);
896 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
898 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
899 unsigned long flags;
901 if (!tty || !info->xmit_buf)
902 return;
904 save_flags(flags);
905 cli();
906 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
907 restore_flags(flags);
908 return;
910 info->xmit_buf[info->xmit_head++] = ch;
911 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
912 info->xmit_cnt++;
913 /********************************************** why ??? ***********
914 if ( !tty->stopped && !tty->hw_stopped &&
915 !(info->IER & UART_IER_THRI) ) {
916 info->IER |= UART_IER_THRI;
917 outb(info->IER, info->base + UART_IER);
919 *****************************************************************/
920 restore_flags(flags);
923 static void mxser_flush_chars(struct tty_struct *tty)
925 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
926 unsigned long flags;
928 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
929 !info->xmit_buf)
930 return;
932 save_flags(flags);
933 cli();
934 info->IER |= UART_IER_THRI;
935 outb(info->IER, info->base + UART_IER);
936 restore_flags(flags);
939 static int mxser_write_room(struct tty_struct *tty)
941 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
942 int ret;
944 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
945 if (ret < 0)
946 ret = 0;
947 return (ret);
950 static int mxser_chars_in_buffer(struct tty_struct *tty)
952 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
954 return (info->xmit_cnt);
957 static void mxser_flush_buffer(struct tty_struct *tty)
959 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
960 unsigned long flags;
962 save_flags(flags);
963 cli();
964 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
965 restore_flags(flags);
966 wake_up_interruptible(&tty->write_wait);
967 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
968 tty->ldisc.write_wakeup)
969 (tty->ldisc.write_wakeup) (tty);
972 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
973 unsigned int cmd, unsigned long arg)
975 unsigned long flags;
976 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
977 int retval;
978 struct async_icount cprev, cnow; /* kernel counter temps */
979 struct serial_icounter_struct *p_cuser; /* user space */
980 unsigned long templ;
982 if (PORTNO(tty) == MXSER_PORTS)
983 return (mxser_ioctl_special(cmd, arg));
984 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) &&
985 (cmd != TIOCGICOUNT)) {
986 if (tty->flags & (1 << TTY_IO_ERROR))
987 return (-EIO);
989 switch (cmd) {
990 case TCSBRK: /* SVID version: non-zero arg --> no break */
991 retval = tty_check_change(tty);
992 if (retval)
993 return (retval);
994 tty_wait_until_sent(tty, 0);
995 if (!arg)
996 mxser_send_break(info, HZ / 4); /* 1/4 second */
997 return (0);
998 case TCSBRKP: /* support for POSIX tcsendbreak() */
999 retval = tty_check_change(tty);
1000 if (retval)
1001 return (retval);
1002 tty_wait_until_sent(tty, 0);
1003 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1004 return (0);
1005 case TIOCGSOFTCAR:
1006 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
1007 case TIOCSSOFTCAR:
1008 if(get_user(templ, (unsigned long *) arg))
1009 return -EFAULT;
1010 arg = templ;
1011 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1012 (arg ? CLOCAL : 0));
1013 return (0);
1014 case TIOCMGET:
1015 return (mxser_get_modem_info(info, (unsigned int *) arg));
1016 case TIOCMBIS:
1017 case TIOCMBIC:
1018 case TIOCMSET:
1019 return (mxser_set_modem_info(info, cmd, (unsigned int *) arg));
1020 case TIOCGSERIAL:
1021 return (mxser_get_serial_info(info, (struct serial_struct *) arg));
1022 case TIOCSSERIAL:
1023 return (mxser_set_serial_info(info, (struct serial_struct *) arg));
1024 case TIOCSERGETLSR: /* Get line status register */
1025 return (mxser_get_lsr_info(info, (unsigned int *) arg));
1027 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1028 * - mask passed in arg for lines of interest
1029 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1030 * Caller should use TIOCGICOUNT to see which one it was
1032 case TIOCMIWAIT:
1033 save_flags(flags);
1034 cli();
1035 cprev = info->icount; /* note the counters on entry */
1036 restore_flags(flags);
1037 while (1) {
1038 interruptible_sleep_on(&info->delta_msr_wait);
1039 /* see if a signal did it */
1040 if (signal_pending(current))
1041 return (-ERESTARTSYS);
1042 save_flags(flags);
1043 cli();
1044 cnow = info->icount; /* atomic copy */
1045 restore_flags(flags);
1046 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1047 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1048 return (-EIO); /* no change => error */
1049 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1050 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1051 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1052 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1053 return (0);
1055 cprev = cnow;
1057 /* NOTREACHED */
1059 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1060 * Return: write counters to the user passed counter struct
1061 * NB: both 1->0 and 0->1 transitions are counted except for
1062 * RI where only 0->1 is counted.
1064 case TIOCGICOUNT:
1065 save_flags(flags);
1066 cli();
1067 cnow = info->icount;
1068 restore_flags(flags);
1069 p_cuser = (struct serial_icounter_struct *) arg;
1070 if(put_user(cnow.cts, &p_cuser->cts))
1071 return -EFAULT;
1072 if(put_user(cnow.dsr, &p_cuser->dsr))
1073 return -EFAULT;
1074 if(put_user(cnow.rng, &p_cuser->rng))
1075 return -EFAULT;
1076 return put_user(cnow.dcd, &p_cuser->dcd);
1077 case MOXA_HighSpeedOn:
1078 return put_user(info->baud_base != 115200 ? 1 : 0, (int *) arg);
1079 default:
1080 return (-ENOIOCTLCMD);
1082 return (0);
1085 static int mxser_ioctl_special(unsigned int cmd, unsigned long arg)
1087 int i, result, status;
1089 switch (cmd) {
1090 case MOXA_GET_CONF:
1091 if(copy_to_user((struct mxser_hwconf *) arg, mxsercfg,
1092 sizeof(struct mxser_hwconf) * 4))
1093 return -EFAULT;
1094 return 0;
1095 case MOXA_GET_MAJOR:
1096 if(copy_to_user((int *) arg, &ttymajor, sizeof(int)))
1097 return -EFAULT;
1098 return 0;
1100 case MOXA_GET_CUMAJOR:
1101 result = 0;
1102 if(copy_to_user((int *) arg, &result, sizeof(int)))
1103 return -EFAULT;
1104 return 0;
1106 case MOXA_CHKPORTENABLE:
1107 result = 0;
1108 for (i = 0; i < MXSER_PORTS; i++) {
1109 if (mxvar_table[i].base)
1110 result |= (1 << i);
1112 return put_user(result, (unsigned long *) arg);
1113 case MOXA_GETDATACOUNT:
1114 if(copy_to_user((struct mxser_log *) arg, &mxvar_log, sizeof(mxvar_log)))
1115 return -EFAULT;
1116 return (0);
1117 case MOXA_GETMSTATUS:
1118 for (i = 0; i < MXSER_PORTS; i++) {
1119 GMStatus[i].ri = 0;
1120 if (!mxvar_table[i].base) {
1121 GMStatus[i].dcd = 0;
1122 GMStatus[i].dsr = 0;
1123 GMStatus[i].cts = 0;
1124 continue;
1126 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1127 GMStatus[i].cflag = mxvar_table[i].cflag;
1128 else
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;
1134 else
1135 GMStatus[i].dcd = 0;
1137 if (status & 0x20 /*UART_MSR_DSR */ )
1138 GMStatus[i].dsr = 1;
1139 else
1140 GMStatus[i].dsr = 0;
1143 if (status & 0x10 /*UART_MSR_CTS */ )
1144 GMStatus[i].cts = 1;
1145 else
1146 GMStatus[i].cts = 0;
1148 if(copy_to_user((struct mxser_mstatus *) arg, GMStatus,
1149 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1150 return -EFAULT;
1151 return 0;
1152 default:
1153 return (-ENOIOCTLCMD);
1155 return (0);
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;
1167 if (I_IXOFF(tty)) {
1168 info->x_char = STOP_CHAR(tty);
1169 save_flags(flags);
1170 cli();
1171 outb(info->IER, 0);
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;
1178 save_flags(flags);
1179 cli();
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;
1190 if (I_IXOFF(tty)) {
1191 if (info->x_char)
1192 info->x_char = 0;
1193 else {
1194 info->x_char = START_CHAR(tty);
1195 save_flags(flags);
1196 cli();
1197 outb(info->IER, 0);
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;
1205 save_flags(flags);
1206 cli();
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)) )
1221 return;
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;
1228 mxser_start(tty);
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;
1240 mxser_start(tty);
1243 /* Handle sw stopped */
1244 if ((old_termios->c_iflag & IXON) &&
1245 !(tty->termios->c_iflag & IXON)) {
1246 tty->stopped = 0;
1247 mxser_start(tty);
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;
1262 save_flags(flags);
1263 cli();
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;
1276 save_flags(flags);
1277 cli();
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);
1295 info->event = 0;
1296 info->count = 0;
1297 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1298 info->tty = 0;
1299 wake_up_interruptible(&info->open_wait);
1303 * This is the serial driver's generic interrupt routine
1305 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1307 int status, i;
1308 struct mxser_struct *info;
1309 struct mxser_struct *port;
1310 int max, irqbits, bits, msr;
1311 int pass_counter = 0;
1312 int handled = 0;
1314 port = 0;
1315 for (i = 0; i < MXSER_BOARDS; i++) {
1316 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1317 port = dev_id;
1318 break;
1322 if (i == MXSER_BOARDS)
1323 return IRQ_NONE;
1324 if (port == 0)
1325 return IRQ_NONE;
1326 max = mxser_numports[mxsercfg[i].board_type];
1328 while (1) {
1329 irqbits = inb(port->vector) & port->vectormask;
1330 if (irqbits == port->vectormask)
1331 break;
1332 handled = 1;
1333 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1334 if (irqbits == port->vectormask)
1335 break;
1336 if (bits & irqbits)
1337 continue;
1338 info = port + i;
1339 if (!info->tty ||
1340 (inb(info->base + UART_IIR) & UART_IIR_NO_INT))
1341 continue;
1342 status = inb(info->base + UART_LSR) & info->read_status_mask;
1343 if (status & UART_LSR_DR)
1344 mxser_receive_chars(info, &status);
1345 msr = inb(info->base + UART_MSR);
1346 if (msr & UART_MSR_ANY_DELTA)
1347 mxser_check_modem_status(info, msr);
1348 if (status & UART_LSR_THRE) {
1349 /* 8-2-99 by William
1350 if ( info->x_char || (info->xmit_cnt > 0) )
1352 mxser_transmit_chars(info);
1355 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
1356 #if 0
1357 printk("MOXA Smartio/Indusrtio family driver interrupt loop break\n");
1358 #endif
1359 break; /* Prevent infinite loops */
1362 return IRQ_RETVAL(handled);
1365 static inline void mxser_receive_chars(struct mxser_struct *info,
1366 int *status)
1368 struct tty_struct *tty = info->tty;
1369 unsigned char ch;
1370 int ignored = 0;
1371 int cnt = 0;
1373 do {
1374 ch = inb(info->base + UART_RX);
1375 if (*status & info->ignore_status_mask) {
1376 if (++ignored > 100)
1377 break;
1378 } else {
1379 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1380 break;
1381 tty->flip.count++;
1382 if (*status & UART_LSR_SPECIAL) {
1383 if (*status & UART_LSR_BI) {
1384 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
1385 if (info->flags & ASYNC_SAK)
1386 do_SAK(tty);
1387 } else if (*status & UART_LSR_PE) {
1388 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
1389 } else if (*status & UART_LSR_FE) {
1390 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
1391 } else if (*status & UART_LSR_OE) {
1392 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
1393 } else
1394 *tty->flip.flag_buf_ptr++ = 0;
1395 } else
1396 *tty->flip.flag_buf_ptr++ = 0;
1397 *tty->flip.char_buf_ptr++ = ch;
1398 cnt++;
1400 *status = inb(info->base + UART_LSR) & info->read_status_mask;
1401 } while (*status & UART_LSR_DR);
1402 mxvar_log.rxcnt[info->port] += cnt;
1403 schedule_delayed_work(&tty->flip.work, 1);
1407 static inline void mxser_transmit_chars(struct mxser_struct *info)
1409 int count, cnt;
1411 if (info->x_char) {
1412 outb(info->x_char, info->base + UART_TX);
1413 info->x_char = 0;
1414 mxvar_log.txcnt[info->port]++;
1415 return;
1417 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
1418 info->tty->hw_stopped) {
1419 info->IER &= ~UART_IER_THRI;
1420 outb(info->IER, info->base + UART_IER);
1421 return;
1423 cnt = info->xmit_cnt;
1424 count = info->xmit_fifo_size;
1425 do {
1426 outb(info->xmit_buf[info->xmit_tail++], info->base + UART_TX);
1427 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1428 if (--info->xmit_cnt <= 0)
1429 break;
1430 } while (--count > 0);
1431 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
1433 if (info->xmit_cnt < WAKEUP_CHARS) {
1434 set_bit(MXSER_EVENT_TXLOW, &info->event);
1435 schedule_work(&info->tqueue);
1437 if (info->xmit_cnt <= 0) {
1438 info->IER &= ~UART_IER_THRI;
1439 outb(info->IER, info->base + UART_IER);
1443 static inline void mxser_check_modem_status(struct mxser_struct *info,
1444 int status)
1447 /* update input line counters */
1448 if (status & UART_MSR_TERI)
1449 info->icount.rng++;
1450 if (status & UART_MSR_DDSR)
1451 info->icount.dsr++;
1452 if (status & UART_MSR_DDCD)
1453 info->icount.dcd++;
1454 if (status & UART_MSR_DCTS)
1455 info->icount.cts++;
1456 wake_up_interruptible(&info->delta_msr_wait);
1458 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
1459 if (status & UART_MSR_DCD)
1460 wake_up_interruptible(&info->open_wait);
1461 else
1462 set_bit(MXSER_EVENT_HANGUP, &info->event);
1463 schedule_work(&info->tqueue);
1465 if (info->flags & ASYNC_CTS_FLOW) {
1466 if (info->tty->hw_stopped) {
1467 if (status & UART_MSR_CTS) {
1468 info->tty->hw_stopped = 0;
1469 info->IER |= UART_IER_THRI;
1470 outb(info->IER, info->base + UART_IER);
1472 set_bit(MXSER_EVENT_TXLOW, &info->event);
1473 schedule_work(&info->tqueue);
1475 } else {
1476 if (!(status & UART_MSR_CTS)) {
1477 info->tty->hw_stopped = 1;
1478 info->IER &= ~UART_IER_THRI;
1479 outb(info->IER, info->base + UART_IER);
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;
1490 int retval;
1491 int do_clocal = 0;
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)
1502 return (-EAGAIN);
1503 else
1504 return (-ERESTARTSYS);
1505 #else
1506 return (-EAGAIN);
1507 #endif
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;
1516 return (0);
1518 if (tty->termios->c_cflag & CLOCAL)
1519 do_clocal = 1;
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.
1528 retval = 0;
1529 add_wait_queue(&info->open_wait, &wait);
1530 save_flags(flags);
1531 cli();
1532 if (!tty_hung_up_p(filp))
1533 info->count--;
1534 restore_flags(flags);
1535 info->blocked_open++;
1536 while (1) {
1537 save_flags(flags);
1538 cli();
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)
1546 retval = -EAGAIN;
1547 else
1548 retval = -ERESTARTSYS;
1549 #else
1550 retval = -EAGAIN;
1551 #endif
1552 break;
1554 if (!(info->flags & ASYNC_CLOSING) &&
1555 (do_clocal || (inb(info->base + UART_MSR) & UART_MSR_DCD)))
1556 break;
1557 if (signal_pending(current)) {
1558 retval = -ERESTARTSYS;
1559 break;
1561 schedule();
1563 set_current_state(TASK_RUNNING);
1564 remove_wait_queue(&info->open_wait, &wait);
1565 if (!tty_hung_up_p(filp))
1566 info->count++;
1567 info->blocked_open--;
1568 if (retval)
1569 return (retval);
1570 info->flags |= ASYNC_NORMAL_ACTIVE;
1571 return (0);
1574 static int mxser_startup(struct mxser_struct *info)
1576 unsigned long flags;
1577 unsigned long page;
1579 page = get_zeroed_page(GFP_KERNEL);
1580 if (!page)
1581 return (-ENOMEM);
1583 save_flags(flags);
1584 cli();
1586 if (info->flags & ASYNC_INITIALIZED) {
1587 free_page(page);
1588 restore_flags(flags);
1589 return (0);
1591 if (!info->base || !info->type) {
1592 if (info->tty)
1593 set_bit(TTY_IO_ERROR, &info->tty->flags);
1594 free_page(page);
1595 restore_flags(flags);
1596 return (0);
1598 if (info->xmit_buf)
1599 free_page(page);
1600 else
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
1614 * here.
1616 if (inb(info->base + UART_LSR) == 0xff) {
1617 restore_flags(flags);
1618 if (capable(CAP_SYS_ADMIN)) {
1619 if (info->tty)
1620 set_bit(TTY_IO_ERROR, &info->tty->flags);
1621 return (0);
1622 } else
1623 return (-ENODEV);
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);
1654 if (info->tty)
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, 0);
1664 info->flags |= ASYNC_INITIALIZED;
1665 restore_flags(flags);
1666 return (0);
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))
1678 return;
1680 save_flags(flags);
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 = 0;
1696 info->IER = 0;
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);
1708 if (info->tty)
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)
1722 int quot = 0;
1723 unsigned cflag, cval, fcr;
1724 int i;
1725 int ret = 0;
1726 unsigned long flags;
1728 if (!info->tty || !info->tty->termios)
1729 return ret;
1730 cflag = info->tty->termios->c_cflag;
1731 if (!(info->base))
1732 return ret;
1734 #ifndef B921600
1735 #define B921600 (B460800 +1)
1736 #endif
1737 switch (cflag & (CBAUD | CBAUDEX)) {
1738 case B921600:
1739 i = 20;
1740 break;
1741 case B460800:
1742 i = 19;
1743 break;
1744 case B230400:
1745 i = 18;
1746 break;
1747 case B115200:
1748 i = 17;
1749 break;
1750 case B57600:
1751 i = 16;
1752 break;
1753 case B38400:
1754 i = 15;
1755 break;
1756 case B19200:
1757 i = 14;
1758 break;
1759 case B9600:
1760 i = 13;
1761 break;
1762 case B4800:
1763 i = 12;
1764 break;
1765 case B2400:
1766 i = 11;
1767 break;
1768 case B1800:
1769 i = 10;
1770 break;
1771 case B1200:
1772 i = 9;
1773 break;
1774 case B600:
1775 i = 8;
1776 break;
1777 case B300:
1778 i = 7;
1779 break;
1780 case B200:
1781 i = 6;
1782 break;
1783 case B150:
1784 i = 5;
1785 break;
1786 case B134:
1787 i = 4;
1788 break;
1789 case B110:
1790 i = 3;
1791 break;
1792 case B75:
1793 i = 2;
1794 break;
1795 case B50:
1796 i = 1;
1797 break;
1798 default:
1799 i = 0;
1800 break;
1803 if (i == 15) {
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)
1811 i = 18;
1812 #endif
1814 #ifdef ASYNC_SPD_WARP
1815 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1816 i = 19;
1817 #endif
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) {
1824 /* re-calculate */
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)) {
1828 case B921600:
1829 i = 20;
1830 break;
1831 case B460800:
1832 i = 19;
1833 break;
1834 case B230400:
1835 i = 18;
1836 break;
1837 case B115200:
1838 i = 17;
1839 break;
1840 case B57600:
1841 i = 16;
1842 break;
1843 case B38400:
1844 i = 15;
1845 break;
1846 case B19200:
1847 i = 14;
1848 break;
1849 case B9600:
1850 i = 13;
1851 break;
1852 case B4800:
1853 i = 12;
1854 break;
1855 case B2400:
1856 i = 11;
1857 break;
1858 case B1800:
1859 i = 10;
1860 break;
1861 case B1200:
1862 i = 9;
1863 break;
1864 case B600:
1865 i = 8;
1866 break;
1867 case B300:
1868 i = 7;
1869 break;
1870 case B200:
1871 i = 6;
1872 break;
1873 case B150:
1874 i = 5;
1875 break;
1876 case B134:
1877 i = 4;
1878 break;
1879 case B110:
1880 i = 3;
1881 break;
1882 case B75:
1883 i = 2;
1884 break;
1885 case B50:
1886 i = 1;
1887 break;
1888 default:
1889 i = 0;
1890 break;
1892 if (i == 15) {
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)
1899 i = 18;
1900 #endif
1901 #ifdef ASYNC_SPD_WARP
1902 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1903 i = 19;
1904 #endif
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];
1910 if (quot == 0)
1911 quot = 1;
1912 } else {
1913 quot = 0;
1915 } else if (quot == 0)
1916 quot = 1;
1917 } else {
1918 quot = 0;
1921 if (quot) {
1922 info->MCR |= UART_MCR_DTR;
1923 save_flags(flags);
1924 cli();
1925 outb(info->MCR, info->base + UART_MCR);
1926 restore_flags(flags);
1927 } else {
1928 info->MCR &= ~UART_MCR_DTR;
1929 save_flags(flags);
1930 cli();
1931 outb(info->MCR, info->base + UART_MCR);
1932 restore_flags(flags);
1933 return ret;
1935 /* byte size and parity */
1936 switch (cflag & CSIZE) {
1937 case CS5:
1938 cval = 0x00;
1939 break;
1940 case CS6:
1941 cval = 0x01;
1942 break;
1943 case CS7:
1944 cval = 0x02;
1945 break;
1946 case CS8:
1947 cval = 0x03;
1948 break;
1949 default:
1950 cval = 0x00;
1951 break; /* too keep GCC shut... */
1953 if (cflag & CSTOPB)
1954 cval |= 0x04;
1955 if (cflag & PARENB)
1956 cval |= UART_LCR_PARITY;
1957 if (!(cflag & PARODD))
1958 cval |= UART_LCR_EPAR;
1959 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
1960 fcr = 0;
1961 } else {
1962 fcr = UART_FCR_ENABLE_FIFO;
1963 switch (info->rx_trigger) {
1964 case 1:
1965 fcr |= UART_FCR_TRIGGER_1;
1966 break;
1967 case 4:
1968 fcr |= UART_FCR_TRIGGER_4;
1969 break;
1970 case 8:
1971 fcr |= UART_FCR_TRIGGER_8;
1972 break;
1973 default:
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;
1986 } else {
1987 info->flags &= ~ASYNC_CTS_FLOW;
1989 outb(info->MCR, info->base + UART_MCR);
1990 if (cflag & CLOCAL)
1991 info->flags &= ~ASYNC_CHECK_CD;
1992 else {
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;
2008 #if 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;
2014 #endif
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;
2027 save_flags(flags);
2028 cli();
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);
2036 return ret;
2040 * ------------------------------------------------------------
2041 * friends of mxser_ioctl()
2042 * ------------------------------------------------------------
2044 static int mxser_get_serial_info(struct mxser_struct *info,
2045 struct serial_struct *retinfo)
2047 struct serial_struct tmp;
2049 if (!retinfo)
2050 return (-EFAULT);
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;
2061 tmp.hub6 = 0;
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 *new_info)
2068 struct serial_struct new_serial;
2069 unsigned int flags;
2070 int retval = 0;
2072 if (!new_info || !info->base)
2073 return (-EFAULT);
2074 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2075 return -EFAULT;
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))
2082 return (-EPERM);
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)))
2091 return (-EPERM);
2092 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2093 (new_serial.flags & ASYNC_USR_MASK));
2094 } else {
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, 0);
2109 } else
2110 retval = mxser_startup(info);
2111 return (retval);
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 *value)
2126 unsigned char status;
2127 unsigned int result;
2128 unsigned long flags;
2130 save_flags(flags);
2131 cli();
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;
2144 if (!info->base)
2145 return;
2146 set_current_state(TASK_INTERRUPTIBLE);
2147 save_flags(flags);
2148 cli();
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_get_modem_info(struct mxser_struct *info,
2156 unsigned int *value)
2158 unsigned char control, status;
2159 unsigned int result;
2160 unsigned long flags;
2162 control = info->MCR;
2163 save_flags(flags);
2164 cli();
2165 status = inb(info->base + UART_MSR);
2166 if (status & UART_MSR_ANY_DELTA)
2167 mxser_check_modem_status(info, status);
2168 restore_flags(flags);
2169 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2170 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2171 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2172 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2173 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2174 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2175 return put_user(result, value);
2178 static int mxser_set_modem_info(struct mxser_struct *info, unsigned int cmd,
2179 unsigned int *value)
2181 unsigned int arg;
2182 unsigned long flags;
2184 if(get_user(arg, value))
2185 return -EFAULT;
2186 switch (cmd) {
2187 case TIOCMBIS:
2188 if (arg & TIOCM_RTS)
2189 info->MCR |= UART_MCR_RTS;
2190 if (arg & TIOCM_DTR)
2191 info->MCR |= UART_MCR_DTR;
2192 break;
2193 case TIOCMBIC:
2194 if (arg & TIOCM_RTS)
2195 info->MCR &= ~UART_MCR_RTS;
2196 if (arg & TIOCM_DTR)
2197 info->MCR &= ~UART_MCR_DTR;
2198 break;
2199 case TIOCMSET:
2200 info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR)) |
2201 ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0) |
2202 ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
2203 break;
2204 default:
2205 return (-EINVAL);
2207 save_flags(flags);
2208 cli();
2209 outb(info->MCR, info->base + UART_MCR);
2210 restore_flags(flags);
2211 return (0);
2214 static int mxser_read_register(int, unsigned short *);
2215 static int mxser_program_mode(int);
2216 static void mxser_normal_mode(int);
2218 static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2220 int id, i, bits;
2221 unsigned short regs[16], irq;
2222 unsigned char scratch, scratch2;
2224 id = mxser_read_register(cap, regs);
2225 if (id == C168_ASIC_ID)
2226 hwconf->board_type = MXSER_BOARD_C168_ISA;
2227 else if (id == C104_ASIC_ID)
2228 hwconf->board_type = MXSER_BOARD_C104_ISA;
2229 else if (id == C102_ASIC_ID)
2230 hwconf->board_type = MXSER_BOARD_C102_ISA;
2231 else if (id == CI132_ASIC_ID)
2232 hwconf->board_type = MXSER_BOARD_CI132;
2233 else if (id == CI134_ASIC_ID)
2234 hwconf->board_type = MXSER_BOARD_CI134;
2235 else if (id == CI104J_ASIC_ID)
2236 hwconf->board_type = MXSER_BOARD_CI104J;
2237 else
2238 return (0);
2239 irq = regs[9] & 0x0F;
2240 irq = irq | (irq << 4);
2241 irq = irq | (irq << 8);
2242 if ((irq != regs[9]) || ((id == 1) && (irq != regs[10]))) {
2243 return (MXSER_ERR_IRQ_CONFLIT);
2245 if (!irq) {
2246 return (MXSER_ERR_IRQ);
2248 for (i = 0; i < 8; i++)
2249 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
2250 hwconf->irq = (int) (irq & 0x0F);
2251 if ((regs[12] & 0x80) == 0) {
2252 return (MXSER_ERR_VECTOR);
2254 hwconf->vector = (int) regs[11]; /* interrupt vector */
2255 if (id == 1)
2256 hwconf->vector_mask = 0x00FF;
2257 else
2258 hwconf->vector_mask = 0x000F;
2259 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2260 if (regs[12] & bits)
2261 hwconf->baud_base[i] = 921600;
2262 else
2263 hwconf->baud_base[i] = 115200;
2265 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2266 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2267 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2268 outb(scratch2, cap + UART_LCR);
2269 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2270 scratch = inb(cap + UART_IIR);
2271 if (scratch & 0xC0)
2272 hwconf->uart_type = PORT_16550A;
2273 else
2274 hwconf->uart_type = PORT_16450;
2275 if (id == 1)
2276 hwconf->ports = 8;
2277 else
2278 hwconf->ports = 4;
2279 return (hwconf->ports);
2282 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
2283 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
2284 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
2285 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
2286 #define EN_CCMD 0x000 /* Chip's command register */
2287 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
2288 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
2289 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
2290 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
2291 #define EN0_DCFG 0x00E /* Data configuration reg WR */
2292 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
2293 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
2294 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
2295 static int mxser_read_register(int port, unsigned short *regs)
2297 int i, k, value, id;
2298 unsigned int j;
2300 id = mxser_program_mode(port);
2301 if (id < 0)
2302 return (id);
2303 for (i = 0; i < 14; i++) {
2304 k = (i & 0x3F) | 0x180;
2305 for (j = 0x100; j > 0; j >>= 1) {
2306 outb(CHIP_CS, port);
2307 if (k & j) {
2308 outb(CHIP_CS | CHIP_DO, port);
2309 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
2310 } else {
2311 outb(CHIP_CS, port);
2312 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
2315 (void) inb(port);
2316 value = 0;
2317 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
2318 outb(CHIP_CS, port);
2319 outb(CHIP_CS | CHIP_SK, port);
2320 if (inb(port) & CHIP_DI)
2321 value |= j;
2323 regs[i] = value;
2324 outb(0, port);
2326 mxser_normal_mode(port);
2327 return (id);
2330 static int mxser_program_mode(int port)
2332 int id, i, j, n;
2333 unsigned long flags;
2335 save_flags(flags);
2336 cli();
2337 outb(0, port);
2338 outb(0, port);
2339 outb(0, port);
2340 (void) inb(port);
2341 (void) inb(port);
2342 outb(0, port);
2343 (void) inb(port);
2344 restore_flags(flags);
2345 id = inb(port + 1) & 0x1F;
2346 if ((id != C168_ASIC_ID) && (id != C104_ASIC_ID) && (id != CI104J_ASIC_ID) &&
2347 (id != C102_ASIC_ID) && (id != CI132_ASIC_ID) && (id != CI134_ASIC_ID))
2348 return (-1);
2349 for (i = 0, j = 0; i < 4; i++) {
2350 n = inb(port + 2);
2351 if (n == 'M') {
2352 j = 1;
2353 } else if ((j == 1) && (n == 1)) {
2354 j = 2;
2355 break;
2356 } else
2357 j = 0;
2359 if (j != 2)
2360 id = -2;
2361 return (id);
2364 static void mxser_normal_mode(int port)
2366 int i, n;
2368 outb(0xA5, port + 1);
2369 outb(0x80, port + 3);
2370 outb(12, port + 0); /* 9600 bps */
2371 outb(0, port + 1);
2372 outb(0x03, port + 3); /* 8 data bits */
2373 outb(0x13, port + 4); /* loop back mode */
2374 for (i = 0; i < 16; i++) {
2375 n = inb(port + 5);
2376 if ((n & 0x61) == 0x60)
2377 break;
2378 if ((n & 1) == 1)
2379 (void) inb(port);
2381 outb(0x00, port + 4);
2384 module_init(mxser_module_init);
2385 module_exit(mxser_module_exit);