mxser/mxser_new: first pass over termios reporting for the mxser cards
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / mxser_new.c
blobf74734b3407b3d331539e85ed3685d112ea23486
1 /*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
4 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com.tw).
5 * Copyright (C) 2006-2007 Jiri Slaby <jirislaby@gmail.com>
7 * This code is loosely based on the 1.8 moxa driver which is based on
8 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
9 * others.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
17 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
18 * - Fixed x86_64 cleanness
19 * - Fixed sleep with spinlock held in mxser_send_break
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/interrupt.h>
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
30 #include <linux/serial.h>
31 #include <linux/serial_reg.h>
32 #include <linux/major.h>
33 #include <linux/string.h>
34 #include <linux/fcntl.h>
35 #include <linux/ptrace.h>
36 #include <linux/gfp.h>
37 #include <linux/ioport.h>
38 #include <linux/mm.h>
39 #include <linux/delay.h>
40 #include <linux/pci.h>
41 #include <linux/bitops.h>
43 #include <asm/system.h>
44 #include <asm/io.h>
45 #include <asm/irq.h>
46 #include <asm/uaccess.h>
48 #include "mxser_new.h"
50 #define MXSER_VERSION "2.0.2" /* 1.10 */
51 #define MXSERMAJOR 174
52 #define MXSERCUMAJOR 175
54 #define MXSER_BOARDS 4 /* Max. boards */
55 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
56 #define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
57 #define MXSER_ISR_PASS_LIMIT 100
59 #define MXSER_ERR_IOADDR -1
60 #define MXSER_ERR_IRQ -2
61 #define MXSER_ERR_IRQ_CONFLIT -3
62 #define MXSER_ERR_VECTOR -4
64 /*CheckIsMoxaMust return value*/
65 #define MOXA_OTHER_UART 0x00
66 #define MOXA_MUST_MU150_HWID 0x01
67 #define MOXA_MUST_MU860_HWID 0x02
69 #define WAKEUP_CHARS 256
71 #define UART_MCR_AFE 0x20
72 #define UART_LSR_SPECIAL 0x1E
74 #define PCI_DEVICE_ID_CB108 0x1080
75 #define PCI_DEVICE_ID_CB114 0x1142
76 #define PCI_DEVICE_ID_CB134I 0x1341
77 #define PCI_DEVICE_ID_CP138U 0x1380
78 #define PCI_DEVICE_ID_POS104UL 0x1044
81 #define C168_ASIC_ID 1
82 #define C104_ASIC_ID 2
83 #define C102_ASIC_ID 0xB
84 #define CI132_ASIC_ID 4
85 #define CI134_ASIC_ID 3
86 #define CI104J_ASIC_ID 5
88 #define MXSER_HIGHBAUD 1
89 #define MXSER_HAS2 2
91 /* This is only for PCI */
92 static const struct {
93 int type;
94 int tx_fifo;
95 int rx_fifo;
96 int xmit_fifo_size;
97 int rx_high_water;
98 int rx_trigger;
99 int rx_low_water;
100 long max_baud;
101 } Gpci_uart_info[] = {
102 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
103 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
104 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
106 #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
108 struct mxser_cardinfo {
109 unsigned int nports;
110 char *name;
111 unsigned int flags;
114 static const struct mxser_cardinfo mxser_cards[] = {
115 /* 0*/ { 8, "C168 series", },
116 { 4, "C104 series", },
117 { 4, "CI-104J series", },
118 { 8, "C168H/PCI series", },
119 { 4, "C104H/PCI series", },
120 /* 5*/ { 4, "C102 series", MXSER_HAS2 }, /* C102-ISA */
121 { 4, "CI-132 series", MXSER_HAS2 },
122 { 4, "CI-134 series", },
123 { 2, "CP-132 series", },
124 { 4, "CP-114 series", },
125 /*10*/ { 4, "CT-114 series", },
126 { 2, "CP-102 series", MXSER_HIGHBAUD },
127 { 4, "CP-104U series", },
128 { 8, "CP-168U series", },
129 { 2, "CP-132U series", },
130 /*15*/ { 4, "CP-134U series", },
131 { 4, "CP-104JU series", },
132 { 8, "Moxa UC7000 Serial", }, /* RC7000 */
133 { 8, "CP-118U series", },
134 { 2, "CP-102UL series", },
135 /*20*/ { 2, "CP-102U series", },
136 { 8, "CP-118EL series", },
137 { 8, "CP-168EL series", },
138 { 4, "CP-104EL series", },
139 { 8, "CB-108 series", },
140 /*25*/ { 4, "CB-114 series", },
141 { 4, "CB-134I series", },
142 { 8, "CP-138U series", },
143 { 4, "POS-104UL series", }
146 /* driver_data correspond to the lines in the structure above
147 see also ISA probe function before you change something */
148 static struct pci_device_id mxser_pcibrds[] = {
149 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
150 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
171 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
174 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
176 static int mxvar_baud_table[] = {
177 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
178 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
180 static unsigned int mxvar_baud_table1[] = {
181 0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400,
182 B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600
184 #define BAUD_TABLE_NO ARRAY_SIZE(mxvar_baud_table)
186 #define B_SPEC B2000000
188 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
189 static int ttymajor = MXSERMAJOR;
190 static int calloutmajor = MXSERCUMAJOR;
192 /* Variables for insmod */
194 MODULE_AUTHOR("Casper Yang");
195 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
196 module_param_array(ioaddr, int, NULL, 0);
197 module_param(ttymajor, int, 0);
198 MODULE_LICENSE("GPL");
200 struct mxser_log {
201 int tick;
202 unsigned long rxcnt[MXSER_PORTS];
203 unsigned long txcnt[MXSER_PORTS];
207 struct mxser_mon {
208 unsigned long rxcnt;
209 unsigned long txcnt;
210 unsigned long up_rxcnt;
211 unsigned long up_txcnt;
212 int modem_status;
213 unsigned char hold_reason;
216 struct mxser_mon_ext {
217 unsigned long rx_cnt[32];
218 unsigned long tx_cnt[32];
219 unsigned long up_rxcnt[32];
220 unsigned long up_txcnt[32];
221 int modem_status[32];
223 long baudrate[32];
224 int databits[32];
225 int stopbits[32];
226 int parity[32];
227 int flowctrl[32];
228 int fifo[32];
229 int iftype[32];
232 struct mxser_board;
234 struct mxser_port {
235 struct mxser_board *board;
236 struct tty_struct *tty;
238 unsigned long ioaddr;
239 unsigned long opmode_ioaddr;
240 int max_baud;
242 int rx_high_water;
243 int rx_trigger; /* Rx fifo trigger level */
244 int rx_low_water;
245 int baud_base; /* max. speed */
246 long realbaud;
247 int type; /* UART type */
248 int flags; /* defined in tty.h */
249 int speed;
251 int x_char; /* xon/xoff character */
252 int IER; /* Interrupt Enable Register */
253 int MCR; /* Modem control register */
255 unsigned char stop_rx;
256 unsigned char ldisc_stop_rx;
258 int custom_divisor;
259 int close_delay;
260 unsigned short closing_wait;
261 unsigned char err_shadow;
262 unsigned long event;
264 int count; /* # of fd on device */
265 int blocked_open; /* # of blocked opens */
266 struct async_icount icount; /* kernel counters for 4 input interrupts */
267 int timeout;
269 int read_status_mask;
270 int ignore_status_mask;
271 int xmit_fifo_size;
272 unsigned char *xmit_buf;
273 int xmit_head;
274 int xmit_tail;
275 int xmit_cnt;
277 struct ktermios normal_termios;
279 struct mxser_mon mon_data;
281 spinlock_t slock;
282 wait_queue_head_t open_wait;
283 wait_queue_head_t delta_msr_wait;
286 struct mxser_board {
287 unsigned int idx;
288 int irq;
289 const struct mxser_cardinfo *info;
290 unsigned long vector;
291 unsigned long vector_mask;
293 int chip_flag;
294 int uart_type;
296 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
299 struct mxser_mstatus {
300 tcflag_t cflag;
301 int cts;
302 int dsr;
303 int ri;
304 int dcd;
307 static struct mxser_mstatus GMStatus[MXSER_PORTS];
309 static int mxserBoardCAP[MXSER_BOARDS] = {
310 0, 0, 0, 0
311 /* 0x180, 0x280, 0x200, 0x320 */
314 static struct mxser_board mxser_boards[MXSER_BOARDS];
315 static struct tty_driver *mxvar_sdriver;
316 static struct mxser_log mxvar_log;
317 static int mxvar_diagflag;
318 static unsigned char mxser_msr[MXSER_PORTS + 1];
319 static struct mxser_mon_ext mon_data_ext;
320 static int mxser_set_baud_method[MXSER_PORTS + 1];
322 #ifdef CONFIG_PCI
323 static int __devinit CheckIsMoxaMust(unsigned long io)
325 u8 oldmcr, hwid;
326 int i;
328 outb(0, io + UART_LCR);
329 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
330 oldmcr = inb(io + UART_MCR);
331 outb(0, io + UART_MCR);
332 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
333 if ((hwid = inb(io + UART_MCR)) != 0) {
334 outb(oldmcr, io + UART_MCR);
335 return MOXA_OTHER_UART;
338 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
339 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
340 if (hwid == Gpci_uart_info[i].type)
341 return (int)hwid;
343 return MOXA_OTHER_UART;
345 #endif
347 static void process_txrx_fifo(struct mxser_port *info)
349 int i;
351 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
352 info->rx_trigger = 1;
353 info->rx_high_water = 1;
354 info->rx_low_water = 1;
355 info->xmit_fifo_size = 1;
356 } else
357 for (i = 0; i < UART_INFO_NUM; i++)
358 if (info->board->chip_flag == Gpci_uart_info[i].type) {
359 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
360 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
361 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
362 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
363 break;
367 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
369 unsigned char status = 0;
371 status = inb(baseaddr + UART_MSR);
373 mxser_msr[port] &= 0x0F;
374 mxser_msr[port] |= status;
375 status = mxser_msr[port];
376 if (mode)
377 mxser_msr[port] = 0;
379 return status;
382 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
383 struct mxser_port *port)
385 DECLARE_WAITQUEUE(wait, current);
386 int retval;
387 int do_clocal = 0;
388 unsigned long flags;
391 * If non-blocking mode is set, or the port is not enabled,
392 * then make the check up front and then exit.
394 if ((filp->f_flags & O_NONBLOCK) ||
395 test_bit(TTY_IO_ERROR, &tty->flags)) {
396 port->flags |= ASYNC_NORMAL_ACTIVE;
397 return 0;
400 if (tty->termios->c_cflag & CLOCAL)
401 do_clocal = 1;
404 * Block waiting for the carrier detect and the line to become
405 * free (i.e., not in use by the callout). While we are in
406 * this loop, port->count is dropped by one, so that
407 * mxser_close() knows when to free things. We restore it upon
408 * exit, either normal or abnormal.
410 retval = 0;
411 add_wait_queue(&port->open_wait, &wait);
413 spin_lock_irqsave(&port->slock, flags);
414 if (!tty_hung_up_p(filp))
415 port->count--;
416 spin_unlock_irqrestore(&port->slock, flags);
417 port->blocked_open++;
418 while (1) {
419 spin_lock_irqsave(&port->slock, flags);
420 outb(inb(port->ioaddr + UART_MCR) |
421 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
422 spin_unlock_irqrestore(&port->slock, flags);
423 set_current_state(TASK_INTERRUPTIBLE);
424 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
425 if (port->flags & ASYNC_HUP_NOTIFY)
426 retval = -EAGAIN;
427 else
428 retval = -ERESTARTSYS;
429 break;
431 if (!(port->flags & ASYNC_CLOSING) &&
432 (do_clocal ||
433 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
434 break;
435 if (signal_pending(current)) {
436 retval = -ERESTARTSYS;
437 break;
439 schedule();
441 set_current_state(TASK_RUNNING);
442 remove_wait_queue(&port->open_wait, &wait);
443 if (!tty_hung_up_p(filp))
444 port->count++;
445 port->blocked_open--;
446 if (retval)
447 return retval;
448 port->flags |= ASYNC_NORMAL_ACTIVE;
449 return 0;
452 static int mxser_set_baud(struct mxser_port *info, long newspd)
454 unsigned int i;
455 int quot = 0, baud;
456 unsigned char cval;
458 if (!info->tty || !info->tty->termios)
459 return -1;
461 if (!(info->ioaddr))
462 return -1;
464 if (newspd > info->max_baud)
465 return -1;
467 info->realbaud = newspd;
468 for (i = 0; i < BAUD_TABLE_NO; i++)
469 if (newspd == mxvar_baud_table[i])
470 break;
471 if (i == BAUD_TABLE_NO) {
472 quot = info->baud_base / info->speed;
473 if (info->speed <= 0 || info->speed > info->max_baud)
474 quot = 0;
475 } else {
476 if (newspd == 134) {
477 quot = (2 * info->baud_base / 269);
478 tty_encode_baud_rate(info->tty, 134, 134);
479 } else if (newspd) {
480 quot = info->baud_base / newspd;
481 if (quot == 0)
482 quot = 1;
483 baud = info->baud_base/quot;
484 tty_encode_baud_rate(info->tty, baud, baud);
485 } else {
486 quot = 0;
490 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
491 info->timeout += HZ / 50; /* Add .02 seconds of slop */
493 if (quot) {
494 info->MCR |= UART_MCR_DTR;
495 outb(info->MCR, info->ioaddr + UART_MCR);
496 } else {
497 info->MCR &= ~UART_MCR_DTR;
498 outb(info->MCR, info->ioaddr + UART_MCR);
499 return 0;
502 cval = inb(info->ioaddr + UART_LCR);
504 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
506 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
507 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
508 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
510 if (i == BAUD_TABLE_NO) {
511 quot = info->baud_base % info->speed;
512 quot *= 8;
513 if ((quot % info->speed) > (info->speed / 2)) {
514 quot /= info->speed;
515 quot++;
516 } else {
517 quot /= info->speed;
519 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, quot);
520 } else
521 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, 0);
523 return 0;
527 * This routine is called to set the UART divisor registers to match
528 * the specified baud rate for a serial port.
530 static int mxser_change_speed(struct mxser_port *info,
531 struct ktermios *old_termios)
533 unsigned cflag, cval, fcr;
534 int ret = 0;
535 unsigned char status;
536 long baud;
538 if (!info->tty || !info->tty->termios)
539 return ret;
540 cflag = info->tty->termios->c_cflag;
541 if (!(info->ioaddr))
542 return ret;
544 if (mxser_set_baud_method[info->tty->index] == 0) {
545 if ((cflag & CBAUD) == B_SPEC)
546 baud = info->speed;
547 else
548 baud = tty_get_baud_rate(info->tty);
549 mxser_set_baud(info, baud);
552 /* byte size and parity */
553 switch (cflag & CSIZE) {
554 case CS5:
555 cval = 0x00;
556 break;
557 case CS6:
558 cval = 0x01;
559 break;
560 case CS7:
561 cval = 0x02;
562 break;
563 case CS8:
564 cval = 0x03;
565 break;
566 default:
567 cval = 0x00;
568 break; /* too keep GCC shut... */
570 if (cflag & CSTOPB)
571 cval |= 0x04;
572 if (cflag & PARENB)
573 cval |= UART_LCR_PARITY;
574 if (!(cflag & PARODD))
575 cval |= UART_LCR_EPAR;
576 if (cflag & CMSPAR)
577 cval |= UART_LCR_SPAR;
579 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
580 if (info->board->chip_flag) {
581 fcr = UART_FCR_ENABLE_FIFO;
582 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
583 SET_MOXA_MUST_FIFO_VALUE(info);
584 } else
585 fcr = 0;
586 } else {
587 fcr = UART_FCR_ENABLE_FIFO;
588 if (info->board->chip_flag) {
589 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
590 SET_MOXA_MUST_FIFO_VALUE(info);
591 } else {
592 switch (info->rx_trigger) {
593 case 1:
594 fcr |= UART_FCR_TRIGGER_1;
595 break;
596 case 4:
597 fcr |= UART_FCR_TRIGGER_4;
598 break;
599 case 8:
600 fcr |= UART_FCR_TRIGGER_8;
601 break;
602 default:
603 fcr |= UART_FCR_TRIGGER_14;
604 break;
609 /* CTS flow control flag and modem status interrupts */
610 info->IER &= ~UART_IER_MSI;
611 info->MCR &= ~UART_MCR_AFE;
612 if (cflag & CRTSCTS) {
613 info->flags |= ASYNC_CTS_FLOW;
614 info->IER |= UART_IER_MSI;
615 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
616 info->MCR |= UART_MCR_AFE;
617 } else {
618 status = inb(info->ioaddr + UART_MSR);
619 if (info->tty->hw_stopped) {
620 if (status & UART_MSR_CTS) {
621 info->tty->hw_stopped = 0;
622 if (info->type != PORT_16550A &&
623 !info->board->chip_flag) {
624 outb(info->IER & ~UART_IER_THRI,
625 info->ioaddr +
626 UART_IER);
627 info->IER |= UART_IER_THRI;
628 outb(info->IER, info->ioaddr +
629 UART_IER);
631 tty_wakeup(info->tty);
633 } else {
634 if (!(status & UART_MSR_CTS)) {
635 info->tty->hw_stopped = 1;
636 if ((info->type != PORT_16550A) &&
637 (!info->board->chip_flag)) {
638 info->IER &= ~UART_IER_THRI;
639 outb(info->IER, info->ioaddr +
640 UART_IER);
645 } else {
646 info->flags &= ~ASYNC_CTS_FLOW;
648 outb(info->MCR, info->ioaddr + UART_MCR);
649 if (cflag & CLOCAL) {
650 info->flags &= ~ASYNC_CHECK_CD;
651 } else {
652 info->flags |= ASYNC_CHECK_CD;
653 info->IER |= UART_IER_MSI;
655 outb(info->IER, info->ioaddr + UART_IER);
658 * Set up parity check flag
660 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
661 if (I_INPCK(info->tty))
662 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
663 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
664 info->read_status_mask |= UART_LSR_BI;
666 info->ignore_status_mask = 0;
668 if (I_IGNBRK(info->tty)) {
669 info->ignore_status_mask |= UART_LSR_BI;
670 info->read_status_mask |= UART_LSR_BI;
672 * If we're ignore parity and break indicators, ignore
673 * overruns too. (For real raw support).
675 if (I_IGNPAR(info->tty)) {
676 info->ignore_status_mask |=
677 UART_LSR_OE |
678 UART_LSR_PE |
679 UART_LSR_FE;
680 info->read_status_mask |=
681 UART_LSR_OE |
682 UART_LSR_PE |
683 UART_LSR_FE;
686 if (info->board->chip_flag) {
687 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
688 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
689 if (I_IXON(info->tty)) {
690 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
691 } else {
692 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
694 if (I_IXOFF(info->tty)) {
695 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
696 } else {
697 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
702 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
703 outb(cval, info->ioaddr + UART_LCR);
705 return ret;
708 static void mxser_check_modem_status(struct mxser_port *port, int status)
710 /* update input line counters */
711 if (status & UART_MSR_TERI)
712 port->icount.rng++;
713 if (status & UART_MSR_DDSR)
714 port->icount.dsr++;
715 if (status & UART_MSR_DDCD)
716 port->icount.dcd++;
717 if (status & UART_MSR_DCTS)
718 port->icount.cts++;
719 port->mon_data.modem_status = status;
720 wake_up_interruptible(&port->delta_msr_wait);
722 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
723 if (status & UART_MSR_DCD)
724 wake_up_interruptible(&port->open_wait);
727 if (port->flags & ASYNC_CTS_FLOW) {
728 if (port->tty->hw_stopped) {
729 if (status & UART_MSR_CTS) {
730 port->tty->hw_stopped = 0;
732 if ((port->type != PORT_16550A) &&
733 (!port->board->chip_flag)) {
734 outb(port->IER & ~UART_IER_THRI,
735 port->ioaddr + UART_IER);
736 port->IER |= UART_IER_THRI;
737 outb(port->IER, port->ioaddr +
738 UART_IER);
740 tty_wakeup(port->tty);
742 } else {
743 if (!(status & UART_MSR_CTS)) {
744 port->tty->hw_stopped = 1;
745 if (port->type != PORT_16550A &&
746 !port->board->chip_flag) {
747 port->IER &= ~UART_IER_THRI;
748 outb(port->IER, port->ioaddr +
749 UART_IER);
756 static int mxser_startup(struct mxser_port *info)
758 unsigned long page;
759 unsigned long flags;
761 page = __get_free_page(GFP_KERNEL);
762 if (!page)
763 return -ENOMEM;
765 spin_lock_irqsave(&info->slock, flags);
767 if (info->flags & ASYNC_INITIALIZED) {
768 free_page(page);
769 spin_unlock_irqrestore(&info->slock, flags);
770 return 0;
773 if (!info->ioaddr || !info->type) {
774 if (info->tty)
775 set_bit(TTY_IO_ERROR, &info->tty->flags);
776 free_page(page);
777 spin_unlock_irqrestore(&info->slock, flags);
778 return 0;
780 if (info->xmit_buf)
781 free_page(page);
782 else
783 info->xmit_buf = (unsigned char *) page;
786 * Clear the FIFO buffers and disable them
787 * (they will be reenabled in mxser_change_speed())
789 if (info->board->chip_flag)
790 outb((UART_FCR_CLEAR_RCVR |
791 UART_FCR_CLEAR_XMIT |
792 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
793 else
794 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
795 info->ioaddr + UART_FCR);
798 * At this point there's no way the LSR could still be 0xFF;
799 * if it is, then bail out, because there's likely no UART
800 * here.
802 if (inb(info->ioaddr + UART_LSR) == 0xff) {
803 spin_unlock_irqrestore(&info->slock, flags);
804 if (capable(CAP_SYS_ADMIN)) {
805 if (info->tty)
806 set_bit(TTY_IO_ERROR, &info->tty->flags);
807 return 0;
808 } else
809 return -ENODEV;
813 * Clear the interrupt registers.
815 (void) inb(info->ioaddr + UART_LSR);
816 (void) inb(info->ioaddr + UART_RX);
817 (void) inb(info->ioaddr + UART_IIR);
818 (void) inb(info->ioaddr + UART_MSR);
821 * Now, initialize the UART
823 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
824 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
825 outb(info->MCR, info->ioaddr + UART_MCR);
828 * Finally, enable interrupts
830 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
832 if (info->board->chip_flag)
833 info->IER |= MOXA_MUST_IER_EGDAI;
834 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
837 * And clear the interrupt registers again for luck.
839 (void) inb(info->ioaddr + UART_LSR);
840 (void) inb(info->ioaddr + UART_RX);
841 (void) inb(info->ioaddr + UART_IIR);
842 (void) inb(info->ioaddr + UART_MSR);
844 if (info->tty)
845 clear_bit(TTY_IO_ERROR, &info->tty->flags);
846 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
849 * and set the speed of the serial port
851 mxser_change_speed(info, NULL);
852 info->flags |= ASYNC_INITIALIZED;
853 spin_unlock_irqrestore(&info->slock, flags);
855 return 0;
859 * This routine will shutdown a serial port; interrupts maybe disabled, and
860 * DTR is dropped if the hangup on close termio flag is on.
862 static void mxser_shutdown(struct mxser_port *info)
864 unsigned long flags;
866 if (!(info->flags & ASYNC_INITIALIZED))
867 return;
869 spin_lock_irqsave(&info->slock, flags);
872 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
873 * here so the queue might never be waken up
875 wake_up_interruptible(&info->delta_msr_wait);
878 * Free the IRQ, if necessary
880 if (info->xmit_buf) {
881 free_page((unsigned long) info->xmit_buf);
882 info->xmit_buf = NULL;
885 info->IER = 0;
886 outb(0x00, info->ioaddr + UART_IER);
888 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
889 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
890 outb(info->MCR, info->ioaddr + UART_MCR);
892 /* clear Rx/Tx FIFO's */
893 if (info->board->chip_flag)
894 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
895 MOXA_MUST_FCR_GDA_MODE_ENABLE,
896 info->ioaddr + UART_FCR);
897 else
898 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
899 info->ioaddr + UART_FCR);
901 /* read data port to reset things */
902 (void) inb(info->ioaddr + UART_RX);
904 if (info->tty)
905 set_bit(TTY_IO_ERROR, &info->tty->flags);
907 info->flags &= ~ASYNC_INITIALIZED;
909 if (info->board->chip_flag)
910 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
912 spin_unlock_irqrestore(&info->slock, flags);
916 * This routine is called whenever a serial port is opened. It
917 * enables interrupts for a serial port, linking in its async structure into
918 * the IRQ chain. It also performs the serial-specific
919 * initialization for the tty structure.
921 static int mxser_open(struct tty_struct *tty, struct file *filp)
923 struct mxser_port *info;
924 unsigned long flags;
925 int retval, line;
927 line = tty->index;
928 if (line == MXSER_PORTS)
929 return 0;
930 if (line < 0 || line > MXSER_PORTS)
931 return -ENODEV;
932 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
933 if (!info->ioaddr)
934 return -ENODEV;
936 tty->driver_data = info;
937 info->tty = tty;
939 * Start up serial port
941 spin_lock_irqsave(&info->slock, flags);
942 info->count++;
943 spin_unlock_irqrestore(&info->slock, flags);
944 retval = mxser_startup(info);
945 if (retval)
946 return retval;
948 retval = mxser_block_til_ready(tty, filp, info);
949 if (retval)
950 return retval;
952 /* unmark here for very high baud rate (ex. 921600 bps) used */
953 tty->low_latency = 1;
954 return 0;
958 * This routine is called when the serial port gets closed. First, we
959 * wait for the last remaining data to be sent. Then, we unlink its
960 * async structure from the interrupt chain if necessary, and we free
961 * that IRQ if nothing is left in the chain.
963 static void mxser_close(struct tty_struct *tty, struct file *filp)
965 struct mxser_port *info = tty->driver_data;
967 unsigned long timeout;
968 unsigned long flags;
970 if (tty->index == MXSER_PORTS)
971 return;
972 if (!info)
973 return;
975 spin_lock_irqsave(&info->slock, flags);
977 if (tty_hung_up_p(filp)) {
978 spin_unlock_irqrestore(&info->slock, flags);
979 return;
981 if ((tty->count == 1) && (info->count != 1)) {
983 * Uh, oh. tty->count is 1, which means that the tty
984 * structure will be freed. Info->count should always
985 * be one in these conditions. If it's greater than
986 * one, we've got real problems, since it means the
987 * serial port won't be shutdown.
989 printk(KERN_ERR "mxser_close: bad serial port count; "
990 "tty->count is 1, info->count is %d\n", info->count);
991 info->count = 1;
993 if (--info->count < 0) {
994 printk(KERN_ERR "mxser_close: bad serial port count for "
995 "ttys%d: %d\n", tty->index, info->count);
996 info->count = 0;
998 if (info->count) {
999 spin_unlock_irqrestore(&info->slock, flags);
1000 return;
1002 info->flags |= ASYNC_CLOSING;
1003 spin_unlock_irqrestore(&info->slock, flags);
1005 * Save the termios structure, since this port may have
1006 * separate termios for callout and dialin.
1008 if (info->flags & ASYNC_NORMAL_ACTIVE)
1009 info->normal_termios = *tty->termios;
1011 * Now we wait for the transmit buffer to clear; and we notify
1012 * the line discipline to only process XON/XOFF characters.
1014 tty->closing = 1;
1015 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1016 tty_wait_until_sent(tty, info->closing_wait);
1018 * At this point we stop accepting input. To do this, we
1019 * disable the receive line status interrupts, and tell the
1020 * interrupt driver to stop checking the data ready bit in the
1021 * line status register.
1023 info->IER &= ~UART_IER_RLSI;
1024 if (info->board->chip_flag)
1025 info->IER &= ~MOXA_MUST_RECV_ISR;
1027 if (info->flags & ASYNC_INITIALIZED) {
1028 outb(info->IER, info->ioaddr + UART_IER);
1030 * Before we drop DTR, make sure the UART transmitter
1031 * has completely drained; this is especially
1032 * important if there is a transmit FIFO!
1034 timeout = jiffies + HZ;
1035 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1036 schedule_timeout_interruptible(5);
1037 if (time_after(jiffies, timeout))
1038 break;
1041 mxser_shutdown(info);
1043 if (tty->driver->flush_buffer)
1044 tty->driver->flush_buffer(tty);
1046 tty_ldisc_flush(tty);
1048 tty->closing = 0;
1049 info->event = 0;
1050 info->tty = NULL;
1051 if (info->blocked_open) {
1052 if (info->close_delay)
1053 schedule_timeout_interruptible(info->close_delay);
1054 wake_up_interruptible(&info->open_wait);
1057 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1060 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1062 int c, total = 0;
1063 struct mxser_port *info = tty->driver_data;
1064 unsigned long flags;
1066 if (!info->xmit_buf)
1067 return 0;
1069 while (1) {
1070 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1071 SERIAL_XMIT_SIZE - info->xmit_head));
1072 if (c <= 0)
1073 break;
1075 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1076 spin_lock_irqsave(&info->slock, flags);
1077 info->xmit_head = (info->xmit_head + c) &
1078 (SERIAL_XMIT_SIZE - 1);
1079 info->xmit_cnt += c;
1080 spin_unlock_irqrestore(&info->slock, flags);
1082 buf += c;
1083 count -= c;
1084 total += c;
1087 if (info->xmit_cnt && !tty->stopped) {
1088 if (!tty->hw_stopped ||
1089 (info->type == PORT_16550A) ||
1090 (info->board->chip_flag)) {
1091 spin_lock_irqsave(&info->slock, flags);
1092 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1093 UART_IER);
1094 info->IER |= UART_IER_THRI;
1095 outb(info->IER, info->ioaddr + UART_IER);
1096 spin_unlock_irqrestore(&info->slock, flags);
1099 return total;
1102 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1104 struct mxser_port *info = tty->driver_data;
1105 unsigned long flags;
1107 if (!info->xmit_buf)
1108 return;
1110 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1111 return;
1113 spin_lock_irqsave(&info->slock, flags);
1114 info->xmit_buf[info->xmit_head++] = ch;
1115 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1116 info->xmit_cnt++;
1117 spin_unlock_irqrestore(&info->slock, flags);
1118 if (!tty->stopped) {
1119 if (!tty->hw_stopped ||
1120 (info->type == PORT_16550A) ||
1121 info->board->chip_flag) {
1122 spin_lock_irqsave(&info->slock, flags);
1123 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1124 info->IER |= UART_IER_THRI;
1125 outb(info->IER, info->ioaddr + UART_IER);
1126 spin_unlock_irqrestore(&info->slock, flags);
1132 static void mxser_flush_chars(struct tty_struct *tty)
1134 struct mxser_port *info = tty->driver_data;
1135 unsigned long flags;
1137 if (info->xmit_cnt <= 0 ||
1138 tty->stopped ||
1139 !info->xmit_buf ||
1140 (tty->hw_stopped &&
1141 (info->type != PORT_16550A) &&
1142 (!info->board->chip_flag)
1144 return;
1146 spin_lock_irqsave(&info->slock, flags);
1148 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1149 info->IER |= UART_IER_THRI;
1150 outb(info->IER, info->ioaddr + UART_IER);
1152 spin_unlock_irqrestore(&info->slock, flags);
1155 static int mxser_write_room(struct tty_struct *tty)
1157 struct mxser_port *info = tty->driver_data;
1158 int ret;
1160 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1161 if (ret < 0)
1162 ret = 0;
1163 return ret;
1166 static int mxser_chars_in_buffer(struct tty_struct *tty)
1168 struct mxser_port *info = tty->driver_data;
1169 return info->xmit_cnt;
1172 static void mxser_flush_buffer(struct tty_struct *tty)
1174 struct mxser_port *info = tty->driver_data;
1175 char fcr;
1176 unsigned long flags;
1179 spin_lock_irqsave(&info->slock, flags);
1180 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1182 fcr = inb(info->ioaddr + UART_FCR);
1183 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1184 info->ioaddr + UART_FCR);
1185 outb(fcr, info->ioaddr + UART_FCR);
1187 spin_unlock_irqrestore(&info->slock, flags);
1189 tty_wakeup(tty);
1193 * ------------------------------------------------------------
1194 * friends of mxser_ioctl()
1195 * ------------------------------------------------------------
1197 static int mxser_get_serial_info(struct mxser_port *info,
1198 struct serial_struct __user *retinfo)
1200 struct serial_struct tmp;
1202 if (!retinfo)
1203 return -EFAULT;
1204 memset(&tmp, 0, sizeof(tmp));
1205 tmp.type = info->type;
1206 tmp.line = info->tty->index;
1207 tmp.port = info->ioaddr;
1208 tmp.irq = info->board->irq;
1209 tmp.flags = info->flags;
1210 tmp.baud_base = info->baud_base;
1211 tmp.close_delay = info->close_delay;
1212 tmp.closing_wait = info->closing_wait;
1213 tmp.custom_divisor = info->custom_divisor;
1214 tmp.hub6 = 0;
1215 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1216 return -EFAULT;
1217 return 0;
1220 static int mxser_set_serial_info(struct mxser_port *info,
1221 struct serial_struct __user *new_info)
1223 struct serial_struct new_serial;
1224 unsigned long sl_flags;
1225 unsigned int flags;
1226 int retval = 0;
1228 if (!new_info || !info->ioaddr)
1229 return -EFAULT;
1230 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1231 return -EFAULT;
1233 if ((new_serial.irq != info->board->irq) ||
1234 (new_serial.port != info->ioaddr) ||
1235 (new_serial.custom_divisor != info->custom_divisor) ||
1236 (new_serial.baud_base != info->baud_base))
1237 return -EPERM;
1239 flags = info->flags & ASYNC_SPD_MASK;
1241 if (!capable(CAP_SYS_ADMIN)) {
1242 if ((new_serial.baud_base != info->baud_base) ||
1243 (new_serial.close_delay != info->close_delay) ||
1244 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1245 return -EPERM;
1246 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1247 (new_serial.flags & ASYNC_USR_MASK));
1248 } else {
1250 * OK, past this point, all the error checking has been done.
1251 * At this point, we start making changes.....
1253 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1254 (new_serial.flags & ASYNC_FLAGS));
1255 info->close_delay = new_serial.close_delay * HZ / 100;
1256 info->closing_wait = new_serial.closing_wait * HZ / 100;
1257 info->tty->low_latency =
1258 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1259 info->tty->low_latency = 0;
1262 info->type = new_serial.type;
1264 process_txrx_fifo(info);
1266 if (info->flags & ASYNC_INITIALIZED) {
1267 if (flags != (info->flags & ASYNC_SPD_MASK)) {
1268 spin_lock_irqsave(&info->slock, sl_flags);
1269 mxser_change_speed(info, NULL);
1270 spin_unlock_irqrestore(&info->slock, sl_flags);
1272 } else
1273 retval = mxser_startup(info);
1275 return retval;
1279 * mxser_get_lsr_info - get line status register info
1281 * Purpose: Let user call ioctl() to get info when the UART physically
1282 * is emptied. On bus types like RS485, the transmitter must
1283 * release the bus after transmitting. This must be done when
1284 * the transmit shift register is empty, not be done when the
1285 * transmit holding register is empty. This functionality
1286 * allows an RS485 driver to be written in user space.
1288 static int mxser_get_lsr_info(struct mxser_port *info,
1289 unsigned int __user *value)
1291 unsigned char status;
1292 unsigned int result;
1293 unsigned long flags;
1295 spin_lock_irqsave(&info->slock, flags);
1296 status = inb(info->ioaddr + UART_LSR);
1297 spin_unlock_irqrestore(&info->slock, flags);
1298 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1299 return put_user(result, value);
1303 * This routine sends a break character out the serial port.
1305 static void mxser_send_break(struct mxser_port *info, int duration)
1307 unsigned long flags;
1309 if (!info->ioaddr)
1310 return;
1311 set_current_state(TASK_INTERRUPTIBLE);
1312 spin_lock_irqsave(&info->slock, flags);
1313 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1314 info->ioaddr + UART_LCR);
1315 spin_unlock_irqrestore(&info->slock, flags);
1316 schedule_timeout(duration);
1317 spin_lock_irqsave(&info->slock, flags);
1318 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1319 info->ioaddr + UART_LCR);
1320 spin_unlock_irqrestore(&info->slock, flags);
1323 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1325 struct mxser_port *info = tty->driver_data;
1326 unsigned char control, status;
1327 unsigned long flags;
1330 if (tty->index == MXSER_PORTS)
1331 return -ENOIOCTLCMD;
1332 if (test_bit(TTY_IO_ERROR, &tty->flags))
1333 return -EIO;
1335 control = info->MCR;
1337 spin_lock_irqsave(&info->slock, flags);
1338 status = inb(info->ioaddr + UART_MSR);
1339 if (status & UART_MSR_ANY_DELTA)
1340 mxser_check_modem_status(info, status);
1341 spin_unlock_irqrestore(&info->slock, flags);
1342 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1343 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1344 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1345 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1346 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1347 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1350 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1351 unsigned int set, unsigned int clear)
1353 struct mxser_port *info = tty->driver_data;
1354 unsigned long flags;
1357 if (tty->index == MXSER_PORTS)
1358 return -ENOIOCTLCMD;
1359 if (test_bit(TTY_IO_ERROR, &tty->flags))
1360 return -EIO;
1362 spin_lock_irqsave(&info->slock, flags);
1364 if (set & TIOCM_RTS)
1365 info->MCR |= UART_MCR_RTS;
1366 if (set & TIOCM_DTR)
1367 info->MCR |= UART_MCR_DTR;
1369 if (clear & TIOCM_RTS)
1370 info->MCR &= ~UART_MCR_RTS;
1371 if (clear & TIOCM_DTR)
1372 info->MCR &= ~UART_MCR_DTR;
1374 outb(info->MCR, info->ioaddr + UART_MCR);
1375 spin_unlock_irqrestore(&info->slock, flags);
1376 return 0;
1379 static int __init mxser_program_mode(int port)
1381 int id, i, j, n;
1383 outb(0, port);
1384 outb(0, port);
1385 outb(0, port);
1386 (void)inb(port);
1387 (void)inb(port);
1388 outb(0, port);
1389 (void)inb(port);
1391 id = inb(port + 1) & 0x1F;
1392 if ((id != C168_ASIC_ID) &&
1393 (id != C104_ASIC_ID) &&
1394 (id != C102_ASIC_ID) &&
1395 (id != CI132_ASIC_ID) &&
1396 (id != CI134_ASIC_ID) &&
1397 (id != CI104J_ASIC_ID))
1398 return -1;
1399 for (i = 0, j = 0; i < 4; i++) {
1400 n = inb(port + 2);
1401 if (n == 'M') {
1402 j = 1;
1403 } else if ((j == 1) && (n == 1)) {
1404 j = 2;
1405 break;
1406 } else
1407 j = 0;
1409 if (j != 2)
1410 id = -2;
1411 return id;
1414 static void __init mxser_normal_mode(int port)
1416 int i, n;
1418 outb(0xA5, port + 1);
1419 outb(0x80, port + 3);
1420 outb(12, port + 0); /* 9600 bps */
1421 outb(0, port + 1);
1422 outb(0x03, port + 3); /* 8 data bits */
1423 outb(0x13, port + 4); /* loop back mode */
1424 for (i = 0; i < 16; i++) {
1425 n = inb(port + 5);
1426 if ((n & 0x61) == 0x60)
1427 break;
1428 if ((n & 1) == 1)
1429 (void)inb(port);
1431 outb(0x00, port + 4);
1434 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1435 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1436 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1437 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1438 #define EN_CCMD 0x000 /* Chip's command register */
1439 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1440 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1441 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1442 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1443 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1444 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1445 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1446 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1447 static int __init mxser_read_register(int port, unsigned short *regs)
1449 int i, k, value, id;
1450 unsigned int j;
1452 id = mxser_program_mode(port);
1453 if (id < 0)
1454 return id;
1455 for (i = 0; i < 14; i++) {
1456 k = (i & 0x3F) | 0x180;
1457 for (j = 0x100; j > 0; j >>= 1) {
1458 outb(CHIP_CS, port);
1459 if (k & j) {
1460 outb(CHIP_CS | CHIP_DO, port);
1461 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1462 } else {
1463 outb(CHIP_CS, port);
1464 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1467 (void)inb(port);
1468 value = 0;
1469 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1470 outb(CHIP_CS, port);
1471 outb(CHIP_CS | CHIP_SK, port);
1472 if (inb(port) & CHIP_DI)
1473 value |= j;
1475 regs[i] = value;
1476 outb(0, port);
1478 mxser_normal_mode(port);
1479 return id;
1482 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1484 struct mxser_port *port;
1485 int result, status;
1486 unsigned int i, j;
1488 switch (cmd) {
1489 case MOXA_GET_CONF:
1490 /* if (copy_to_user(argp, mxsercfg,
1491 sizeof(struct mxser_hwconf) * 4))
1492 return -EFAULT;
1493 return 0;*/
1494 return -ENXIO;
1495 case MOXA_GET_MAJOR:
1496 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1497 return -EFAULT;
1498 return 0;
1500 case MOXA_GET_CUMAJOR:
1501 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1502 return -EFAULT;
1503 return 0;
1505 case MOXA_CHKPORTENABLE:
1506 result = 0;
1508 for (i = 0; i < MXSER_BOARDS; i++)
1509 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1510 if (mxser_boards[i].ports[j].ioaddr)
1511 result |= (1 << i);
1513 return put_user(result, (unsigned long __user *)argp);
1514 case MOXA_GETDATACOUNT:
1515 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1516 return -EFAULT;
1517 return 0;
1518 case MOXA_GETMSTATUS:
1519 for (i = 0; i < MXSER_BOARDS; i++)
1520 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1521 port = &mxser_boards[i].ports[j];
1523 GMStatus[i].ri = 0;
1524 if (!port->ioaddr) {
1525 GMStatus[i].dcd = 0;
1526 GMStatus[i].dsr = 0;
1527 GMStatus[i].cts = 0;
1528 continue;
1531 if (!port->tty || !port->tty->termios)
1532 GMStatus[i].cflag =
1533 port->normal_termios.c_cflag;
1534 else
1535 GMStatus[i].cflag =
1536 port->tty->termios->c_cflag;
1538 status = inb(port->ioaddr + UART_MSR);
1539 if (status & 0x80 /*UART_MSR_DCD */ )
1540 GMStatus[i].dcd = 1;
1541 else
1542 GMStatus[i].dcd = 0;
1544 if (status & 0x20 /*UART_MSR_DSR */ )
1545 GMStatus[i].dsr = 1;
1546 else
1547 GMStatus[i].dsr = 0;
1550 if (status & 0x10 /*UART_MSR_CTS */ )
1551 GMStatus[i].cts = 1;
1552 else
1553 GMStatus[i].cts = 0;
1555 if (copy_to_user(argp, GMStatus,
1556 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1557 return -EFAULT;
1558 return 0;
1559 case MOXA_ASPP_MON_EXT: {
1560 int p, shiftbit;
1561 unsigned long opmode;
1562 unsigned cflag, iflag;
1564 for (i = 0; i < MXSER_BOARDS; i++)
1565 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1566 port = &mxser_boards[i].ports[j];
1567 if (!port->ioaddr)
1568 continue;
1570 status = mxser_get_msr(port->ioaddr, 0, i);
1572 if (status & UART_MSR_TERI)
1573 port->icount.rng++;
1574 if (status & UART_MSR_DDSR)
1575 port->icount.dsr++;
1576 if (status & UART_MSR_DDCD)
1577 port->icount.dcd++;
1578 if (status & UART_MSR_DCTS)
1579 port->icount.cts++;
1581 port->mon_data.modem_status = status;
1582 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1583 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1584 mon_data_ext.up_rxcnt[i] =
1585 port->mon_data.up_rxcnt;
1586 mon_data_ext.up_txcnt[i] =
1587 port->mon_data.up_txcnt;
1588 mon_data_ext.modem_status[i] =
1589 port->mon_data.modem_status;
1590 mon_data_ext.baudrate[i] = port->realbaud;
1592 if (!port->tty || !port->tty->termios) {
1593 cflag = port->normal_termios.c_cflag;
1594 iflag = port->normal_termios.c_iflag;
1595 } else {
1596 cflag = port->tty->termios->c_cflag;
1597 iflag = port->tty->termios->c_iflag;
1600 mon_data_ext.databits[i] = cflag & CSIZE;
1602 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1604 mon_data_ext.parity[i] =
1605 cflag & (PARENB | PARODD | CMSPAR);
1607 mon_data_ext.flowctrl[i] = 0x00;
1609 if (cflag & CRTSCTS)
1610 mon_data_ext.flowctrl[i] |= 0x03;
1612 if (iflag & (IXON | IXOFF))
1613 mon_data_ext.flowctrl[i] |= 0x0C;
1615 if (port->type == PORT_16550A)
1616 mon_data_ext.fifo[i] = 1;
1617 else
1618 mon_data_ext.fifo[i] = 0;
1620 p = i % 4;
1621 shiftbit = p * 2;
1622 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1623 opmode &= OP_MODE_MASK;
1625 mon_data_ext.iftype[i] = opmode;
1628 if (copy_to_user(argp, &mon_data_ext,
1629 sizeof(mon_data_ext)))
1630 return -EFAULT;
1632 return 0;
1634 } default:
1635 return -ENOIOCTLCMD;
1637 return 0;
1640 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1641 unsigned int cmd, unsigned long arg)
1643 struct mxser_port *info = tty->driver_data;
1644 struct async_icount cprev, cnow; /* kernel counter temps */
1645 struct serial_icounter_struct __user *p_cuser;
1646 unsigned long templ;
1647 unsigned long flags;
1648 unsigned int i;
1649 void __user *argp = (void __user *)arg;
1650 int retval;
1652 if (tty->index == MXSER_PORTS)
1653 return mxser_ioctl_special(cmd, argp);
1655 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1656 int p;
1657 unsigned long opmode;
1658 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1659 int shiftbit;
1660 unsigned char val, mask;
1662 p = tty->index % 4;
1663 if (cmd == MOXA_SET_OP_MODE) {
1664 if (get_user(opmode, (int __user *) argp))
1665 return -EFAULT;
1666 if (opmode != RS232_MODE &&
1667 opmode != RS485_2WIRE_MODE &&
1668 opmode != RS422_MODE &&
1669 opmode != RS485_4WIRE_MODE)
1670 return -EFAULT;
1671 mask = ModeMask[p];
1672 shiftbit = p * 2;
1673 val = inb(info->opmode_ioaddr);
1674 val &= mask;
1675 val |= (opmode << shiftbit);
1676 outb(val, info->opmode_ioaddr);
1677 } else {
1678 shiftbit = p * 2;
1679 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1680 opmode &= OP_MODE_MASK;
1681 if (copy_to_user(argp, &opmode, sizeof(int)))
1682 return -EFAULT;
1684 return 0;
1687 if (cmd == MOXA_SET_SPECIAL_BAUD_RATE) {
1688 int speed;
1690 if (get_user(speed, (int __user *)argp))
1691 return -EFAULT;
1692 if (speed <= 0 || speed > info->max_baud)
1693 return -EFAULT;
1694 if (!info->tty || !info->tty->termios || !info->ioaddr)
1695 return 0;
1696 info->tty->termios->c_cflag &= ~(CBAUD | CBAUDEX);
1697 for (i = 0; i < BAUD_TABLE_NO; i++)
1698 if (speed == mxvar_baud_table[i])
1699 break;
1700 if (i == BAUD_TABLE_NO) {
1701 info->tty->termios->c_cflag |= B_SPEC;
1702 } else if (speed != 0)
1703 info->tty->termios->c_cflag |= mxvar_baud_table1[i];
1705 info->speed = speed;
1706 spin_lock_irqsave(&info->slock, flags);
1707 mxser_change_speed(info, NULL);
1708 spin_unlock_irqrestore(&info->slock, flags);
1710 return 0;
1711 } else if (cmd == MOXA_GET_SPECIAL_BAUD_RATE) {
1712 if (copy_to_user(argp, &info->speed, sizeof(int)))
1713 return -EFAULT;
1714 return 0;
1717 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1718 test_bit(TTY_IO_ERROR, &tty->flags))
1719 return -EIO;
1721 switch (cmd) {
1722 case TCSBRK: /* SVID version: non-zero arg --> no break */
1723 retval = tty_check_change(tty);
1724 if (retval)
1725 return retval;
1726 tty_wait_until_sent(tty, 0);
1727 if (!arg)
1728 mxser_send_break(info, HZ / 4); /* 1/4 second */
1729 return 0;
1730 case TCSBRKP: /* support for POSIX tcsendbreak() */
1731 retval = tty_check_change(tty);
1732 if (retval)
1733 return retval;
1734 tty_wait_until_sent(tty, 0);
1735 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1736 return 0;
1737 case TIOCGSOFTCAR:
1738 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1739 case TIOCSSOFTCAR:
1740 if (get_user(templ, (unsigned long __user *) argp))
1741 return -EFAULT;
1742 arg = templ;
1743 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1744 return 0;
1745 case TIOCGSERIAL:
1746 return mxser_get_serial_info(info, argp);
1747 case TIOCSSERIAL:
1748 return mxser_set_serial_info(info, argp);
1749 case TIOCSERGETLSR: /* Get line status register */
1750 return mxser_get_lsr_info(info, argp);
1752 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1753 * - mask passed in arg for lines of interest
1754 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1755 * Caller should use TIOCGICOUNT to see which one it was
1757 case TIOCMIWAIT:
1758 spin_lock_irqsave(&info->slock, flags);
1759 cnow = info->icount; /* note the counters on entry */
1760 spin_unlock_irqrestore(&info->slock, flags);
1762 wait_event_interruptible(info->delta_msr_wait, ({
1763 cprev = cnow;
1764 spin_lock_irqsave(&info->slock, flags);
1765 cnow = info->icount; /* atomic copy */
1766 spin_unlock_irqrestore(&info->slock, flags);
1768 ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1769 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1770 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1771 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
1772 }));
1773 break;
1775 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1776 * Return: write counters to the user passed counter struct
1777 * NB: both 1->0 and 0->1 transitions are counted except for
1778 * RI where only 0->1 is counted.
1780 case TIOCGICOUNT:
1781 spin_lock_irqsave(&info->slock, flags);
1782 cnow = info->icount;
1783 spin_unlock_irqrestore(&info->slock, flags);
1784 p_cuser = argp;
1785 if (put_user(cnow.frame, &p_cuser->frame))
1786 return -EFAULT;
1787 if (put_user(cnow.brk, &p_cuser->brk))
1788 return -EFAULT;
1789 if (put_user(cnow.overrun, &p_cuser->overrun))
1790 return -EFAULT;
1791 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1792 return -EFAULT;
1793 if (put_user(cnow.parity, &p_cuser->parity))
1794 return -EFAULT;
1795 if (put_user(cnow.rx, &p_cuser->rx))
1796 return -EFAULT;
1797 if (put_user(cnow.tx, &p_cuser->tx))
1798 return -EFAULT;
1799 put_user(cnow.cts, &p_cuser->cts);
1800 put_user(cnow.dsr, &p_cuser->dsr);
1801 put_user(cnow.rng, &p_cuser->rng);
1802 put_user(cnow.dcd, &p_cuser->dcd);
1803 return 0;
1804 case MOXA_HighSpeedOn:
1805 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1806 case MOXA_SDS_RSTICOUNTER:
1807 info->mon_data.rxcnt = 0;
1808 info->mon_data.txcnt = 0;
1809 return 0;
1810 case MOXA_ASPP_SETBAUD:{
1811 long baud;
1812 if (get_user(baud, (long __user *)argp))
1813 return -EFAULT;
1814 spin_lock_irqsave(&info->slock, flags);
1815 mxser_set_baud(info, baud);
1816 spin_unlock_irqrestore(&info->slock, flags);
1817 return 0;
1819 case MOXA_ASPP_GETBAUD:
1820 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1821 return -EFAULT;
1823 return 0;
1825 case MOXA_ASPP_OQUEUE:{
1826 int len, lsr;
1828 len = mxser_chars_in_buffer(tty);
1830 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1832 len += (lsr ? 0 : 1);
1834 if (copy_to_user(argp, &len, sizeof(int)))
1835 return -EFAULT;
1837 return 0;
1839 case MOXA_ASPP_MON: {
1840 int mcr, status;
1842 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1843 mxser_check_modem_status(info, status);
1845 mcr = inb(info->ioaddr + UART_MCR);
1846 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1847 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1848 else
1849 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1851 if (mcr & MOXA_MUST_MCR_TX_XON)
1852 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1853 else
1854 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1856 if (info->tty->hw_stopped)
1857 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1858 else
1859 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1861 if (copy_to_user(argp, &info->mon_data,
1862 sizeof(struct mxser_mon)))
1863 return -EFAULT;
1865 return 0;
1867 case MOXA_ASPP_LSTATUS: {
1868 if (copy_to_user(argp, &info->err_shadow,
1869 sizeof(unsigned char)))
1870 return -EFAULT;
1872 info->err_shadow = 0;
1873 return 0;
1875 case MOXA_SET_BAUD_METHOD: {
1876 int method;
1878 if (get_user(method, (int __user *)argp))
1879 return -EFAULT;
1880 mxser_set_baud_method[tty->index] = method;
1881 if (copy_to_user(argp, &method, sizeof(int)))
1882 return -EFAULT;
1884 return 0;
1886 default:
1887 return -ENOIOCTLCMD;
1889 return 0;
1892 static void mxser_stoprx(struct tty_struct *tty)
1894 struct mxser_port *info = tty->driver_data;
1896 info->ldisc_stop_rx = 1;
1897 if (I_IXOFF(tty)) {
1898 if (info->board->chip_flag) {
1899 info->IER &= ~MOXA_MUST_RECV_ISR;
1900 outb(info->IER, info->ioaddr + UART_IER);
1901 } else {
1902 info->x_char = STOP_CHAR(tty);
1903 outb(0, info->ioaddr + UART_IER);
1904 info->IER |= UART_IER_THRI;
1905 outb(info->IER, info->ioaddr + UART_IER);
1909 if (info->tty->termios->c_cflag & CRTSCTS) {
1910 info->MCR &= ~UART_MCR_RTS;
1911 outb(info->MCR, info->ioaddr + UART_MCR);
1916 * This routine is called by the upper-layer tty layer to signal that
1917 * incoming characters should be throttled.
1919 static void mxser_throttle(struct tty_struct *tty)
1921 mxser_stoprx(tty);
1924 static void mxser_unthrottle(struct tty_struct *tty)
1926 struct mxser_port *info = tty->driver_data;
1928 /* startrx */
1929 info->ldisc_stop_rx = 0;
1930 if (I_IXOFF(tty)) {
1931 if (info->x_char)
1932 info->x_char = 0;
1933 else {
1934 if (info->board->chip_flag) {
1935 info->IER |= MOXA_MUST_RECV_ISR;
1936 outb(info->IER, info->ioaddr + UART_IER);
1937 } else {
1938 info->x_char = START_CHAR(tty);
1939 outb(0, info->ioaddr + UART_IER);
1940 info->IER |= UART_IER_THRI;
1941 outb(info->IER, info->ioaddr + UART_IER);
1946 if (info->tty->termios->c_cflag & CRTSCTS) {
1947 info->MCR |= UART_MCR_RTS;
1948 outb(info->MCR, info->ioaddr + UART_MCR);
1953 * mxser_stop() and mxser_start()
1955 * This routines are called before setting or resetting tty->stopped.
1956 * They enable or disable transmitter interrupts, as necessary.
1958 static void mxser_stop(struct tty_struct *tty)
1960 struct mxser_port *info = tty->driver_data;
1961 unsigned long flags;
1963 spin_lock_irqsave(&info->slock, flags);
1964 if (info->IER & UART_IER_THRI) {
1965 info->IER &= ~UART_IER_THRI;
1966 outb(info->IER, info->ioaddr + UART_IER);
1968 spin_unlock_irqrestore(&info->slock, flags);
1971 static void mxser_start(struct tty_struct *tty)
1973 struct mxser_port *info = tty->driver_data;
1974 unsigned long flags;
1976 spin_lock_irqsave(&info->slock, flags);
1977 if (info->xmit_cnt && info->xmit_buf) {
1978 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1979 info->IER |= UART_IER_THRI;
1980 outb(info->IER, info->ioaddr + UART_IER);
1982 spin_unlock_irqrestore(&info->slock, flags);
1985 static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1987 struct mxser_port *info = tty->driver_data;
1988 unsigned long flags;
1990 spin_lock_irqsave(&info->slock, flags);
1991 mxser_change_speed(info, old_termios);
1992 spin_unlock_irqrestore(&info->slock, flags);
1994 if ((old_termios->c_cflag & CRTSCTS) &&
1995 !(tty->termios->c_cflag & CRTSCTS)) {
1996 tty->hw_stopped = 0;
1997 mxser_start(tty);
2000 /* Handle sw stopped */
2001 if ((old_termios->c_iflag & IXON) &&
2002 !(tty->termios->c_iflag & IXON)) {
2003 tty->stopped = 0;
2005 if (info->board->chip_flag) {
2006 spin_lock_irqsave(&info->slock, flags);
2007 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2008 spin_unlock_irqrestore(&info->slock, flags);
2011 mxser_start(tty);
2016 * mxser_wait_until_sent() --- wait until the transmitter is empty
2018 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2020 struct mxser_port *info = tty->driver_data;
2021 unsigned long orig_jiffies, char_time;
2022 int lsr;
2024 if (info->type == PORT_UNKNOWN)
2025 return;
2027 if (info->xmit_fifo_size == 0)
2028 return; /* Just in case.... */
2030 orig_jiffies = jiffies;
2032 * Set the check interval to be 1/5 of the estimated time to
2033 * send a single character, and make it at least 1. The check
2034 * interval should also be less than the timeout.
2036 * Note: we have to use pretty tight timings here to satisfy
2037 * the NIST-PCTS.
2039 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2040 char_time = char_time / 5;
2041 if (char_time == 0)
2042 char_time = 1;
2043 if (timeout && timeout < char_time)
2044 char_time = timeout;
2046 * If the transmitter hasn't cleared in twice the approximate
2047 * amount of time to send the entire FIFO, it probably won't
2048 * ever clear. This assumes the UART isn't doing flow
2049 * control, which is currently the case. Hence, if it ever
2050 * takes longer than info->timeout, this is probably due to a
2051 * UART bug of some kind. So, we clamp the timeout parameter at
2052 * 2*info->timeout.
2054 if (!timeout || timeout > 2 * info->timeout)
2055 timeout = 2 * info->timeout;
2056 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2057 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2058 timeout, char_time);
2059 printk("jiff=%lu...", jiffies);
2060 #endif
2061 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2062 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2063 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2064 #endif
2065 schedule_timeout_interruptible(char_time);
2066 if (signal_pending(current))
2067 break;
2068 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2069 break;
2071 set_current_state(TASK_RUNNING);
2073 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2074 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2075 #endif
2079 * This routine is called by tty_hangup() when a hangup is signaled.
2081 static void mxser_hangup(struct tty_struct *tty)
2083 struct mxser_port *info = tty->driver_data;
2085 mxser_flush_buffer(tty);
2086 mxser_shutdown(info);
2087 info->event = 0;
2088 info->count = 0;
2089 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2090 info->tty = NULL;
2091 wake_up_interruptible(&info->open_wait);
2095 * mxser_rs_break() --- routine which turns the break handling on or off
2097 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2099 struct mxser_port *info = tty->driver_data;
2100 unsigned long flags;
2102 spin_lock_irqsave(&info->slock, flags);
2103 if (break_state == -1)
2104 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2105 info->ioaddr + UART_LCR);
2106 else
2107 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2108 info->ioaddr + UART_LCR);
2109 spin_unlock_irqrestore(&info->slock, flags);
2112 static void mxser_receive_chars(struct mxser_port *port, int *status)
2114 struct tty_struct *tty = port->tty;
2115 unsigned char ch, gdl;
2116 int ignored = 0;
2117 int cnt = 0;
2118 int recv_room;
2119 int max = 256;
2121 recv_room = tty->receive_room;
2122 if ((recv_room == 0) && (!port->ldisc_stop_rx))
2123 mxser_stoprx(tty);
2125 if (port->board->chip_flag != MOXA_OTHER_UART) {
2127 if (*status & UART_LSR_SPECIAL)
2128 goto intr_old;
2129 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2130 (*status & MOXA_MUST_LSR_RERR))
2131 goto intr_old;
2132 if (*status & MOXA_MUST_LSR_RERR)
2133 goto intr_old;
2135 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2137 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2138 gdl &= MOXA_MUST_GDL_MASK;
2139 if (gdl >= recv_room) {
2140 if (!port->ldisc_stop_rx)
2141 mxser_stoprx(tty);
2143 while (gdl--) {
2144 ch = inb(port->ioaddr + UART_RX);
2145 tty_insert_flip_char(tty, ch, 0);
2146 cnt++;
2148 goto end_intr;
2150 intr_old:
2152 do {
2153 if (max-- < 0)
2154 break;
2156 ch = inb(port->ioaddr + UART_RX);
2157 if (port->board->chip_flag && (*status & UART_LSR_OE))
2158 outb(0x23, port->ioaddr + UART_FCR);
2159 *status &= port->read_status_mask;
2160 if (*status & port->ignore_status_mask) {
2161 if (++ignored > 100)
2162 break;
2163 } else {
2164 char flag = 0;
2165 if (*status & UART_LSR_SPECIAL) {
2166 if (*status & UART_LSR_BI) {
2167 flag = TTY_BREAK;
2168 port->icount.brk++;
2170 if (port->flags & ASYNC_SAK)
2171 do_SAK(tty);
2172 } else if (*status & UART_LSR_PE) {
2173 flag = TTY_PARITY;
2174 port->icount.parity++;
2175 } else if (*status & UART_LSR_FE) {
2176 flag = TTY_FRAME;
2177 port->icount.frame++;
2178 } else if (*status & UART_LSR_OE) {
2179 flag = TTY_OVERRUN;
2180 port->icount.overrun++;
2181 } else
2182 flag = TTY_BREAK;
2184 tty_insert_flip_char(tty, ch, flag);
2185 cnt++;
2186 if (cnt >= recv_room) {
2187 if (!port->ldisc_stop_rx)
2188 mxser_stoprx(tty);
2189 break;
2194 if (port->board->chip_flag)
2195 break;
2197 *status = inb(port->ioaddr + UART_LSR);
2198 } while (*status & UART_LSR_DR);
2200 end_intr:
2201 mxvar_log.rxcnt[port->tty->index] += cnt;
2202 port->mon_data.rxcnt += cnt;
2203 port->mon_data.up_rxcnt += cnt;
2206 * We are called from an interrupt context with &port->slock
2207 * being held. Drop it temporarily in order to prevent
2208 * recursive locking.
2210 spin_unlock(&port->slock);
2211 tty_flip_buffer_push(tty);
2212 spin_lock(&port->slock);
2215 static void mxser_transmit_chars(struct mxser_port *port)
2217 int count, cnt;
2219 if (port->x_char) {
2220 outb(port->x_char, port->ioaddr + UART_TX);
2221 port->x_char = 0;
2222 mxvar_log.txcnt[port->tty->index]++;
2223 port->mon_data.txcnt++;
2224 port->mon_data.up_txcnt++;
2225 port->icount.tx++;
2226 return;
2229 if (port->xmit_buf == 0)
2230 return;
2232 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2233 (port->tty->hw_stopped &&
2234 (port->type != PORT_16550A) &&
2235 (!port->board->chip_flag))) {
2236 port->IER &= ~UART_IER_THRI;
2237 outb(port->IER, port->ioaddr + UART_IER);
2238 return;
2241 cnt = port->xmit_cnt;
2242 count = port->xmit_fifo_size;
2243 do {
2244 outb(port->xmit_buf[port->xmit_tail++],
2245 port->ioaddr + UART_TX);
2246 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2247 if (--port->xmit_cnt <= 0)
2248 break;
2249 } while (--count > 0);
2250 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2252 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2253 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2254 port->icount.tx += (cnt - port->xmit_cnt);
2256 if (port->xmit_cnt < WAKEUP_CHARS)
2257 tty_wakeup(port->tty);
2259 if (port->xmit_cnt <= 0) {
2260 port->IER &= ~UART_IER_THRI;
2261 outb(port->IER, port->ioaddr + UART_IER);
2266 * This is the serial driver's generic interrupt routine
2268 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2270 int status, iir, i;
2271 struct mxser_board *brd = NULL;
2272 struct mxser_port *port;
2273 int max, irqbits, bits, msr;
2274 unsigned int int_cnt, pass_counter = 0;
2275 int handled = IRQ_NONE;
2277 for (i = 0; i < MXSER_BOARDS; i++)
2278 if (dev_id == &mxser_boards[i]) {
2279 brd = dev_id;
2280 break;
2283 if (i == MXSER_BOARDS)
2284 goto irq_stop;
2285 if (brd == NULL)
2286 goto irq_stop;
2287 max = brd->info->nports;
2288 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2289 irqbits = inb(brd->vector) & brd->vector_mask;
2290 if (irqbits == brd->vector_mask)
2291 break;
2293 handled = IRQ_HANDLED;
2294 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2295 if (irqbits == brd->vector_mask)
2296 break;
2297 if (bits & irqbits)
2298 continue;
2299 port = &brd->ports[i];
2301 int_cnt = 0;
2302 spin_lock(&port->slock);
2303 do {
2304 iir = inb(port->ioaddr + UART_IIR);
2305 if (iir & UART_IIR_NO_INT)
2306 break;
2307 iir &= MOXA_MUST_IIR_MASK;
2308 if (!port->tty ||
2309 (port->flags & ASYNC_CLOSING) ||
2310 !(port->flags &
2311 ASYNC_INITIALIZED)) {
2312 status = inb(port->ioaddr + UART_LSR);
2313 outb(0x27, port->ioaddr + UART_FCR);
2314 inb(port->ioaddr + UART_MSR);
2315 break;
2318 status = inb(port->ioaddr + UART_LSR);
2320 if (status & UART_LSR_PE)
2321 port->err_shadow |= NPPI_NOTIFY_PARITY;
2322 if (status & UART_LSR_FE)
2323 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2324 if (status & UART_LSR_OE)
2325 port->err_shadow |=
2326 NPPI_NOTIFY_HW_OVERRUN;
2327 if (status & UART_LSR_BI)
2328 port->err_shadow |= NPPI_NOTIFY_BREAK;
2330 if (port->board->chip_flag) {
2331 if (iir == MOXA_MUST_IIR_GDA ||
2332 iir == MOXA_MUST_IIR_RDA ||
2333 iir == MOXA_MUST_IIR_RTO ||
2334 iir == MOXA_MUST_IIR_LSR)
2335 mxser_receive_chars(port,
2336 &status);
2338 } else {
2339 status &= port->read_status_mask;
2340 if (status & UART_LSR_DR)
2341 mxser_receive_chars(port,
2342 &status);
2344 msr = inb(port->ioaddr + UART_MSR);
2345 if (msr & UART_MSR_ANY_DELTA)
2346 mxser_check_modem_status(port, msr);
2348 if (port->board->chip_flag) {
2349 if (iir == 0x02 && (status &
2350 UART_LSR_THRE))
2351 mxser_transmit_chars(port);
2352 } else {
2353 if (status & UART_LSR_THRE)
2354 mxser_transmit_chars(port);
2356 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2357 spin_unlock(&port->slock);
2361 irq_stop:
2362 return handled;
2365 static const struct tty_operations mxser_ops = {
2366 .open = mxser_open,
2367 .close = mxser_close,
2368 .write = mxser_write,
2369 .put_char = mxser_put_char,
2370 .flush_chars = mxser_flush_chars,
2371 .write_room = mxser_write_room,
2372 .chars_in_buffer = mxser_chars_in_buffer,
2373 .flush_buffer = mxser_flush_buffer,
2374 .ioctl = mxser_ioctl,
2375 .throttle = mxser_throttle,
2376 .unthrottle = mxser_unthrottle,
2377 .set_termios = mxser_set_termios,
2378 .stop = mxser_stop,
2379 .start = mxser_start,
2380 .hangup = mxser_hangup,
2381 .break_ctl = mxser_rs_break,
2382 .wait_until_sent = mxser_wait_until_sent,
2383 .tiocmget = mxser_tiocmget,
2384 .tiocmset = mxser_tiocmset,
2388 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2391 static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2392 unsigned int irq)
2394 if (irq)
2395 free_irq(brd->irq, brd);
2396 if (pdev != NULL) { /* PCI */
2397 #ifdef CONFIG_PCI
2398 pci_release_region(pdev, 2);
2399 pci_release_region(pdev, 3);
2400 #endif
2401 } else {
2402 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2403 release_region(brd->vector, 1);
2407 static int __devinit mxser_initbrd(struct mxser_board *brd,
2408 struct pci_dev *pdev)
2410 struct mxser_port *info;
2411 unsigned int i;
2412 int retval;
2414 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2416 for (i = 0; i < brd->info->nports; i++) {
2417 info = &brd->ports[i];
2418 info->board = brd;
2419 info->stop_rx = 0;
2420 info->ldisc_stop_rx = 0;
2422 /* Enhance mode enabled here */
2423 if (brd->chip_flag != MOXA_OTHER_UART)
2424 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2426 info->flags = ASYNC_SHARE_IRQ;
2427 info->type = brd->uart_type;
2429 process_txrx_fifo(info);
2431 info->custom_divisor = info->baud_base * 16;
2432 info->close_delay = 5 * HZ / 10;
2433 info->closing_wait = 30 * HZ;
2434 info->normal_termios = mxvar_sdriver->init_termios;
2435 init_waitqueue_head(&info->open_wait);
2436 init_waitqueue_head(&info->delta_msr_wait);
2437 info->speed = 9600;
2438 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2439 info->err_shadow = 0;
2440 spin_lock_init(&info->slock);
2442 /* before set INT ISR, disable all int */
2443 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2444 info->ioaddr + UART_IER);
2447 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2448 brd);
2449 if (retval) {
2450 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2451 "conflict with another device.\n",
2452 brd->info->name, brd->irq);
2453 /* We hold resources, we need to release them. */
2454 mxser_release_res(brd, pdev, 0);
2456 return retval;
2459 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2461 int id, i, bits;
2462 unsigned short regs[16], irq;
2463 unsigned char scratch, scratch2;
2465 brd->chip_flag = MOXA_OTHER_UART;
2467 id = mxser_read_register(cap, regs);
2468 switch (id) {
2469 case C168_ASIC_ID:
2470 brd->info = &mxser_cards[0];
2471 break;
2472 case C104_ASIC_ID:
2473 brd->info = &mxser_cards[1];
2474 break;
2475 case CI104J_ASIC_ID:
2476 brd->info = &mxser_cards[2];
2477 break;
2478 case C102_ASIC_ID:
2479 brd->info = &mxser_cards[5];
2480 break;
2481 case CI132_ASIC_ID:
2482 brd->info = &mxser_cards[6];
2483 break;
2484 case CI134_ASIC_ID:
2485 brd->info = &mxser_cards[7];
2486 break;
2487 default:
2488 return 0;
2491 irq = 0;
2492 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2493 Flag-hack checks if configuration should be read as 2-port here. */
2494 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2495 irq = regs[9] & 0xF000;
2496 irq = irq | (irq >> 4);
2497 if (irq != (regs[9] & 0xFF00))
2498 return MXSER_ERR_IRQ_CONFLIT;
2499 } else if (brd->info->nports == 4) {
2500 irq = regs[9] & 0xF000;
2501 irq = irq | (irq >> 4);
2502 irq = irq | (irq >> 8);
2503 if (irq != regs[9])
2504 return MXSER_ERR_IRQ_CONFLIT;
2505 } else if (brd->info->nports == 8) {
2506 irq = regs[9] & 0xF000;
2507 irq = irq | (irq >> 4);
2508 irq = irq | (irq >> 8);
2509 if ((irq != regs[9]) || (irq != regs[10]))
2510 return MXSER_ERR_IRQ_CONFLIT;
2513 if (!irq)
2514 return MXSER_ERR_IRQ;
2515 brd->irq = ((int)(irq & 0xF000) >> 12);
2516 for (i = 0; i < 8; i++)
2517 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2518 if ((regs[12] & 0x80) == 0)
2519 return MXSER_ERR_VECTOR;
2520 brd->vector = (int)regs[11]; /* interrupt vector */
2521 if (id == 1)
2522 brd->vector_mask = 0x00FF;
2523 else
2524 brd->vector_mask = 0x000F;
2525 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2526 if (regs[12] & bits) {
2527 brd->ports[i].baud_base = 921600;
2528 brd->ports[i].max_baud = 921600;
2529 } else {
2530 brd->ports[i].baud_base = 115200;
2531 brd->ports[i].max_baud = 115200;
2534 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2535 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2536 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2537 outb(scratch2, cap + UART_LCR);
2538 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2539 scratch = inb(cap + UART_IIR);
2541 if (scratch & 0xC0)
2542 brd->uart_type = PORT_16550A;
2543 else
2544 brd->uart_type = PORT_16450;
2545 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2546 "mxser(IO)"))
2547 return MXSER_ERR_IOADDR;
2548 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2549 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2550 return MXSER_ERR_VECTOR;
2552 return brd->info->nports;
2555 static int __devinit mxser_probe(struct pci_dev *pdev,
2556 const struct pci_device_id *ent)
2558 #ifdef CONFIG_PCI
2559 struct mxser_board *brd;
2560 unsigned int i, j;
2561 unsigned long ioaddress;
2562 int retval = -EINVAL;
2564 for (i = 0; i < MXSER_BOARDS; i++)
2565 if (mxser_boards[i].info == NULL)
2566 break;
2568 if (i >= MXSER_BOARDS) {
2569 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2570 "(maximum %d), board not configured\n", MXSER_BOARDS);
2571 goto err;
2574 brd = &mxser_boards[i];
2575 brd->idx = i * MXSER_PORTS_PER_BOARD;
2576 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2577 mxser_cards[ent->driver_data].name,
2578 pdev->bus->number, PCI_SLOT(pdev->devfn));
2580 retval = pci_enable_device(pdev);
2581 if (retval) {
2582 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2583 goto err;
2586 /* io address */
2587 ioaddress = pci_resource_start(pdev, 2);
2588 retval = pci_request_region(pdev, 2, "mxser(IO)");
2589 if (retval)
2590 goto err;
2592 brd->info = &mxser_cards[ent->driver_data];
2593 for (i = 0; i < brd->info->nports; i++)
2594 brd->ports[i].ioaddr = ioaddress + 8 * i;
2596 /* vector */
2597 ioaddress = pci_resource_start(pdev, 3);
2598 retval = pci_request_region(pdev, 3, "mxser(vector)");
2599 if (retval)
2600 goto err_relio;
2601 brd->vector = ioaddress;
2603 /* irq */
2604 brd->irq = pdev->irq;
2606 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2607 brd->uart_type = PORT_16550A;
2608 brd->vector_mask = 0;
2610 for (i = 0; i < brd->info->nports; i++) {
2611 for (j = 0; j < UART_INFO_NUM; j++) {
2612 if (Gpci_uart_info[j].type == brd->chip_flag) {
2613 brd->ports[i].max_baud =
2614 Gpci_uart_info[j].max_baud;
2616 /* exception....CP-102 */
2617 if (brd->info->flags & MXSER_HIGHBAUD)
2618 brd->ports[i].max_baud = 921600;
2619 break;
2624 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2625 for (i = 0; i < brd->info->nports; i++) {
2626 if (i < 4)
2627 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2628 else
2629 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2631 outb(0, ioaddress + 4); /* default set to RS232 mode */
2632 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2635 for (i = 0; i < brd->info->nports; i++) {
2636 brd->vector_mask |= (1 << i);
2637 brd->ports[i].baud_base = 921600;
2640 /* mxser_initbrd will hook ISR. */
2641 retval = mxser_initbrd(brd, pdev);
2642 if (retval)
2643 goto err_null;
2645 for (i = 0; i < brd->info->nports; i++)
2646 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2648 pci_set_drvdata(pdev, brd);
2650 return 0;
2651 err_relio:
2652 pci_release_region(pdev, 2);
2653 err_null:
2654 brd->info = NULL;
2655 err:
2656 return retval;
2657 #else
2658 return -ENODEV;
2659 #endif
2662 static void __devexit mxser_remove(struct pci_dev *pdev)
2664 struct mxser_board *brd = pci_get_drvdata(pdev);
2665 unsigned int i;
2667 for (i = 0; i < brd->info->nports; i++)
2668 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2670 mxser_release_res(brd, pdev, 1);
2671 brd->info = NULL;
2674 static struct pci_driver mxser_driver = {
2675 .name = "mxser",
2676 .id_table = mxser_pcibrds,
2677 .probe = mxser_probe,
2678 .remove = __devexit_p(mxser_remove)
2681 static int __init mxser_module_init(void)
2683 struct mxser_board *brd;
2684 unsigned long cap;
2685 unsigned int i, m, isaloop;
2686 int retval, b;
2688 pr_debug("Loading module mxser ...\n");
2690 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2691 if (!mxvar_sdriver)
2692 return -ENOMEM;
2694 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2695 MXSER_VERSION);
2697 /* Initialize the tty_driver structure */
2698 mxvar_sdriver->owner = THIS_MODULE;
2699 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2700 mxvar_sdriver->name = "ttyMI";
2701 mxvar_sdriver->major = ttymajor;
2702 mxvar_sdriver->minor_start = 0;
2703 mxvar_sdriver->num = MXSER_PORTS + 1;
2704 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2705 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2706 mxvar_sdriver->init_termios = tty_std_termios;
2707 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2708 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2709 tty_set_operations(mxvar_sdriver, &mxser_ops);
2711 retval = tty_register_driver(mxvar_sdriver);
2712 if (retval) {
2713 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2714 "tty driver !\n");
2715 goto err_put;
2718 mxvar_diagflag = 0;
2720 m = 0;
2721 /* Start finding ISA boards here */
2722 for (isaloop = 0; isaloop < 2; isaloop++)
2723 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2724 if (!isaloop)
2725 cap = mxserBoardCAP[b]; /* predefined */
2726 else
2727 cap = ioaddr[b]; /* module param */
2729 if (!cap)
2730 continue;
2732 brd = &mxser_boards[m];
2733 retval = mxser_get_ISA_conf(cap, brd);
2735 if (retval != 0)
2736 printk(KERN_INFO "Found MOXA %s board "
2737 "(CAP=0x%x)\n",
2738 brd->info->name, ioaddr[b]);
2740 if (retval <= 0) {
2741 if (retval == MXSER_ERR_IRQ)
2742 printk(KERN_ERR "Invalid interrupt "
2743 "number, board not "
2744 "configured\n");
2745 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2746 printk(KERN_ERR "Invalid interrupt "
2747 "number, board not "
2748 "configured\n");
2749 else if (retval == MXSER_ERR_VECTOR)
2750 printk(KERN_ERR "Invalid interrupt "
2751 "vector, board not "
2752 "configured\n");
2753 else if (retval == MXSER_ERR_IOADDR)
2754 printk(KERN_ERR "Invalid I/O address, "
2755 "board not configured\n");
2757 brd->info = NULL;
2758 continue;
2761 /* mxser_initbrd will hook ISR. */
2762 if (mxser_initbrd(brd, NULL) < 0) {
2763 brd->info = NULL;
2764 continue;
2767 brd->idx = m * MXSER_PORTS_PER_BOARD;
2768 for (i = 0; i < brd->info->nports; i++)
2769 tty_register_device(mxvar_sdriver, brd->idx + i,
2770 NULL);
2772 m++;
2775 retval = pci_register_driver(&mxser_driver);
2776 if (retval) {
2777 printk(KERN_ERR "Can't register pci driver\n");
2778 if (!m) {
2779 retval = -ENODEV;
2780 goto err_unr;
2781 } /* else: we have some ISA cards under control */
2784 pr_debug("Done.\n");
2786 return 0;
2787 err_unr:
2788 tty_unregister_driver(mxvar_sdriver);
2789 err_put:
2790 put_tty_driver(mxvar_sdriver);
2791 return retval;
2794 static void __exit mxser_module_exit(void)
2796 unsigned int i, j;
2798 pr_debug("Unloading module mxser ...\n");
2800 pci_unregister_driver(&mxser_driver);
2802 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2803 if (mxser_boards[i].info != NULL)
2804 for (j = 0; j < mxser_boards[i].info->nports; j++)
2805 tty_unregister_device(mxvar_sdriver,
2806 mxser_boards[i].idx + j);
2807 tty_unregister_driver(mxvar_sdriver);
2808 put_tty_driver(mxvar_sdriver);
2810 for (i = 0; i < MXSER_BOARDS; i++)
2811 if (mxser_boards[i].info != NULL)
2812 mxser_release_res(&mxser_boards[i], NULL, 1);
2814 pr_debug("Done.\n");
2817 module_init(mxser_module_init);
2818 module_exit(mxser_module_exit);