1 /*****************************************************************************/
3 * moxa.c -- MOXA Intellio 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.
17 * MOXA Intellio Series Driver
23 #include <linux/module.h>
24 #include <linux/types.h>
26 #include <linux/ioport.h>
27 #include <linux/errno.h>
28 #include <linux/firmware.h>
29 #include <linux/signal.h>
30 #include <linux/sched.h>
31 #include <linux/timer.h>
32 #include <linux/interrupt.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
35 #include <linux/major.h>
36 #include <linux/string.h>
37 #include <linux/fcntl.h>
38 #include <linux/ptrace.h>
39 #include <linux/serial.h>
40 #include <linux/tty_driver.h>
41 #include <linux/delay.h>
42 #include <linux/pci.h>
43 #include <linux/init.h>
44 #include <linux/bitops.h>
45 #include <linux/completion.h>
47 #include <asm/system.h>
49 #include <asm/uaccess.h>
53 #define MOXA_VERSION "5.1k"
55 #define MOXA_FW_HDRLEN 32
58 #define MOXACUMAJOR 173
60 #define MAX_BOARDS 4 /* Don't change this value */
61 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
62 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
65 * Define the Moxa PCI vendor and device IDs.
67 #define MOXA_BUS_TYPE_ISA 0
68 #define MOXA_BUS_TYPE_PCI 1
71 MOXA_BOARD_C218_PCI = 1,
78 static char *moxa_brdname[] =
80 "C218 Turbo PCI series",
81 "C218 Turbo ISA series",
82 "C320 Turbo PCI series",
83 "C320 Turbo ISA series",
88 static struct pci_device_id moxa_pcibrds[] = {
89 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
90 .driver_data = MOXA_BOARD_C218_PCI },
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
92 .driver_data = MOXA_BOARD_C320_PCI },
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
94 .driver_data = MOXA_BOARD_CP204J },
97 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
98 #endif /* CONFIG_PCI */
102 static struct moxa_board_conf {
109 struct moxa_port *ports;
111 void __iomem *basemem;
112 void __iomem *intNdx;
113 void __iomem *intPend;
114 void __iomem *intTable;
115 } moxa_boards[MAX_BOARDS];
117 struct mxser_mstatus {
134 unsigned short closing_wait;
137 long event; /* long req'd for set_bit --RR */
139 unsigned long statusflags;
140 struct tty_struct *tty;
142 wait_queue_head_t open_wait;
143 struct completion close_wait;
145 struct timer_list emptyTimer;
149 void __iomem *tableAddr;
158 #define TXSTOPPED 0x1
160 #define EMPTYWAIT 0x4
163 #define SERIAL_DO_RESTART
165 #define WAKEUP_CHARS 256
167 static int ttymajor = MOXAMAJOR;
169 /* Variables for insmod */
171 static unsigned long baseaddr[MAX_BOARDS];
172 static unsigned int type[MAX_BOARDS];
173 static unsigned int numports[MAX_BOARDS];
176 MODULE_AUTHOR("William Chen");
177 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
178 MODULE_LICENSE("GPL");
180 module_param_array(type, uint, NULL, 0);
181 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
182 module_param_array(baseaddr, ulong, NULL, 0);
183 MODULE_PARM_DESC(baseaddr, "base address");
184 module_param_array(numports, uint, NULL, 0);
185 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
187 module_param(ttymajor, int, 0);
192 static int moxa_open(struct tty_struct *, struct file *);
193 static void moxa_close(struct tty_struct *, struct file *);
194 static int moxa_write(struct tty_struct *, const unsigned char *, int);
195 static int moxa_write_room(struct tty_struct *);
196 static void moxa_flush_buffer(struct tty_struct *);
197 static int moxa_chars_in_buffer(struct tty_struct *);
198 static void moxa_flush_chars(struct tty_struct *);
199 static void moxa_put_char(struct tty_struct *, unsigned char);
200 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
201 static void moxa_throttle(struct tty_struct *);
202 static void moxa_unthrottle(struct tty_struct *);
203 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
204 static void moxa_stop(struct tty_struct *);
205 static void moxa_start(struct tty_struct *);
206 static void moxa_hangup(struct tty_struct *);
207 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
208 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
209 unsigned int set, unsigned int clear);
210 static void moxa_poll(unsigned long);
211 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
212 static int moxa_block_till_ready(struct tty_struct *, struct file *,
214 static void moxa_setup_empty_event(struct tty_struct *);
215 static void moxa_check_xmit_empty(unsigned long);
216 static void moxa_shut_down(struct moxa_port *);
217 static void moxa_receive_data(struct moxa_port *);
219 * moxa board interface functions:
221 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
222 static int MoxaDriverPoll(void);
223 static int MoxaPortsOfCard(int);
224 static int MoxaPortIsValid(int);
225 static void MoxaPortEnable(int);
226 static void MoxaPortDisable(int);
227 static long MoxaPortGetMaxBaud(int);
228 static long MoxaPortSetBaud(int, long);
229 static int MoxaPortSetTermio(int, struct ktermios *, speed_t);
230 static int MoxaPortGetLineOut(int, int *, int *);
231 static void MoxaPortLineCtrl(int, int, int);
232 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
233 static int MoxaPortLineStatus(int);
234 static int MoxaPortDCDChange(int);
235 static int MoxaPortDCDON(int);
236 static void MoxaPortFlushData(int, int);
237 static int MoxaPortWriteData(int, unsigned char *, int);
238 static int MoxaPortReadData(int, struct tty_struct *tty);
239 static int MoxaPortTxQueue(int);
240 static int MoxaPortRxQueue(int);
241 static int MoxaPortTxFree(int);
242 static void MoxaPortTxDisable(int);
243 static void MoxaPortTxEnable(int);
244 static int MoxaPortResetBrkCnt(int);
245 static void MoxaPortSendBreak(int, int);
246 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
247 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
248 static void MoxaSetFifo(int port, int enable);
250 static const struct tty_operations moxa_ops = {
254 .write_room = moxa_write_room,
255 .flush_buffer = moxa_flush_buffer,
256 .chars_in_buffer = moxa_chars_in_buffer,
257 .flush_chars = moxa_flush_chars,
258 .put_char = moxa_put_char,
260 .throttle = moxa_throttle,
261 .unthrottle = moxa_unthrottle,
262 .set_termios = moxa_set_termios,
265 .hangup = moxa_hangup,
266 .tiocmget = moxa_tiocmget,
267 .tiocmset = moxa_tiocmset,
270 static struct tty_driver *moxaDriver;
271 static struct moxa_port moxa_ports[MAX_PORTS];
272 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
273 static DEFINE_SPINLOCK(moxa_lock);
275 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
277 switch (brd->boardType) {
278 case MOXA_BOARD_C218_ISA:
279 case MOXA_BOARD_C218_PCI:
283 case MOXA_BOARD_CP204J:
297 static int moxa_check_fw(const void *ptr)
299 const __le16 *lptr = ptr;
301 if (*lptr != cpu_to_le16(0x7980))
307 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
310 void __iomem *baseAddr = brd->basemem;
313 writeb(HW_reset, baseAddr + Control_reg); /* reset */
315 memset_io(baseAddr, 0, 4096);
316 memcpy_toio(baseAddr, buf, len); /* download BIOS */
317 writeb(0, baseAddr + Control_reg); /* restart */
321 switch (brd->boardType) {
322 case MOXA_BOARD_C218_ISA:
323 case MOXA_BOARD_C218_PCI:
324 tmp = readw(baseAddr + C218_key);
325 if (tmp != C218_KeyCode)
328 case MOXA_BOARD_CP204J:
329 tmp = readw(baseAddr + C218_key);
330 if (tmp != CP204J_KeyCode)
334 tmp = readw(baseAddr + C320_key);
335 if (tmp != C320_KeyCode)
337 tmp = readw(baseAddr + C320_status);
338 if (tmp != STS_init) {
339 printk(KERN_ERR "moxa: bios upload failed -- CPU/Basic "
340 "module not found\n");
348 printk(KERN_ERR "moxa: bios upload failed -- board not found\n");
352 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
355 void __iomem *baseAddr = brd->basemem;
358 printk(KERN_ERR "moxa: invalid 320 bios -- too short\n");
362 writew(len - 7168 - 2, baseAddr + C320bapi_len);
363 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
364 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
365 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
366 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
371 static int moxa_load_c218(struct moxa_board_conf *brd, const void *ptr,
374 void __iomem *baseAddr = brd->basemem;
375 const u16 *uptr = ptr;
376 size_t wlen, len2, j;
377 unsigned int i, retry;
380 if (brd->boardType == MOXA_BOARD_CP204J)
381 keycode = CP204J_KeyCode;
383 keycode = C218_KeyCode;
386 for (i = 0; i < wlen; i++)
387 usum += le16_to_cpu(uptr[i]);
393 len2 = (wlen > 2048) ? 2048 : wlen;
395 memcpy_toio(baseAddr + C218_LoadBuf, ptr + j,
399 writew(len2, baseAddr + C218DLoad_len);
400 writew(0, baseAddr + C218_key);
401 for (i = 0; i < 100; i++) {
402 if (readw(baseAddr + C218_key) == keycode)
406 if (readw(baseAddr + C218_key) != keycode)
409 writew(0, baseAddr + C218DLoad_len);
410 writew(usum, baseAddr + C218check_sum);
411 writew(0, baseAddr + C218_key);
412 for (i = 0; i < 100; i++) {
413 if (readw(baseAddr + C218_key) == keycode)
418 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
419 if (readb(baseAddr + C218chksum_ok) != 1)
422 writew(0, baseAddr + C218_key);
423 for (i = 0; i < 100; i++) {
424 if (readw(baseAddr + Magic_no) == Magic_code)
428 if (readw(baseAddr + Magic_no) != Magic_code)
431 writew(1, baseAddr + Disable_IRQ);
432 writew(0, baseAddr + Magic_no);
433 for (i = 0; i < 100; i++) {
434 if (readw(baseAddr + Magic_no) == Magic_code)
438 if (readw(baseAddr + Magic_no) != Magic_code)
442 brd->intNdx = baseAddr + IRQindex;
443 brd->intPend = baseAddr + IRQpending;
444 brd->intTable = baseAddr + IRQtable;
449 static int moxa_load_c320(struct moxa_board_conf *brd, const void *ptr,
452 void __iomem *baseAddr = brd->basemem;
453 const u16 *uptr = ptr;
454 size_t wlen, len2, j;
455 unsigned int i, retry;
460 for (i = 0; i < wlen; i++)
461 usum += le16_to_cpu(uptr[i]);
467 len2 = (wlen > 2048) ? 2048 : wlen;
469 memcpy_toio(baseAddr + C320_LoadBuf, ptr + j,
472 writew(len2, baseAddr + C320DLoad_len);
473 writew(0, baseAddr + C320_key);
474 for (i = 0; i < 10; i++) {
475 if (readw(baseAddr + C320_key) == C320_KeyCode)
479 if (readw(baseAddr + C320_key) != C320_KeyCode)
482 writew(0, baseAddr + C320DLoad_len);
483 writew(usum, baseAddr + C320check_sum);
484 writew(0, baseAddr + C320_key);
485 for (i = 0; i < 10; i++) {
486 if (readw(baseAddr + C320_key) == C320_KeyCode)
491 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
492 if (readb(baseAddr + C320chksum_ok) != 1)
495 writew(0, baseAddr + C320_key);
496 for (i = 0; i < 600; i++) {
497 if (readw(baseAddr + Magic_no) == Magic_code)
501 if (readw(baseAddr + Magic_no) != Magic_code)
504 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
505 writew(0x3800, baseAddr + TMS320_PORT1);
506 writew(0x3900, baseAddr + TMS320_PORT2);
507 writew(28499, baseAddr + TMS320_CLOCK);
509 writew(0x3200, baseAddr + TMS320_PORT1);
510 writew(0x3400, baseAddr + TMS320_PORT2);
511 writew(19999, baseAddr + TMS320_CLOCK);
513 writew(1, baseAddr + Disable_IRQ);
514 writew(0, baseAddr + Magic_no);
515 for (i = 0; i < 500; i++) {
516 if (readw(baseAddr + Magic_no) == Magic_code)
520 if (readw(baseAddr + Magic_no) != Magic_code)
523 j = readw(baseAddr + Module_cnt);
526 brd->numPorts = j * 8;
527 writew(j, baseAddr + Module_no);
528 writew(0, baseAddr + Magic_no);
529 for (i = 0; i < 600; i++) {
530 if (readw(baseAddr + Magic_no) == Magic_code)
534 if (readw(baseAddr + Magic_no) != Magic_code)
537 brd->intNdx = baseAddr + IRQindex;
538 brd->intPend = baseAddr + IRQpending;
539 brd->intTable = baseAddr + IRQtable;
544 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
547 void __iomem *ofsAddr, *baseAddr = brd->basemem;
548 struct moxa_port *port;
552 printk(KERN_ERR "moxa: C2XX bios length is not even\n");
556 switch (brd->boardType) {
557 case MOXA_BOARD_C218_ISA:
558 case MOXA_BOARD_C218_PCI:
559 case MOXA_BOARD_CP204J:
560 retval = moxa_load_c218(brd, ptr, len);
564 for (i = 0; i < brd->numPorts; i++, port++) {
566 port->curBaud = 9600L;
568 port->tableAddr = baseAddr + Extern_table +
570 ofsAddr = port->tableAddr;
571 writew(C218rx_mask, ofsAddr + RX_mask);
572 writew(C218tx_mask, ofsAddr + TX_mask);
573 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
574 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
576 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
577 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
582 retval = moxa_load_c320(brd, ptr, len); /* fills in numPorts */
586 for (i = 0; i < brd->numPorts; i++, port++) {
588 port->curBaud = 9600L;
590 port->tableAddr = baseAddr + Extern_table +
592 ofsAddr = port->tableAddr;
593 switch (brd->numPorts) {
595 writew(C320p8rx_mask, ofsAddr + RX_mask);
596 writew(C320p8tx_mask, ofsAddr + TX_mask);
597 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
598 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
599 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
600 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
604 writew(C320p16rx_mask, ofsAddr + RX_mask);
605 writew(C320p16tx_mask, ofsAddr + TX_mask);
606 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
607 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
608 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
609 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
613 writew(C320p24rx_mask, ofsAddr + RX_mask);
614 writew(C320p24tx_mask, ofsAddr + TX_mask);
615 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
616 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
617 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
618 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
621 writew(C320p32rx_mask, ofsAddr + RX_mask);
622 writew(C320p32tx_mask, ofsAddr + TX_mask);
623 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
624 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
625 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
626 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
627 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
637 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
639 void *ptr = fw->data;
643 unsigned int a, lenp, lencnt;
646 __le32 magic; /* 0x34303430 */
648 u8 type; /* UNIX = 3 */
649 u8 model; /* C218T=1, C320T=2, CP204=3 */
654 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
656 if (fw->size < MOXA_FW_HDRLEN) {
657 strcpy(rsn, "too short (even header won't fit)");
660 if (hdr->magic != cpu_to_le32(0x30343034)) {
661 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
664 if (hdr->type != 3) {
665 sprintf(rsn, "not for linux, type is %u", hdr->type);
668 if (moxa_check_fw_model(brd, hdr->model)) {
669 sprintf(rsn, "not for this card, model is %u", hdr->model);
673 len = MOXA_FW_HDRLEN;
674 lencnt = hdr->model == 2 ? 5 : 3;
675 for (a = 0; a < ARRAY_SIZE(lens); a++) {
676 lens[a] = le16_to_cpu(hdr->len[a]);
677 if (lens[a] && len + lens[a] <= fw->size &&
678 moxa_check_fw(&fw->data[len]))
679 printk(KERN_WARNING "moxa firmware: unexpected input "
680 "at offset %u, but going on\n", (u32)len);
681 if (!lens[a] && a < lencnt) {
682 sprintf(rsn, "too few entries in fw file");
688 if (len != fw->size) {
689 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
694 ptr += MOXA_FW_HDRLEN;
697 strcpy(rsn, "read above");
699 ret = moxa_load_bios(brd, ptr, lens[lenp]);
703 /* we skip the tty section (lens[1]), since we don't need it */
704 ptr += lens[lenp] + lens[lenp + 1];
705 lenp += 2; /* comm */
707 if (hdr->model == 2) {
708 ret = moxa_load_320b(brd, ptr, lens[lenp]);
711 /* skip another tty */
712 ptr += lens[lenp] + lens[lenp + 1];
716 ret = moxa_load_code(brd, ptr, lens[lenp]);
722 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
726 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
728 const struct firmware *fw;
732 switch (brd->boardType) {
733 case MOXA_BOARD_C218_ISA:
734 case MOXA_BOARD_C218_PCI:
735 file = "c218tunx.cod";
737 case MOXA_BOARD_CP204J:
738 file = "cp204unx.cod";
741 file = "c320tunx.cod";
745 ret = request_firmware(&fw, file, dev);
747 printk(KERN_ERR "request_firmware failed\n");
751 ret = moxa_load_fw(brd, fw);
753 release_firmware(fw);
759 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
760 const struct pci_device_id *ent)
762 struct moxa_board_conf *board;
764 int board_type = ent->driver_data;
767 retval = pci_enable_device(pdev);
769 dev_err(&pdev->dev, "can't enable pci device\n");
773 for (i = 0; i < MAX_BOARDS; i++)
774 if (moxa_boards[i].basemem == NULL)
778 if (i >= MAX_BOARDS) {
779 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
780 "found. Board is ignored.\n", MAX_BOARDS);
784 board = &moxa_boards[i];
785 board->ports = &moxa_ports[i * MAX_PORTS_PER_BOARD];
787 retval = pci_request_region(pdev, 2, "moxa-base");
789 dev_err(&pdev->dev, "can't request pci region 2\n");
793 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
794 if (board->basemem == NULL) {
795 dev_err(&pdev->dev, "can't remap io space 2\n");
799 board->boardType = board_type;
800 switch (board_type) {
801 case MOXA_BOARD_C218_ISA:
802 case MOXA_BOARD_C218_PCI:
806 case MOXA_BOARD_CP204J:
813 board->busType = MOXA_BUS_TYPE_PCI;
815 retval = moxa_init_board(board, &pdev->dev);
819 pci_set_drvdata(pdev, board);
823 iounmap(board->basemem);
824 board->basemem = NULL;
826 pci_release_region(pdev, 2);
831 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
833 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
835 iounmap(brd->basemem);
837 pci_release_region(pdev, 2);
840 static struct pci_driver moxa_pci_driver = {
842 .id_table = moxa_pcibrds,
843 .probe = moxa_pci_probe,
844 .remove = __devexit_p(moxa_pci_remove)
846 #endif /* CONFIG_PCI */
848 static int __init moxa_init(void)
850 struct moxa_port *ch;
851 unsigned int i, isabrds = 0;
854 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
856 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
860 moxaDriver->owner = THIS_MODULE;
861 moxaDriver->name = "ttyMX";
862 moxaDriver->major = ttymajor;
863 moxaDriver->minor_start = 0;
864 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
865 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
866 moxaDriver->init_termios = tty_std_termios;
867 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
868 moxaDriver->init_termios.c_ispeed = 9600;
869 moxaDriver->init_termios.c_ospeed = 9600;
870 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
871 tty_set_operations(moxaDriver, &moxa_ops);
873 for (i = 0, ch = moxa_ports; i < MAX_PORTS; i++, ch++) {
874 ch->type = PORT_16550A;
876 ch->close_delay = 5 * HZ / 10;
877 ch->closing_wait = 30 * HZ;
878 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
879 init_waitqueue_head(&ch->open_wait);
880 init_completion(&ch->close_wait);
882 setup_timer(&ch->emptyTimer, moxa_check_xmit_empty,
886 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
888 if (tty_register_driver(moxaDriver)) {
889 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
890 put_tty_driver(moxaDriver);
894 mod_timer(&moxaTimer, jiffies + HZ / 50);
896 /* Find the boards defined from module args. */
899 struct moxa_board_conf *brd = moxa_boards;
900 for (i = 0; i < MAX_BOARDS; i++) {
903 if (type[i] == MOXA_BOARD_C218_ISA ||
904 type[i] == MOXA_BOARD_C320_ISA) {
905 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
906 isabrds + 1, moxa_brdname[type[i] - 1],
908 brd->boardType = type[i];
909 brd->ports = &moxa_ports[isabrds * MAX_PORTS_PER_BOARD];
910 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
912 brd->busType = MOXA_BUS_TYPE_ISA;
913 brd->basemem = ioremap(baseaddr[i], 0x4000);
915 printk(KERN_ERR "moxa: can't remap %lx\n",
919 if (moxa_init_board(brd, NULL)) {
920 iounmap(brd->basemem);
933 retval = pci_register_driver(&moxa_pci_driver);
935 printk(KERN_ERR "Can't register moxa pci driver!\n");
944 static void __exit moxa_exit(void)
948 del_timer_sync(&moxaTimer);
950 for (i = 0; i < MAX_PORTS; i++)
951 del_timer_sync(&moxa_ports[i].emptyTimer);
953 if (tty_unregister_driver(moxaDriver))
954 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
956 put_tty_driver(moxaDriver);
959 pci_unregister_driver(&moxa_pci_driver);
962 for (i = 0; i < MAX_BOARDS; i++)
963 if (moxa_boards[i].basemem)
964 iounmap(moxa_boards[i].basemem);
967 module_init(moxa_init);
968 module_exit(moxa_exit);
970 static int moxa_open(struct tty_struct *tty, struct file *filp)
972 struct moxa_port *ch;
977 if (port == MAX_PORTS) {
980 if (!MoxaPortIsValid(port)) {
981 tty->driver_data = NULL;
985 ch = &moxa_ports[port];
987 tty->driver_data = ch;
989 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
991 moxa_set_tty_param(tty, tty->termios);
992 MoxaPortLineCtrl(ch->port, 1, 1);
993 MoxaPortEnable(ch->port);
994 ch->asyncflags |= ASYNC_INITIALIZED;
996 retval = moxa_block_till_ready(tty, filp, ch);
998 moxa_unthrottle(tty);
1000 if (ch->type == PORT_16550A) {
1001 MoxaSetFifo(ch->port, 1);
1003 MoxaSetFifo(ch->port, 0);
1009 static void moxa_close(struct tty_struct *tty, struct file *filp)
1011 struct moxa_port *ch;
1015 if (port == MAX_PORTS) {
1018 if (!MoxaPortIsValid(port)) {
1019 pr_debug("Invalid portno in moxa_close\n");
1020 tty->driver_data = NULL;
1023 if (tty->driver_data == NULL) {
1026 if (tty_hung_up_p(filp)) {
1029 ch = (struct moxa_port *) tty->driver_data;
1031 if ((tty->count == 1) && (ch->count != 1)) {
1032 printk(KERN_WARNING "moxa_close: bad serial port count; "
1033 "tty->count is 1, ch->count is %d\n", ch->count);
1036 if (--ch->count < 0) {
1037 printk(KERN_WARNING "moxa_close: bad serial port count, "
1038 "device=%s\n", tty->name);
1044 ch->asyncflags |= ASYNC_CLOSING;
1046 ch->cflag = tty->termios->c_cflag;
1047 if (ch->asyncflags & ASYNC_INITIALIZED) {
1048 moxa_setup_empty_event(tty);
1049 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
1050 del_timer_sync(&moxa_ports[ch->port].emptyTimer);
1053 MoxaPortFlushData(port, 2);
1055 if (tty->driver->flush_buffer)
1056 tty->driver->flush_buffer(tty);
1057 tty_ldisc_flush(tty);
1062 if (ch->blocked_open) {
1063 if (ch->close_delay) {
1064 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
1066 wake_up_interruptible(&ch->open_wait);
1068 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1069 complete_all(&ch->close_wait);
1072 static int moxa_write(struct tty_struct *tty,
1073 const unsigned char *buf, int count)
1075 struct moxa_port *ch;
1077 unsigned long flags;
1079 ch = (struct moxa_port *) tty->driver_data;
1084 spin_lock_irqsave(&moxa_lock, flags);
1085 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
1086 spin_unlock_irqrestore(&moxa_lock, flags);
1088 /*********************************************
1089 if ( !(ch->statusflags & LOWWAIT) &&
1090 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1091 ************************************************/
1092 ch->statusflags |= LOWWAIT;
1096 static int moxa_write_room(struct tty_struct *tty)
1098 struct moxa_port *ch;
1102 ch = (struct moxa_port *) tty->driver_data;
1105 return (MoxaPortTxFree(ch->port));
1108 static void moxa_flush_buffer(struct tty_struct *tty)
1110 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1114 MoxaPortFlushData(ch->port, 1);
1118 static int moxa_chars_in_buffer(struct tty_struct *tty)
1121 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1124 * Sigh...I have to check if driver_data is NULL here, because
1125 * if an open() fails, the TTY subsystem eventually calls
1126 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1127 * routine. And since the open() failed, we return 0 here. TDJ
1131 chars = MoxaPortTxQueue(ch->port);
1134 * Make it possible to wakeup anything waiting for output
1135 * in tty_ioctl.c, etc.
1137 if (!(ch->statusflags & EMPTYWAIT))
1138 moxa_setup_empty_event(tty);
1143 static void moxa_flush_chars(struct tty_struct *tty)
1146 * Don't think I need this, because this is called to empty the TX
1147 * buffer for the 16450, 16550, etc.
1151 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
1153 struct moxa_port *ch;
1155 unsigned long flags;
1157 ch = (struct moxa_port *) tty->driver_data;
1161 spin_lock_irqsave(&moxa_lock, flags);
1162 MoxaPortWriteData(port, &c, 1);
1163 spin_unlock_irqrestore(&moxa_lock, flags);
1164 /************************************************
1165 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1166 *************************************************/
1167 ch->statusflags |= LOWWAIT;
1170 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1172 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1174 int flag = 0, dtr, rts;
1177 if ((port != MAX_PORTS) && (!ch))
1180 MoxaPortGetLineOut(ch->port, &dtr, &rts);
1185 dtr = MoxaPortLineStatus(ch->port);
1195 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1196 unsigned int set, unsigned int clear)
1198 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1203 if ((port != MAX_PORTS) && (!ch))
1206 MoxaPortGetLineOut(ch->port, &dtr, &rts);
1207 if (set & TIOCM_RTS)
1209 if (set & TIOCM_DTR)
1211 if (clear & TIOCM_RTS)
1213 if (clear & TIOCM_DTR)
1215 MoxaPortLineCtrl(ch->port, dtr, rts);
1219 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
1220 unsigned int cmd, unsigned long arg)
1222 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1224 void __user *argp = (void __user *)arg;
1228 if ((port != MAX_PORTS) && (!ch))
1232 case TCSBRK: /* SVID version: non-zero arg --> no break */
1233 retval = tty_check_change(tty);
1236 moxa_setup_empty_event(tty);
1237 tty_wait_until_sent(tty, 0);
1239 MoxaPortSendBreak(ch->port, 0);
1241 case TCSBRKP: /* support for POSIX tcsendbreak() */
1242 retval = tty_check_change(tty);
1245 moxa_setup_empty_event(tty);
1246 tty_wait_until_sent(tty, 0);
1247 MoxaPortSendBreak(ch->port, arg);
1250 return put_user(C_CLOCAL(tty) ? 1 : 0, (int __user *)argp);
1252 if (get_user(retval, (int __user *)argp))
1255 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1256 (arg ? CLOCAL : 0));
1258 ch->asyncflags &= ~ASYNC_CHECK_CD;
1260 ch->asyncflags |= ASYNC_CHECK_CD;
1263 return moxa_get_serial_info(ch, argp);
1266 return moxa_set_serial_info(ch, argp);
1268 retval = MoxaDriverIoctl(cmd, arg, port);
1273 static void moxa_throttle(struct tty_struct *tty)
1275 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1277 ch->statusflags |= THROTTLE;
1280 static void moxa_unthrottle(struct tty_struct *tty)
1282 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1284 ch->statusflags &= ~THROTTLE;
1287 static void moxa_set_termios(struct tty_struct *tty,
1288 struct ktermios *old_termios)
1290 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1294 moxa_set_tty_param(tty, old_termios);
1295 if (!(old_termios->c_cflag & CLOCAL) &&
1296 (tty->termios->c_cflag & CLOCAL))
1297 wake_up_interruptible(&ch->open_wait);
1300 static void moxa_stop(struct tty_struct *tty)
1302 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1306 MoxaPortTxDisable(ch->port);
1307 ch->statusflags |= TXSTOPPED;
1311 static void moxa_start(struct tty_struct *tty)
1313 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1318 if (!(ch->statusflags & TXSTOPPED))
1321 MoxaPortTxEnable(ch->port);
1322 ch->statusflags &= ~TXSTOPPED;
1325 static void moxa_hangup(struct tty_struct *tty)
1327 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1329 moxa_flush_buffer(tty);
1333 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1335 wake_up_interruptible(&ch->open_wait);
1338 static void moxa_poll(unsigned long ignored)
1341 struct moxa_port *ch;
1342 struct tty_struct *tp;
1345 del_timer(&moxaTimer);
1347 if (MoxaDriverPoll() < 0) {
1348 mod_timer(&moxaTimer, jiffies + HZ / 50);
1351 for (card = 0; card < MAX_BOARDS; card++) {
1352 if ((ports = MoxaPortsOfCard(card)) <= 0)
1354 ch = &moxa_ports[card * MAX_PORTS_PER_BOARD];
1355 for (i = 0; i < ports; i++, ch++) {
1356 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
1358 if (!(ch->statusflags & THROTTLE) &&
1359 (MoxaPortRxQueue(ch->port) > 0))
1360 moxa_receive_data(ch);
1361 if ((tp = ch->tty) == 0)
1363 if (ch->statusflags & LOWWAIT) {
1364 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
1366 ch->statusflags &= ~LOWWAIT;
1371 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
1372 tty_insert_flip_char(tp, 0, TTY_BREAK);
1373 tty_schedule_flip(tp);
1375 if (MoxaPortDCDChange(ch->port)) {
1376 if (ch->asyncflags & ASYNC_CHECK_CD) {
1377 if (MoxaPortDCDON(ch->port))
1378 wake_up_interruptible(&ch->open_wait);
1381 wake_up_interruptible(&ch->open_wait);
1382 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1389 mod_timer(&moxaTimer, jiffies + HZ / 50);
1392 /******************************************************************************/
1394 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1396 register struct ktermios *ts;
1397 struct moxa_port *ch;
1398 int rts, cts, txflow, rxflow, xany, baud;
1400 ch = (struct moxa_port *) tty->driver_data;
1402 if (ts->c_cflag & CLOCAL)
1403 ch->asyncflags &= ~ASYNC_CHECK_CD;
1405 ch->asyncflags |= ASYNC_CHECK_CD;
1406 rts = cts = txflow = rxflow = xany = 0;
1407 if (ts->c_cflag & CRTSCTS)
1409 if (ts->c_iflag & IXON)
1411 if (ts->c_iflag & IXOFF)
1413 if (ts->c_iflag & IXANY)
1416 /* Clear the features we don't support */
1417 ts->c_cflag &= ~CMSPAR;
1418 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
1419 baud = MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
1421 baud = tty_termios_baud_rate(old_termios);
1422 /* Not put the baud rate into the termios data */
1423 tty_encode_baud_rate(tty, baud, baud);
1426 static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1427 struct moxa_port *ch)
1429 DECLARE_WAITQUEUE(wait,current);
1430 unsigned long flags;
1432 int do_clocal = C_CLOCAL(tty);
1435 * If the device is in the middle of being closed, then block
1436 * until it's done, and then try again.
1438 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1439 if (ch->asyncflags & ASYNC_CLOSING)
1440 wait_for_completion_interruptible(&ch->close_wait);
1441 #ifdef SERIAL_DO_RESTART
1442 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1445 return (-ERESTARTSYS);
1451 * If non-blocking mode is set, then make the check up front
1454 if (filp->f_flags & O_NONBLOCK) {
1455 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1459 * Block waiting for the carrier detect and the line to become free
1462 add_wait_queue(&ch->open_wait, &wait);
1463 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
1464 ch->port, ch->count);
1465 spin_lock_irqsave(&moxa_lock, flags);
1466 if (!tty_hung_up_p(filp))
1469 spin_unlock_irqrestore(&moxa_lock, flags);
1472 set_current_state(TASK_INTERRUPTIBLE);
1473 if (tty_hung_up_p(filp) ||
1474 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1475 #ifdef SERIAL_DO_RESTART
1476 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1479 retval = -ERESTARTSYS;
1485 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1486 MoxaPortDCDON(ch->port)))
1489 if (signal_pending(current)) {
1490 retval = -ERESTARTSYS;
1495 set_current_state(TASK_RUNNING);
1496 remove_wait_queue(&ch->open_wait, &wait);
1498 spin_lock_irqsave(&moxa_lock, flags);
1499 if (!tty_hung_up_p(filp))
1502 spin_unlock_irqrestore(&moxa_lock, flags);
1503 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
1504 ch->port, ch->count);
1507 /* FIXME: review to see if we need to use set_bit on these */
1508 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1512 static void moxa_setup_empty_event(struct tty_struct *tty)
1514 struct moxa_port *ch = tty->driver_data;
1515 unsigned long flags;
1517 spin_lock_irqsave(&moxa_lock, flags);
1518 ch->statusflags |= EMPTYWAIT;
1519 mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
1520 spin_unlock_irqrestore(&moxa_lock, flags);
1523 static void moxa_check_xmit_empty(unsigned long data)
1525 struct moxa_port *ch;
1527 ch = (struct moxa_port *) data;
1528 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1529 if (MoxaPortTxQueue(ch->port) == 0) {
1530 ch->statusflags &= ~EMPTYWAIT;
1531 tty_wakeup(ch->tty);
1534 mod_timer(&moxa_ports[ch->port].emptyTimer,
1535 round_jiffies(jiffies + HZ));
1537 ch->statusflags &= ~EMPTYWAIT;
1540 static void moxa_shut_down(struct moxa_port *ch)
1542 struct tty_struct *tp;
1544 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1549 MoxaPortDisable(ch->port);
1552 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1554 if (tp->termios->c_cflag & HUPCL)
1555 MoxaPortLineCtrl(ch->port, 0, 0);
1557 ch->asyncflags &= ~ASYNC_INITIALIZED;
1560 static void moxa_receive_data(struct moxa_port *ch)
1562 struct tty_struct *tp;
1563 struct ktermios *ts;
1564 unsigned long flags;
1570 /**************************************************
1571 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1572 *****************************************************/
1574 MoxaPortFlushData(ch->port, 0);
1577 spin_lock_irqsave(&moxa_lock, flags);
1578 MoxaPortReadData(ch->port, tp);
1579 spin_unlock_irqrestore(&moxa_lock, flags);
1580 tty_schedule_flip(tp);
1589 int rxcnt[MAX_PORTS];
1590 int txcnt[MAX_PORTS];
1593 #define DCD_changed 0x01
1594 #define DCD_oldstate 0x80
1596 static int moxaLowWaterChk;
1597 static struct mon_str moxaLog;
1598 static int moxaFuncTout = HZ / 2;
1600 static void moxafunc(void __iomem *, int, ushort);
1601 static void moxa_wait_finish(void __iomem *);
1602 static void moxa_low_water_check(void __iomem *);
1604 /*****************************************************************************
1605 * Driver level functions: *
1606 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1607 * 3. MoxaDriverPoll(void); *
1608 *****************************************************************************/
1610 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1611 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1612 #define MOXA_GETDATACOUNT (MOXA + 23)
1613 #define MOXA_GET_IOQUEUE (MOXA + 27)
1614 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1615 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1616 #define MOXA_GET_MAJOR (MOXA + 63)
1617 #define MOXA_GET_CUMAJOR (MOXA + 64)
1618 #define MOXA_GETMSTATUS (MOXA + 65)
1620 void MoxaPortFlushData(int port, int mode)
1622 void __iomem *ofsAddr;
1623 if ((mode < 0) || (mode > 2))
1625 ofsAddr = moxa_ports[port].tableAddr;
1626 moxafunc(ofsAddr, FC_FlushQueue, mode);
1628 moxa_ports[port].lowChkFlag = 0;
1629 moxa_low_water_check(ofsAddr);
1633 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1637 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1638 void __user *argp = (void __user *)arg;
1640 if (port == MAX_PORTS) {
1641 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_GETDATACOUNT) &&
1642 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1643 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1647 case MOXA_GETDATACOUNT:
1648 moxaLog.tick = jiffies;
1649 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
1652 case MOXA_FLUSH_QUEUE:
1653 MoxaPortFlushData(port, arg);
1655 case MOXA_GET_IOQUEUE: {
1656 struct moxaq_str __user *argm = argp;
1657 struct moxaq_str tmp;
1659 for (i = 0; i < MAX_PORTS; i++, argm++) {
1660 memset(&tmp, 0, sizeof(tmp));
1661 if (moxa_ports[i].chkPort) {
1662 tmp.inq = MoxaPortRxQueue(i);
1663 tmp.outq = MoxaPortTxQueue(i);
1665 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1669 } case MOXA_GET_OQUEUE:
1670 i = MoxaPortTxQueue(port);
1671 return put_user(i, (unsigned long __user *)argp);
1672 case MOXA_GET_IQUEUE:
1673 i = MoxaPortRxQueue(port);
1674 return put_user(i, (unsigned long __user *)argp);
1675 case MOXA_GET_MAJOR:
1676 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1679 case MOXA_GET_CUMAJOR:
1681 if(copy_to_user(argp, &i, sizeof(int)))
1684 case MOXA_GETMSTATUS: {
1685 struct mxser_mstatus __user *argm = argp;
1686 struct mxser_mstatus tmp;
1687 struct moxa_port *p;
1689 for (i = 0; i < MAX_PORTS; i++, argm++) {
1691 memset(&tmp, 0, sizeof(tmp));
1695 status = MoxaPortLineStatus(p->port);
1704 if (!p->tty || !p->tty->termios)
1705 tmp.cflag = p->cflag;
1707 tmp.cflag = p->tty->termios->c_cflag;
1709 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1716 return -ENOIOCTLCMD;
1719 int MoxaDriverPoll(void)
1721 struct moxa_board_conf *brd;
1722 register ushort temp;
1724 void __iomem *ofsAddr;
1730 for (card = 0; card < MAX_BOARDS; card++) {
1731 brd = &moxa_boards[card];
1732 if (brd->loadstat == 0)
1734 if ((ports = brd->numPorts) == 0)
1736 if (readb(brd->intPend) == 0xff) {
1737 ip = brd->intTable + readb(brd->intNdx);
1738 p = card * MAX_PORTS_PER_BOARD;
1740 for (port = 0; port < ports; port += 2, p++) {
1741 if ((temp = readw(ip + port)) != 0) {
1742 writew(0, ip + port);
1743 ofsAddr = moxa_ports[p].tableAddr;
1745 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1746 if (temp & IntrBreak) {
1747 moxa_ports[p].breakCnt++;
1749 if (temp & IntrLine) {
1750 if (readb(ofsAddr + FlagStat) & DCD_state) {
1751 if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
1752 moxa_ports[p].DCDState = (DCD_oldstate |
1755 if (moxa_ports[p].DCDState & DCD_oldstate)
1756 moxa_ports[p].DCDState = DCD_changed;
1761 writeb(0, brd->intPend);
1763 if (moxaLowWaterChk) {
1764 p = card * MAX_PORTS_PER_BOARD;
1765 for (port = 0; port < ports; port++, p++) {
1766 if (moxa_ports[p].lowChkFlag) {
1767 moxa_ports[p].lowChkFlag = 0;
1768 ofsAddr = moxa_ports[p].tableAddr;
1769 moxa_low_water_check(ofsAddr);
1774 moxaLowWaterChk = 0;
1778 /*****************************************************************************
1779 * Card level function: *
1780 * 1. MoxaPortsOfCard(int cardno); *
1781 *****************************************************************************/
1782 int MoxaPortsOfCard(int cardno)
1785 if (moxa_boards[cardno].boardType == 0)
1787 return (moxa_boards[cardno].numPorts);
1790 /*****************************************************************************
1791 * Port level functions: *
1792 * 1. MoxaPortIsValid(int port); *
1793 * 2. MoxaPortEnable(int port); *
1794 * 3. MoxaPortDisable(int port); *
1795 * 4. MoxaPortGetMaxBaud(int port); *
1796 * 6. MoxaPortSetBaud(int port, long baud); *
1797 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1798 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1799 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1800 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1801 * 12. MoxaPortLineStatus(int port); *
1802 * 13. MoxaPortDCDChange(int port); *
1803 * 14. MoxaPortDCDON(int port); *
1804 * 15. MoxaPortFlushData(int port, int mode); *
1805 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1806 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1807 * 20. MoxaPortTxQueue(int port); *
1808 * 21. MoxaPortTxFree(int port); *
1809 * 22. MoxaPortRxQueue(int port); *
1810 * 24. MoxaPortTxDisable(int port); *
1811 * 25. MoxaPortTxEnable(int port); *
1812 * 27. MoxaPortResetBrkCnt(int port); *
1813 * 30. MoxaPortSendBreak(int port, int ticks); *
1814 *****************************************************************************/
1816 * Moxa Port Number Description:
1818 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1819 * the port number using in MOXA driver functions will be 0 to 31 for
1820 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1821 * to 127 for fourth. For example, if you setup three MOXA boards,
1822 * first board is C218, second board is C320-16 and third board is
1823 * C320-32. The port number of first board (C218 - 8 ports) is from
1824 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1825 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1826 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1827 * 127 will be invalid.
1830 * Moxa Functions Description:
1832 * Function 1: Driver initialization routine, this routine must be
1833 * called when initialized driver.
1835 * void MoxaDriverInit();
1838 * Function 2: Moxa driver private IOCTL command processing.
1840 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1842 * unsigned int cmd : IOCTL command
1843 * unsigned long arg : IOCTL argument
1844 * int port : port number (0 - 127)
1851 * Function 3: Moxa driver polling process routine.
1853 * int MoxaDriverPoll(void);
1855 * return: 0 ; polling O.K.
1856 * -1 : no any Moxa card.
1859 * Function 4: Get the ports of this card.
1861 * int MoxaPortsOfCard(int cardno);
1863 * int cardno : card number (0 - 3)
1865 * return: 0 : this card is invalid
1869 * Function 5: Check this port is valid or invalid
1871 * int MoxaPortIsValid(int port);
1872 * int port : port number (0 - 127, ref port description)
1874 * return: 0 : this port is invalid
1875 * 1 : this port is valid
1878 * Function 6: Enable this port to start Tx/Rx data.
1880 * void MoxaPortEnable(int port);
1881 * int port : port number (0 - 127)
1884 * Function 7: Disable this port
1886 * void MoxaPortDisable(int port);
1887 * int port : port number (0 - 127)
1890 * Function 8: Get the maximun available baud rate of this port.
1892 * long MoxaPortGetMaxBaud(int port);
1893 * int port : port number (0 - 127)
1895 * return: 0 : this port is invalid
1896 * 38400/57600/115200 bps
1899 * Function 10: Setting baud rate of this port.
1901 * long MoxaPortSetBaud(int port, long baud);
1902 * int port : port number (0 - 127)
1903 * long baud : baud rate (50 - 115200)
1905 * return: 0 : this port is invalid or baud < 50
1906 * 50 - 115200 : the real baud rate set to the port, if
1907 * the argument baud is large than maximun
1908 * available baud rate, the real setting
1909 * baud rate will be the maximun baud rate.
1912 * Function 12: Configure the port.
1914 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1915 * int port : port number (0 - 127)
1916 * struct ktermios * termio : termio structure pointer
1917 * speed_t baud : baud rate
1919 * return: -1 : this port is invalid or termio == NULL
1923 * Function 13: Get the DTR/RTS state of this port.
1925 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1926 * int port : port number (0 - 127)
1927 * int * dtrState : pointer to INT to receive the current DTR
1928 * state. (if NULL, this function will not
1929 * write to this address)
1930 * int * rtsState : pointer to INT to receive the current RTS
1931 * state. (if NULL, this function will not
1932 * write to this address)
1934 * return: -1 : this port is invalid
1938 * Function 14: Setting the DTR/RTS output state of this port.
1940 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1941 * int port : port number (0 - 127)
1942 * int dtrState : DTR output state (0: off, 1: on)
1943 * int rtsState : RTS output state (0: off, 1: on)
1946 * Function 15: Setting the flow control of this port.
1948 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1949 * int txFlow,int xany);
1950 * int port : port number (0 - 127)
1951 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1952 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1953 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1954 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1955 * int xany : S/W XANY flow control (0: no, 1: yes)
1958 * Function 16: Get ths line status of this port
1960 * int MoxaPortLineStatus(int port);
1961 * int port : port number (0 - 127)
1963 * return: Bit 0 - CTS state (0: off, 1: on)
1964 * Bit 1 - DSR state (0: off, 1: on)
1965 * Bit 2 - DCD state (0: off, 1: on)
1968 * Function 17: Check the DCD state has changed since the last read
1971 * int MoxaPortDCDChange(int port);
1972 * int port : port number (0 - 127)
1974 * return: 0 : no changed
1975 * 1 : DCD has changed
1978 * Function 18: Check ths current DCD state is ON or not.
1980 * int MoxaPortDCDON(int port);
1981 * int port : port number (0 - 127)
1983 * return: 0 : DCD off
1987 * Function 19: Flush the Rx/Tx buffer data of this port.
1989 * void MoxaPortFlushData(int port, int mode);
1990 * int port : port number (0 - 127)
1992 * 0 : flush the Rx buffer
1993 * 1 : flush the Tx buffer
1994 * 2 : flush the Rx and Tx buffer
1997 * Function 20: Write data.
1999 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
2000 * int port : port number (0 - 127)
2001 * unsigned char * buffer : pointer to write data buffer.
2002 * int length : write data length
2004 * return: 0 - length : real write data length
2007 * Function 21: Read data.
2009 * int MoxaPortReadData(int port, struct tty_struct *tty);
2010 * int port : port number (0 - 127)
2011 * struct tty_struct *tty : tty for data
2013 * return: 0 - length : real read data length
2016 * Function 24: Get the Tx buffer current queued data bytes
2018 * int MoxaPortTxQueue(int port);
2019 * int port : port number (0 - 127)
2021 * return: .. : Tx buffer current queued data bytes
2024 * Function 25: Get the Tx buffer current free space
2026 * int MoxaPortTxFree(int port);
2027 * int port : port number (0 - 127)
2029 * return: .. : Tx buffer current free space
2032 * Function 26: Get the Rx buffer current queued data bytes
2034 * int MoxaPortRxQueue(int port);
2035 * int port : port number (0 - 127)
2037 * return: .. : Rx buffer current queued data bytes
2040 * Function 28: Disable port data transmission.
2042 * void MoxaPortTxDisable(int port);
2043 * int port : port number (0 - 127)
2046 * Function 29: Enable port data transmission.
2048 * void MoxaPortTxEnable(int port);
2049 * int port : port number (0 - 127)
2052 * Function 31: Get the received BREAK signal count and reset it.
2054 * int MoxaPortResetBrkCnt(int port);
2055 * int port : port number (0 - 127)
2057 * return: 0 - .. : BREAK signal count
2060 * Function 34: Send out a BREAK signal.
2062 * void MoxaPortSendBreak(int port, int ms100);
2063 * int port : port number (0 - 127)
2064 * int ms100 : break signal time interval.
2065 * unit: 100 mini-second. if ms100 == 0, it will
2066 * send out a about 250 ms BREAK signal.
2069 int MoxaPortIsValid(int port)
2073 if (moxa_ports[port].chkPort == 0)
2078 void MoxaPortEnable(int port)
2080 void __iomem *ofsAddr;
2081 int MoxaPortLineStatus(int);
2082 short lowwater = 512;
2084 ofsAddr = moxa_ports[port].tableAddr;
2085 writew(lowwater, ofsAddr + Low_water);
2086 moxa_ports[port].breakCnt = 0;
2087 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2088 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2089 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2091 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2094 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2095 moxafunc(ofsAddr, FC_FlushQueue, 2);
2097 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2098 MoxaPortLineStatus(port);
2101 void MoxaPortDisable(int port)
2103 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
2105 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
2106 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2107 writew(0, ofsAddr + HostStat);
2108 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2111 long MoxaPortGetMaxBaud(int port)
2113 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2114 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
2121 long MoxaPortSetBaud(int port, long baud)
2123 void __iomem *ofsAddr;
2127 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2129 ofsAddr = moxa_ports[port].tableAddr;
2133 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2134 else if (max == 57600L)
2135 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2137 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2139 moxafunc(ofsAddr, FC_SetBaud, val);
2141 moxa_ports[port].curBaud = baud;
2145 int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
2147 void __iomem *ofsAddr;
2151 if (moxa_ports[port].chkPort == 0 || termio == 0)
2153 ofsAddr = moxa_ports[port].tableAddr;
2154 cflag = termio->c_cflag; /* termio->c_cflag */
2156 mode = termio->c_cflag & CSIZE;
2159 else if (mode == CS6)
2161 else if (mode == CS7)
2163 else if (mode == CS8)
2166 if (termio->c_cflag & CSTOPB) {
2174 if (termio->c_cflag & PARENB) {
2175 if (termio->c_cflag & PARODD)
2182 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2184 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2185 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2186 if (baud >= 921600L)
2189 baud = MoxaPortSetBaud(port, baud);
2191 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2192 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2193 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2194 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2195 moxa_wait_finish(ofsAddr);
2201 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2204 if (!MoxaPortIsValid(port))
2207 if (moxa_ports[port].lineCtrl & DTR_ON)
2213 if (moxa_ports[port].lineCtrl & RTS_ON)
2221 void MoxaPortLineCtrl(int port, int dtr, int rts)
2223 void __iomem *ofsAddr;
2226 ofsAddr = moxa_ports[port].tableAddr;
2232 moxa_ports[port].lineCtrl = mode;
2233 moxafunc(ofsAddr, FC_LineControl, mode);
2236 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2238 void __iomem *ofsAddr;
2241 ofsAddr = moxa_ports[port].tableAddr;
2244 mode |= RTS_FlowCtl;
2246 mode |= CTS_FlowCtl;
2253 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2256 int MoxaPortLineStatus(int port)
2258 void __iomem *ofsAddr;
2261 ofsAddr = moxa_ports[port].tableAddr;
2262 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2263 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2264 moxafunc(ofsAddr, FC_LineStatus, 0);
2265 val = readw(ofsAddr + FuncArg);
2267 val = readw(ofsAddr + FlagStat) >> 4;
2272 if ((moxa_ports[port].DCDState & DCD_oldstate) == 0)
2273 moxa_ports[port].DCDState = (DCD_oldstate | DCD_changed);
2275 if (moxa_ports[port].DCDState & DCD_oldstate)
2276 moxa_ports[port].DCDState = DCD_changed;
2282 int MoxaPortDCDChange(int port)
2286 if (moxa_ports[port].chkPort == 0)
2288 n = moxa_ports[port].DCDState;
2289 moxa_ports[port].DCDState &= ~DCD_changed;
2294 int MoxaPortDCDON(int port)
2298 if (moxa_ports[port].chkPort == 0)
2300 if (moxa_ports[port].DCDState & DCD_oldstate)
2307 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2312 ushort head, tx_mask, spage, epage;
2313 ushort pageno, pageofs, bufhead;
2314 void __iomem *baseAddr, *ofsAddr, *ofs;
2316 ofsAddr = moxa_ports[port].tableAddr;
2317 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
2318 tx_mask = readw(ofsAddr + TX_mask);
2319 spage = readw(ofsAddr + Page_txb);
2320 epage = readw(ofsAddr + EndPage_txb);
2321 tail = readw(ofsAddr + TXwptr);
2322 head = readw(ofsAddr + TXrptr);
2323 c = (head > tail) ? (head - tail - 1)
2324 : (head - tail + tx_mask);
2327 moxaLog.txcnt[port] += c;
2329 if (spage == epage) {
2330 bufhead = readw(ofsAddr + Ofs_txb);
2331 writew(spage, baseAddr + Control_reg);
2334 len = head - tail - 1;
2336 len = tx_mask + 1 - tail;
2337 len = (c > len) ? len : c;
2338 ofs = baseAddr + DynPage_addr + bufhead + tail;
2339 for (i = 0; i < len; i++)
2340 writeb(*buffer++, ofs + i);
2341 tail = (tail + len) & tx_mask;
2344 writew(tail, ofsAddr + TXwptr);
2347 pageno = spage + (tail >> 13);
2348 pageofs = tail & Page_mask;
2350 cnt = Page_size - pageofs;
2354 writeb(pageno, baseAddr + Control_reg);
2355 ofs = baseAddr + DynPage_addr + pageofs;
2356 for (i = 0; i < cnt; i++)
2357 writeb(*buffer++, ofs + i);
2359 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2362 if (++pageno == epage)
2367 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2371 int MoxaPortReadData(int port, struct tty_struct *tty)
2373 register ushort head, pageofs;
2374 int i, count, cnt, len, total, remain;
2375 ushort tail, rx_mask, spage, epage;
2376 ushort pageno, bufhead;
2377 void __iomem *baseAddr, *ofsAddr, *ofs;
2379 ofsAddr = moxa_ports[port].tableAddr;
2380 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
2381 head = readw(ofsAddr + RXrptr);
2382 tail = readw(ofsAddr + RXwptr);
2383 rx_mask = readw(ofsAddr + RX_mask);
2384 spage = readw(ofsAddr + Page_rxb);
2385 epage = readw(ofsAddr + EndPage_rxb);
2386 count = (tail >= head) ? (tail - head)
2387 : (tail - head + rx_mask + 1);
2392 remain = count - total;
2393 moxaLog.rxcnt[port] += total;
2395 if (spage == epage) {
2396 bufhead = readw(ofsAddr + Ofs_rxb);
2397 writew(spage, baseAddr + Control_reg);
2402 len = rx_mask + 1 - head;
2403 len = (count > len) ? len : count;
2404 ofs = baseAddr + DynPage_addr + bufhead + head;
2405 for (i = 0; i < len; i++)
2406 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2407 head = (head + len) & rx_mask;
2410 writew(head, ofsAddr + RXrptr);
2413 pageno = spage + (head >> 13);
2414 pageofs = head & Page_mask;
2416 cnt = Page_size - pageofs;
2420 writew(pageno, baseAddr + Control_reg);
2421 ofs = baseAddr + DynPage_addr + pageofs;
2422 for (i = 0; i < cnt; i++)
2423 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2425 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2428 if (++pageno == epage)
2433 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2434 moxaLowWaterChk = 1;
2435 moxa_ports[port].lowChkFlag = 1;
2441 int MoxaPortTxQueue(int port)
2443 void __iomem *ofsAddr;
2444 ushort rptr, wptr, mask;
2447 ofsAddr = moxa_ports[port].tableAddr;
2448 rptr = readw(ofsAddr + TXrptr);
2449 wptr = readw(ofsAddr + TXwptr);
2450 mask = readw(ofsAddr + TX_mask);
2451 len = (wptr - rptr) & mask;
2455 int MoxaPortTxFree(int port)
2457 void __iomem *ofsAddr;
2458 ushort rptr, wptr, mask;
2461 ofsAddr = moxa_ports[port].tableAddr;
2462 rptr = readw(ofsAddr + TXrptr);
2463 wptr = readw(ofsAddr + TXwptr);
2464 mask = readw(ofsAddr + TX_mask);
2465 len = mask - ((wptr - rptr) & mask);
2469 int MoxaPortRxQueue(int port)
2471 void __iomem *ofsAddr;
2472 ushort rptr, wptr, mask;
2475 ofsAddr = moxa_ports[port].tableAddr;
2476 rptr = readw(ofsAddr + RXrptr);
2477 wptr = readw(ofsAddr + RXwptr);
2478 mask = readw(ofsAddr + RX_mask);
2479 len = (wptr - rptr) & mask;
2484 void MoxaPortTxDisable(int port)
2486 void __iomem *ofsAddr;
2488 ofsAddr = moxa_ports[port].tableAddr;
2489 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2492 void MoxaPortTxEnable(int port)
2494 void __iomem *ofsAddr;
2496 ofsAddr = moxa_ports[port].tableAddr;
2497 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2501 int MoxaPortResetBrkCnt(int port)
2504 cnt = moxa_ports[port].breakCnt;
2505 moxa_ports[port].breakCnt = 0;
2510 void MoxaPortSendBreak(int port, int ms100)
2512 void __iomem *ofsAddr;
2514 ofsAddr = moxa_ports[port].tableAddr;
2516 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2519 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2522 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2525 static int moxa_get_serial_info(struct moxa_port *info,
2526 struct serial_struct __user *retinfo)
2528 struct serial_struct tmp;
2530 memset(&tmp, 0, sizeof(tmp));
2531 tmp.type = info->type;
2532 tmp.line = info->port;
2535 tmp.flags = info->asyncflags;
2536 tmp.baud_base = 921600;
2537 tmp.close_delay = info->close_delay;
2538 tmp.closing_wait = info->closing_wait;
2539 tmp.custom_divisor = 0;
2541 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2547 static int moxa_set_serial_info(struct moxa_port *info,
2548 struct serial_struct __user *new_info)
2550 struct serial_struct new_serial;
2552 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2555 if ((new_serial.irq != 0) ||
2556 (new_serial.port != 0) ||
2557 // (new_serial.type != info->type) ||
2558 (new_serial.custom_divisor != 0) ||
2559 (new_serial.baud_base != 921600))
2562 if (!capable(CAP_SYS_ADMIN)) {
2563 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2564 (info->asyncflags & ~ASYNC_USR_MASK)))
2567 info->close_delay = new_serial.close_delay * HZ / 100;
2568 info->closing_wait = new_serial.closing_wait * HZ / 100;
2571 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2572 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2574 if (new_serial.type == PORT_16550A) {
2575 MoxaSetFifo(info->port, 1);
2577 MoxaSetFifo(info->port, 0);
2580 info->type = new_serial.type;
2586 /*****************************************************************************
2587 * Static local functions: *
2588 *****************************************************************************/
2589 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2592 writew(arg, ofsAddr + FuncArg);
2593 writew(cmd, ofsAddr + FuncCode);
2594 moxa_wait_finish(ofsAddr);
2597 static void moxa_wait_finish(void __iomem *ofsAddr)
2602 while (readw(ofsAddr + FuncCode) != 0) {
2604 if ((j - i) > moxaFuncTout) {
2610 static void moxa_low_water_check(void __iomem *ofsAddr)
2613 ushort rptr, wptr, mask;
2615 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2616 rptr = readw(ofsAddr + RXrptr);
2617 wptr = readw(ofsAddr + RXwptr);
2618 mask = readw(ofsAddr + RX_mask);
2619 len = (wptr - rptr) & mask;
2620 if (len <= Low_water)
2621 moxafunc(ofsAddr, FC_SendXon, 0);
2625 static void MoxaSetFifo(int port, int enable)
2627 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
2630 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2631 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2633 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2634 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);