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>
46 #include <asm/system.h>
48 #include <asm/uaccess.h>
52 #define MOXA_VERSION "5.1k"
54 #define MOXA_FW_HDRLEN 32
58 #define MAX_BOARDS 4 /* Don't change this value */
59 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
60 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
63 * Define the Moxa PCI vendor and device IDs.
65 #define MOXA_BUS_TYPE_ISA 0
66 #define MOXA_BUS_TYPE_PCI 1
69 MOXA_BOARD_C218_PCI = 1,
76 static char *moxa_brdname[] =
78 "C218 Turbo PCI series",
79 "C218 Turbo ISA series",
80 "C320 Turbo PCI series",
81 "C320 Turbo ISA series",
86 static struct pci_device_id moxa_pcibrds[] = {
87 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
88 .driver_data = MOXA_BOARD_C218_PCI },
89 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
90 .driver_data = MOXA_BOARD_C320_PCI },
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
92 .driver_data = MOXA_BOARD_CP204J },
95 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
96 #endif /* CONFIG_PCI */
100 static struct moxa_board_conf {
107 struct moxa_port *ports;
109 void __iomem *basemem;
110 void __iomem *intNdx;
111 void __iomem *intPend;
112 void __iomem *intTable;
113 } moxa_boards[MAX_BOARDS];
115 struct mxser_mstatus {
129 struct moxa_board_conf *board;
130 struct tty_struct *tty;
131 void __iomem *tableAddr;
138 unsigned long statusflags;
139 wait_queue_head_t open_wait;
148 int rxcnt[MAX_PORTS];
149 int txcnt[MAX_PORTS];
153 #define TXSTOPPED 0x1
155 #define EMPTYWAIT 0x4
158 #define SERIAL_DO_RESTART
160 #define WAKEUP_CHARS 256
162 static int ttymajor = MOXAMAJOR;
163 static struct mon_str moxaLog;
164 static unsigned int moxaFuncTout = HZ / 2;
165 static unsigned int moxaLowWaterChk;
166 static DEFINE_MUTEX(moxa_openlock);
167 /* Variables for insmod */
169 static unsigned long baseaddr[MAX_BOARDS];
170 static unsigned int type[MAX_BOARDS];
171 static unsigned int numports[MAX_BOARDS];
174 MODULE_AUTHOR("William Chen");
175 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
176 MODULE_LICENSE("GPL");
178 module_param_array(type, uint, NULL, 0);
179 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
180 module_param_array(baseaddr, ulong, NULL, 0);
181 MODULE_PARM_DESC(baseaddr, "base address");
182 module_param_array(numports, uint, NULL, 0);
183 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
185 module_param(ttymajor, int, 0);
190 static int moxa_open(struct tty_struct *, struct file *);
191 static void moxa_close(struct tty_struct *, struct file *);
192 static int moxa_write(struct tty_struct *, const unsigned char *, int);
193 static int moxa_write_room(struct tty_struct *);
194 static void moxa_flush_buffer(struct tty_struct *);
195 static int moxa_chars_in_buffer(struct tty_struct *);
196 static void moxa_flush_chars(struct tty_struct *);
197 static void moxa_put_char(struct tty_struct *, unsigned char);
198 static void moxa_throttle(struct tty_struct *);
199 static void moxa_unthrottle(struct tty_struct *);
200 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
201 static void moxa_stop(struct tty_struct *);
202 static void moxa_start(struct tty_struct *);
203 static void moxa_hangup(struct tty_struct *);
204 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
205 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
206 unsigned int set, unsigned int clear);
207 static void moxa_poll(unsigned long);
208 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
209 static void moxa_setup_empty_event(struct tty_struct *);
210 static void moxa_shut_down(struct moxa_port *);
212 * moxa board interface functions:
214 static void MoxaPortEnable(struct moxa_port *);
215 static void MoxaPortDisable(struct moxa_port *);
216 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
217 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
218 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
219 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
220 static int MoxaPortLineStatus(struct moxa_port *);
221 static void MoxaPortFlushData(struct moxa_port *, int);
222 static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int);
223 static int MoxaPortReadData(struct moxa_port *);
224 static int MoxaPortTxQueue(struct moxa_port *);
225 static int MoxaPortRxQueue(struct moxa_port *);
226 static int MoxaPortTxFree(struct moxa_port *);
227 static void MoxaPortTxDisable(struct moxa_port *);
228 static void MoxaPortTxEnable(struct moxa_port *);
229 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
230 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
231 static void MoxaSetFifo(struct moxa_port *port, int enable);
237 static void moxa_wait_finish(void __iomem *ofsAddr)
239 unsigned long end = jiffies + moxaFuncTout;
241 while (readw(ofsAddr + FuncCode) != 0)
242 if (time_after(jiffies, end))
244 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
245 printk(KERN_WARNING "moxa function expired\n");
248 static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
250 writew(arg, ofsAddr + FuncArg);
251 writew(cmd, ofsAddr + FuncCode);
252 moxa_wait_finish(ofsAddr);
255 static void moxa_low_water_check(void __iomem *ofsAddr)
257 u16 rptr, wptr, mask, len;
259 if (readb(ofsAddr + FlagStat) & Xoff_state) {
260 rptr = readw(ofsAddr + RXrptr);
261 wptr = readw(ofsAddr + RXwptr);
262 mask = readw(ofsAddr + RX_mask);
263 len = (wptr - rptr) & mask;
264 if (len <= Low_water)
265 moxafunc(ofsAddr, FC_SendXon, 0);
273 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
274 unsigned int cmd, unsigned long arg)
276 struct moxa_port *ch = tty->driver_data;
277 void __user *argp = (void __user *)arg;
280 if (tty->index == MAX_PORTS) {
281 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
282 cmd != MOXA_GETMSTATUS)
288 case MOXA_GETDATACOUNT:
289 moxaLog.tick = jiffies;
290 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
293 case MOXA_FLUSH_QUEUE:
294 MoxaPortFlushData(ch, arg);
296 case MOXA_GET_IOQUEUE: {
297 struct moxaq_str __user *argm = argp;
298 struct moxaq_str tmp;
302 mutex_lock(&moxa_openlock);
303 for (i = 0; i < MAX_BOARDS; i++) {
304 p = moxa_boards[i].ports;
305 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
306 memset(&tmp, 0, sizeof(tmp));
307 if (moxa_boards[i].ready) {
308 tmp.inq = MoxaPortRxQueue(p);
309 tmp.outq = MoxaPortTxQueue(p);
311 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
312 mutex_unlock(&moxa_openlock);
317 mutex_unlock(&moxa_openlock);
319 } case MOXA_GET_OQUEUE:
320 status = MoxaPortTxQueue(ch);
321 ret = put_user(status, (unsigned long __user *)argp);
323 case MOXA_GET_IQUEUE:
324 status = MoxaPortRxQueue(ch);
325 ret = put_user(status, (unsigned long __user *)argp);
327 case MOXA_GETMSTATUS: {
328 struct mxser_mstatus __user *argm = argp;
329 struct mxser_mstatus tmp;
333 mutex_lock(&moxa_openlock);
334 for (i = 0; i < MAX_BOARDS; i++) {
335 p = moxa_boards[i].ports;
336 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
337 memset(&tmp, 0, sizeof(tmp));
338 if (!moxa_boards[i].ready)
341 status = MoxaPortLineStatus(p);
349 if (!p->tty || !p->tty->termios)
350 tmp.cflag = p->cflag;
352 tmp.cflag = p->tty->termios->c_cflag;
354 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
355 mutex_unlock(&moxa_openlock);
360 mutex_unlock(&moxa_openlock);
364 mutex_lock(&moxa_openlock);
365 ret = moxa_get_serial_info(ch, argp);
366 mutex_unlock(&moxa_openlock);
369 mutex_lock(&moxa_openlock);
370 ret = moxa_set_serial_info(ch, argp);
371 mutex_unlock(&moxa_openlock);
379 static void moxa_break_ctl(struct tty_struct *tty, int state)
381 struct moxa_port *port = tty->driver_data;
383 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
387 static const struct tty_operations moxa_ops = {
391 .write_room = moxa_write_room,
392 .flush_buffer = moxa_flush_buffer,
393 .chars_in_buffer = moxa_chars_in_buffer,
394 .flush_chars = moxa_flush_chars,
395 .put_char = moxa_put_char,
397 .throttle = moxa_throttle,
398 .unthrottle = moxa_unthrottle,
399 .set_termios = moxa_set_termios,
402 .hangup = moxa_hangup,
403 .break_ctl = moxa_break_ctl,
404 .tiocmget = moxa_tiocmget,
405 .tiocmset = moxa_tiocmset,
408 static struct tty_driver *moxaDriver;
409 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
410 static DEFINE_SPINLOCK(moxa_lock);
416 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
418 switch (brd->boardType) {
419 case MOXA_BOARD_C218_ISA:
420 case MOXA_BOARD_C218_PCI:
424 case MOXA_BOARD_CP204J:
438 static int moxa_check_fw(const void *ptr)
440 const __le16 *lptr = ptr;
442 if (*lptr != cpu_to_le16(0x7980))
448 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
451 void __iomem *baseAddr = brd->basemem;
454 writeb(HW_reset, baseAddr + Control_reg); /* reset */
456 memset_io(baseAddr, 0, 4096);
457 memcpy_toio(baseAddr, buf, len); /* download BIOS */
458 writeb(0, baseAddr + Control_reg); /* restart */
462 switch (brd->boardType) {
463 case MOXA_BOARD_C218_ISA:
464 case MOXA_BOARD_C218_PCI:
465 tmp = readw(baseAddr + C218_key);
466 if (tmp != C218_KeyCode)
469 case MOXA_BOARD_CP204J:
470 tmp = readw(baseAddr + C218_key);
471 if (tmp != CP204J_KeyCode)
475 tmp = readw(baseAddr + C320_key);
476 if (tmp != C320_KeyCode)
478 tmp = readw(baseAddr + C320_status);
479 if (tmp != STS_init) {
480 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
481 "module not found\n");
489 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
493 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
496 void __iomem *baseAddr = brd->basemem;
499 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
503 writew(len - 7168 - 2, baseAddr + C320bapi_len);
504 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
505 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
506 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
507 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
512 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
515 void __iomem *baseAddr = brd->basemem;
516 const u16 *uptr = ptr;
517 size_t wlen, len2, j;
518 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
519 unsigned int i, retry, c320;
522 c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
523 brd->boardType == MOXA_BOARD_C320_ISA;
524 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
527 switch (brd->boardType) {
528 case MOXA_BOARD_CP204J:
529 case MOXA_BOARD_C218_ISA:
530 case MOXA_BOARD_C218_PCI:
532 loadbuf = C218_LoadBuf;
533 loadlen = C218DLoad_len;
534 checksum = C218check_sum;
535 checksum_ok = C218chksum_ok;
539 keycode = C320_KeyCode;
540 loadbuf = C320_LoadBuf;
541 loadlen = C320DLoad_len;
542 checksum = C320check_sum;
543 checksum_ok = C320chksum_ok;
549 for (i = 0; i < wlen; i++)
550 usum += le16_to_cpu(uptr[i]);
556 len2 = (wlen > 2048) ? 2048 : wlen;
558 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
561 writew(len2, baseAddr + loadlen);
562 writew(0, baseAddr + key);
563 for (i = 0; i < 100; i++) {
564 if (readw(baseAddr + key) == keycode)
568 if (readw(baseAddr + key) != keycode)
571 writew(0, baseAddr + loadlen);
572 writew(usum, baseAddr + checksum);
573 writew(0, baseAddr + key);
574 for (i = 0; i < 100; i++) {
575 if (readw(baseAddr + key) == keycode)
580 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
581 if (readb(baseAddr + checksum_ok) != 1)
584 writew(0, baseAddr + key);
585 for (i = 0; i < 600; i++) {
586 if (readw(baseAddr + Magic_no) == Magic_code)
590 if (readw(baseAddr + Magic_no) != Magic_code)
594 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
595 writew(0x3800, baseAddr + TMS320_PORT1);
596 writew(0x3900, baseAddr + TMS320_PORT2);
597 writew(28499, baseAddr + TMS320_CLOCK);
599 writew(0x3200, baseAddr + TMS320_PORT1);
600 writew(0x3400, baseAddr + TMS320_PORT2);
601 writew(19999, baseAddr + TMS320_CLOCK);
604 writew(1, baseAddr + Disable_IRQ);
605 writew(0, baseAddr + Magic_no);
606 for (i = 0; i < 500; i++) {
607 if (readw(baseAddr + Magic_no) == Magic_code)
611 if (readw(baseAddr + Magic_no) != Magic_code)
615 j = readw(baseAddr + Module_cnt);
618 brd->numPorts = j * 8;
619 writew(j, baseAddr + Module_no);
620 writew(0, baseAddr + Magic_no);
621 for (i = 0; i < 600; i++) {
622 if (readw(baseAddr + Magic_no) == Magic_code)
626 if (readw(baseAddr + Magic_no) != Magic_code)
629 brd->intNdx = baseAddr + IRQindex;
630 brd->intPend = baseAddr + IRQpending;
631 brd->intTable = baseAddr + IRQtable;
636 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
639 void __iomem *ofsAddr, *baseAddr = brd->basemem;
640 struct moxa_port *port;
644 printk(KERN_ERR "MOXA: bios length is not even\n");
648 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
652 switch (brd->boardType) {
653 case MOXA_BOARD_C218_ISA:
654 case MOXA_BOARD_C218_PCI:
655 case MOXA_BOARD_CP204J:
657 for (i = 0; i < brd->numPorts; i++, port++) {
660 port->tableAddr = baseAddr + Extern_table +
662 ofsAddr = port->tableAddr;
663 writew(C218rx_mask, ofsAddr + RX_mask);
664 writew(C218tx_mask, ofsAddr + TX_mask);
665 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
666 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
668 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
669 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
675 for (i = 0; i < brd->numPorts; i++, port++) {
678 port->tableAddr = baseAddr + Extern_table +
680 ofsAddr = port->tableAddr;
681 switch (brd->numPorts) {
683 writew(C320p8rx_mask, ofsAddr + RX_mask);
684 writew(C320p8tx_mask, ofsAddr + TX_mask);
685 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
686 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
687 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
688 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
692 writew(C320p16rx_mask, ofsAddr + RX_mask);
693 writew(C320p16tx_mask, ofsAddr + TX_mask);
694 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
695 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
696 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
697 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
701 writew(C320p24rx_mask, ofsAddr + RX_mask);
702 writew(C320p24tx_mask, ofsAddr + TX_mask);
703 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
704 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
705 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
706 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
709 writew(C320p32rx_mask, ofsAddr + RX_mask);
710 writew(C320p32tx_mask, ofsAddr + TX_mask);
711 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
712 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
713 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
714 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
715 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
724 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
726 void *ptr = fw->data;
730 unsigned int a, lenp, lencnt;
733 __le32 magic; /* 0x34303430 */
735 u8 type; /* UNIX = 3 */
736 u8 model; /* C218T=1, C320T=2, CP204=3 */
741 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
743 if (fw->size < MOXA_FW_HDRLEN) {
744 strcpy(rsn, "too short (even header won't fit)");
747 if (hdr->magic != cpu_to_le32(0x30343034)) {
748 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
751 if (hdr->type != 3) {
752 sprintf(rsn, "not for linux, type is %u", hdr->type);
755 if (moxa_check_fw_model(brd, hdr->model)) {
756 sprintf(rsn, "not for this card, model is %u", hdr->model);
760 len = MOXA_FW_HDRLEN;
761 lencnt = hdr->model == 2 ? 5 : 3;
762 for (a = 0; a < ARRAY_SIZE(lens); a++) {
763 lens[a] = le16_to_cpu(hdr->len[a]);
764 if (lens[a] && len + lens[a] <= fw->size &&
765 moxa_check_fw(&fw->data[len]))
766 printk(KERN_WARNING "MOXA firmware: unexpected input "
767 "at offset %u, but going on\n", (u32)len);
768 if (!lens[a] && a < lencnt) {
769 sprintf(rsn, "too few entries in fw file");
775 if (len != fw->size) {
776 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
781 ptr += MOXA_FW_HDRLEN;
784 strcpy(rsn, "read above");
786 ret = moxa_load_bios(brd, ptr, lens[lenp]);
790 /* we skip the tty section (lens[1]), since we don't need it */
791 ptr += lens[lenp] + lens[lenp + 1];
792 lenp += 2; /* comm */
794 if (hdr->model == 2) {
795 ret = moxa_load_320b(brd, ptr, lens[lenp]);
798 /* skip another tty */
799 ptr += lens[lenp] + lens[lenp + 1];
803 ret = moxa_load_code(brd, ptr, lens[lenp]);
809 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
813 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
815 const struct firmware *fw;
821 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
823 if (brd->ports == NULL) {
824 printk(KERN_ERR "cannot allocate memory for ports\n");
829 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
830 p->type = PORT_16550A;
831 p->close_delay = 5 * HZ / 10;
832 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
833 init_waitqueue_head(&p->open_wait);
836 switch (brd->boardType) {
837 case MOXA_BOARD_C218_ISA:
838 case MOXA_BOARD_C218_PCI:
839 file = "c218tunx.cod";
841 case MOXA_BOARD_CP204J:
842 file = "cp204unx.cod";
845 file = "c320tunx.cod";
849 ret = request_firmware(&fw, file, dev);
851 printk(KERN_ERR "request_firmware failed\n");
855 ret = moxa_load_fw(brd, fw);
857 release_firmware(fw);
862 spin_lock_bh(&moxa_lock);
864 if (!timer_pending(&moxaTimer))
865 mod_timer(&moxaTimer, jiffies + HZ / 50);
866 spin_unlock_bh(&moxa_lock);
875 static void moxa_board_deinit(struct moxa_board_conf *brd)
877 unsigned int a, opened;
879 mutex_lock(&moxa_openlock);
880 spin_lock_bh(&moxa_lock);
882 spin_unlock_bh(&moxa_lock);
884 /* pci hot-un-plug support */
885 for (a = 0; a < brd->numPorts; a++)
886 if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
887 tty_hangup(brd->ports[a].tty);
890 for (a = 0; a < brd->numPorts; a++)
891 if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
893 mutex_unlock(&moxa_openlock);
897 mutex_lock(&moxa_openlock);
900 iounmap(brd->basemem);
906 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
907 const struct pci_device_id *ent)
909 struct moxa_board_conf *board;
911 int board_type = ent->driver_data;
914 retval = pci_enable_device(pdev);
916 dev_err(&pdev->dev, "can't enable pci device\n");
920 for (i = 0; i < MAX_BOARDS; i++)
921 if (moxa_boards[i].basemem == NULL)
925 if (i >= MAX_BOARDS) {
926 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
927 "found. Board is ignored.\n", MAX_BOARDS);
931 board = &moxa_boards[i];
933 retval = pci_request_region(pdev, 2, "moxa-base");
935 dev_err(&pdev->dev, "can't request pci region 2\n");
939 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
940 if (board->basemem == NULL) {
941 dev_err(&pdev->dev, "can't remap io space 2\n");
945 board->boardType = board_type;
946 switch (board_type) {
947 case MOXA_BOARD_C218_ISA:
948 case MOXA_BOARD_C218_PCI:
952 case MOXA_BOARD_CP204J:
959 board->busType = MOXA_BUS_TYPE_PCI;
961 retval = moxa_init_board(board, &pdev->dev);
965 pci_set_drvdata(pdev, board);
969 iounmap(board->basemem);
970 board->basemem = NULL;
972 pci_release_region(pdev, 2);
977 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
979 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
981 moxa_board_deinit(brd);
983 pci_release_region(pdev, 2);
986 static struct pci_driver moxa_pci_driver = {
988 .id_table = moxa_pcibrds,
989 .probe = moxa_pci_probe,
990 .remove = __devexit_p(moxa_pci_remove)
992 #endif /* CONFIG_PCI */
994 static int __init moxa_init(void)
996 unsigned int isabrds = 0;
999 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1001 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1005 moxaDriver->owner = THIS_MODULE;
1006 moxaDriver->name = "ttyMX";
1007 moxaDriver->major = ttymajor;
1008 moxaDriver->minor_start = 0;
1009 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1010 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1011 moxaDriver->init_termios = tty_std_termios;
1012 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1013 moxaDriver->init_termios.c_ispeed = 9600;
1014 moxaDriver->init_termios.c_ospeed = 9600;
1015 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1016 tty_set_operations(moxaDriver, &moxa_ops);
1018 if (tty_register_driver(moxaDriver)) {
1019 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1020 put_tty_driver(moxaDriver);
1024 /* Find the boards defined from module args. */
1027 struct moxa_board_conf *brd = moxa_boards;
1029 for (i = 0; i < MAX_BOARDS; i++) {
1032 if (type[i] == MOXA_BOARD_C218_ISA ||
1033 type[i] == MOXA_BOARD_C320_ISA) {
1034 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1035 isabrds + 1, moxa_brdname[type[i] - 1],
1037 brd->boardType = type[i];
1038 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1040 brd->busType = MOXA_BUS_TYPE_ISA;
1041 brd->basemem = ioremap(baseaddr[i], 0x4000);
1042 if (!brd->basemem) {
1043 printk(KERN_ERR "MOXA: can't remap %lx\n",
1047 if (moxa_init_board(brd, NULL)) {
1048 iounmap(brd->basemem);
1049 brd->basemem = NULL;
1061 retval = pci_register_driver(&moxa_pci_driver);
1063 printk(KERN_ERR "Can't register MOXA pci driver!\n");
1072 static void __exit moxa_exit(void)
1077 pci_unregister_driver(&moxa_pci_driver);
1080 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1081 if (moxa_boards[i].ready)
1082 moxa_board_deinit(&moxa_boards[i]);
1084 del_timer_sync(&moxaTimer);
1086 if (tty_unregister_driver(moxaDriver))
1087 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1089 put_tty_driver(moxaDriver);
1092 module_init(moxa_init);
1093 module_exit(moxa_exit);
1095 static void moxa_close_port(struct moxa_port *ch)
1098 MoxaPortFlushData(ch, 2);
1099 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1100 ch->tty->driver_data = NULL;
1104 static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1105 struct moxa_port *ch)
1112 prepare_to_wait(&ch->open_wait, &wait, TASK_INTERRUPTIBLE);
1113 if (tty_hung_up_p(filp)) {
1114 #ifdef SERIAL_DO_RESTART
1115 retval = -ERESTARTSYS;
1121 spin_lock_bh(&moxa_lock);
1123 spin_unlock_bh(&moxa_lock);
1127 if (signal_pending(current)) {
1128 retval = -ERESTARTSYS;
1133 finish_wait(&ch->open_wait, &wait);
1138 static int moxa_open(struct tty_struct *tty, struct file *filp)
1140 struct moxa_board_conf *brd;
1141 struct moxa_port *ch;
1146 if (port == MAX_PORTS) {
1147 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1149 if (mutex_lock_interruptible(&moxa_openlock))
1150 return -ERESTARTSYS;
1151 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1153 mutex_unlock(&moxa_openlock);
1157 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1159 tty->driver_data = ch;
1161 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
1162 ch->statusflags = 0;
1163 moxa_set_tty_param(tty, tty->termios);
1164 MoxaPortLineCtrl(ch, 1, 1);
1166 MoxaSetFifo(ch, ch->type == PORT_16550A);
1167 ch->asyncflags |= ASYNC_INITIALIZED;
1169 mutex_unlock(&moxa_openlock);
1172 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1173 retval = moxa_block_till_ready(tty, filp, ch);
1174 mutex_lock(&moxa_openlock);
1176 if (ch->count) /* 0 means already hung up... */
1177 if (--ch->count == 0)
1178 moxa_close_port(ch);
1180 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1181 mutex_unlock(&moxa_openlock);
1186 static void moxa_close(struct tty_struct *tty, struct file *filp)
1188 struct moxa_port *ch;
1192 if (port == MAX_PORTS || tty_hung_up_p(filp))
1195 mutex_lock(&moxa_openlock);
1196 ch = tty->driver_data;
1199 if (tty->count == 1 && ch->count != 1) {
1200 printk(KERN_WARNING "moxa_close: bad serial port count; "
1201 "tty->count is 1, ch->count is %d\n", ch->count);
1204 if (--ch->count < 0) {
1205 printk(KERN_WARNING "moxa_close: bad serial port count, "
1206 "device=%s\n", tty->name);
1212 ch->cflag = tty->termios->c_cflag;
1213 if (ch->asyncflags & ASYNC_INITIALIZED) {
1214 moxa_setup_empty_event(tty);
1215 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
1218 moxa_close_port(ch);
1220 mutex_unlock(&moxa_openlock);
1223 static int moxa_write(struct tty_struct *tty,
1224 const unsigned char *buf, int count)
1226 struct moxa_port *ch = tty->driver_data;
1232 spin_lock_bh(&moxa_lock);
1233 len = MoxaPortWriteData(ch, buf, count);
1234 spin_unlock_bh(&moxa_lock);
1236 ch->statusflags |= LOWWAIT;
1240 static int moxa_write_room(struct tty_struct *tty)
1242 struct moxa_port *ch;
1246 ch = tty->driver_data;
1249 return MoxaPortTxFree(ch);
1252 static void moxa_flush_buffer(struct tty_struct *tty)
1254 struct moxa_port *ch = tty->driver_data;
1258 MoxaPortFlushData(ch, 1);
1262 static int moxa_chars_in_buffer(struct tty_struct *tty)
1264 struct moxa_port *ch = tty->driver_data;
1268 * Sigh...I have to check if driver_data is NULL here, because
1269 * if an open() fails, the TTY subsystem eventually calls
1270 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1271 * routine. And since the open() failed, we return 0 here. TDJ
1275 chars = MoxaPortTxQueue(ch);
1278 * Make it possible to wakeup anything waiting for output
1279 * in tty_ioctl.c, etc.
1281 if (!(ch->statusflags & EMPTYWAIT))
1282 moxa_setup_empty_event(tty);
1287 static void moxa_flush_chars(struct tty_struct *tty)
1290 * Don't think I need this, because this is called to empty the TX
1291 * buffer for the 16450, 16550, etc.
1295 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
1297 struct moxa_port *ch = tty->driver_data;
1301 spin_lock_bh(&moxa_lock);
1302 MoxaPortWriteData(ch, &c, 1);
1303 spin_unlock_bh(&moxa_lock);
1305 ch->statusflags |= LOWWAIT;
1308 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1310 struct moxa_port *ch;
1311 int flag = 0, dtr, rts;
1313 mutex_lock(&moxa_openlock);
1314 ch = tty->driver_data;
1316 mutex_unlock(&moxa_openlock);
1320 MoxaPortGetLineOut(ch, &dtr, &rts);
1325 dtr = MoxaPortLineStatus(ch);
1332 mutex_unlock(&moxa_openlock);
1336 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1337 unsigned int set, unsigned int clear)
1339 struct moxa_port *ch;
1344 mutex_lock(&moxa_openlock);
1345 ch = tty->driver_data;
1347 mutex_unlock(&moxa_openlock);
1351 MoxaPortGetLineOut(ch, &dtr, &rts);
1352 if (set & TIOCM_RTS)
1354 if (set & TIOCM_DTR)
1356 if (clear & TIOCM_RTS)
1358 if (clear & TIOCM_DTR)
1360 MoxaPortLineCtrl(ch, dtr, rts);
1361 mutex_unlock(&moxa_openlock);
1365 static void moxa_throttle(struct tty_struct *tty)
1367 struct moxa_port *ch = tty->driver_data;
1369 ch->statusflags |= THROTTLE;
1372 static void moxa_unthrottle(struct tty_struct *tty)
1374 struct moxa_port *ch = tty->driver_data;
1376 ch->statusflags &= ~THROTTLE;
1379 static void moxa_set_termios(struct tty_struct *tty,
1380 struct ktermios *old_termios)
1382 struct moxa_port *ch = tty->driver_data;
1386 moxa_set_tty_param(tty, old_termios);
1387 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1388 wake_up_interruptible(&ch->open_wait);
1391 static void moxa_stop(struct tty_struct *tty)
1393 struct moxa_port *ch = tty->driver_data;
1397 MoxaPortTxDisable(ch);
1398 ch->statusflags |= TXSTOPPED;
1402 static void moxa_start(struct tty_struct *tty)
1404 struct moxa_port *ch = tty->driver_data;
1409 if (!(ch->statusflags & TXSTOPPED))
1412 MoxaPortTxEnable(ch);
1413 ch->statusflags &= ~TXSTOPPED;
1416 static void moxa_hangup(struct tty_struct *tty)
1418 struct moxa_port *ch;
1420 mutex_lock(&moxa_openlock);
1421 ch = tty->driver_data;
1423 mutex_unlock(&moxa_openlock);
1427 moxa_close_port(ch);
1428 mutex_unlock(&moxa_openlock);
1430 wake_up_interruptible(&ch->open_wait);
1433 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1437 if (dcd != p->DCDState && p->tty && C_CLOCAL(p->tty)) {
1444 static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1447 struct tty_struct *tty = p->tty;
1448 void __iomem *ofsAddr;
1449 unsigned int inited = p->asyncflags & ASYNC_INITIALIZED;
1453 if ((p->statusflags & EMPTYWAIT) &&
1454 MoxaPortTxQueue(p) == 0) {
1455 p->statusflags &= ~EMPTYWAIT;
1458 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1459 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1460 p->statusflags &= ~LOWWAIT;
1464 if (inited && !(p->statusflags & THROTTLE) &&
1465 MoxaPortRxQueue(p) > 0) { /* RX */
1466 MoxaPortReadData(p);
1467 tty_schedule_flip(tty);
1470 p->statusflags &= ~EMPTYWAIT;
1471 MoxaPortFlushData(p, 0); /* flush RX */
1474 if (!handle) /* nothing else to do */
1477 intr = readw(ip); /* port irq status */
1481 writew(0, ip); /* ACK port */
1482 ofsAddr = p->tableAddr;
1483 if (intr & IntrTx) /* disable tx intr */
1484 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1485 ofsAddr + HostStat);
1490 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1491 tty_insert_flip_char(tty, 0, TTY_BREAK);
1492 tty_schedule_flip(tty);
1495 if (intr & IntrLine)
1496 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1501 static void moxa_poll(unsigned long ignored)
1503 struct moxa_board_conf *brd;
1505 unsigned int card, port, served = 0;
1507 spin_lock(&moxa_lock);
1508 for (card = 0; card < MAX_BOARDS; card++) {
1509 brd = &moxa_boards[card];
1516 if (readb(brd->intPend) == 0xff)
1517 ip = brd->intTable + readb(brd->intNdx);
1519 for (port = 0; port < brd->numPorts; port++)
1520 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1523 writeb(0, brd->intPend); /* ACK */
1525 if (moxaLowWaterChk) {
1526 struct moxa_port *p = brd->ports;
1527 for (port = 0; port < brd->numPorts; port++, p++)
1528 if (p->lowChkFlag) {
1530 moxa_low_water_check(p->tableAddr);
1534 moxaLowWaterChk = 0;
1537 mod_timer(&moxaTimer, jiffies + HZ / 50);
1538 spin_unlock(&moxa_lock);
1541 /******************************************************************************/
1543 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1545 register struct ktermios *ts = tty->termios;
1546 struct moxa_port *ch = tty->driver_data;
1547 int rts, cts, txflow, rxflow, xany, baud;
1549 rts = cts = txflow = rxflow = xany = 0;
1550 if (ts->c_cflag & CRTSCTS)
1552 if (ts->c_iflag & IXON)
1554 if (ts->c_iflag & IXOFF)
1556 if (ts->c_iflag & IXANY)
1559 /* Clear the features we don't support */
1560 ts->c_cflag &= ~CMSPAR;
1561 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1562 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1564 baud = tty_termios_baud_rate(old_termios);
1565 /* Not put the baud rate into the termios data */
1566 tty_encode_baud_rate(tty, baud, baud);
1569 static void moxa_setup_empty_event(struct tty_struct *tty)
1571 struct moxa_port *ch = tty->driver_data;
1573 spin_lock_bh(&moxa_lock);
1574 ch->statusflags |= EMPTYWAIT;
1575 spin_unlock_bh(&moxa_lock);
1578 static void moxa_shut_down(struct moxa_port *ch)
1580 struct tty_struct *tp = ch->tty;
1582 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1585 MoxaPortDisable(ch);
1588 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1591 MoxaPortLineCtrl(ch, 0, 0);
1593 spin_lock_bh(&moxa_lock);
1594 ch->asyncflags &= ~ASYNC_INITIALIZED;
1595 spin_unlock_bh(&moxa_lock);
1598 /*****************************************************************************
1599 * Driver level functions: *
1600 *****************************************************************************/
1602 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1604 void __iomem *ofsAddr;
1605 if (mode < 0 || mode > 2)
1607 ofsAddr = port->tableAddr;
1608 moxafunc(ofsAddr, FC_FlushQueue, mode);
1610 port->lowChkFlag = 0;
1611 moxa_low_water_check(ofsAddr);
1616 * Moxa Port Number Description:
1618 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1619 * the port number using in MOXA driver functions will be 0 to 31 for
1620 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1621 * to 127 for fourth. For example, if you setup three MOXA boards,
1622 * first board is C218, second board is C320-16 and third board is
1623 * C320-32. The port number of first board (C218 - 8 ports) is from
1624 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1625 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1626 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1627 * 127 will be invalid.
1630 * Moxa Functions Description:
1632 * Function 1: Driver initialization routine, this routine must be
1633 * called when initialized driver.
1635 * void MoxaDriverInit();
1638 * Function 2: Moxa driver private IOCTL command processing.
1640 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1642 * unsigned int cmd : IOCTL command
1643 * unsigned long arg : IOCTL argument
1644 * int port : port number (0 - 127)
1651 * Function 6: Enable this port to start Tx/Rx data.
1653 * void MoxaPortEnable(int port);
1654 * int port : port number (0 - 127)
1657 * Function 7: Disable this port
1659 * void MoxaPortDisable(int port);
1660 * int port : port number (0 - 127)
1663 * Function 8: Get the maximun available baud rate of this port.
1665 * long MoxaPortGetMaxBaud(int port);
1666 * int port : port number (0 - 127)
1668 * return: 0 : this port is invalid
1669 * 38400/57600/115200 bps
1672 * Function 10: Setting baud rate of this port.
1674 * long MoxaPortSetBaud(int port, long baud);
1675 * int port : port number (0 - 127)
1676 * long baud : baud rate (50 - 115200)
1678 * return: 0 : this port is invalid or baud < 50
1679 * 50 - 115200 : the real baud rate set to the port, if
1680 * the argument baud is large than maximun
1681 * available baud rate, the real setting
1682 * baud rate will be the maximun baud rate.
1685 * Function 12: Configure the port.
1687 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1688 * int port : port number (0 - 127)
1689 * struct ktermios * termio : termio structure pointer
1690 * speed_t baud : baud rate
1692 * return: -1 : this port is invalid or termio == NULL
1696 * Function 13: Get the DTR/RTS state of this port.
1698 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1699 * int port : port number (0 - 127)
1700 * int * dtrState : pointer to INT to receive the current DTR
1701 * state. (if NULL, this function will not
1702 * write to this address)
1703 * int * rtsState : pointer to INT to receive the current RTS
1704 * state. (if NULL, this function will not
1705 * write to this address)
1707 * return: -1 : this port is invalid
1711 * Function 14: Setting the DTR/RTS output state of this port.
1713 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1714 * int port : port number (0 - 127)
1715 * int dtrState : DTR output state (0: off, 1: on)
1716 * int rtsState : RTS output state (0: off, 1: on)
1719 * Function 15: Setting the flow control of this port.
1721 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1722 * int txFlow,int xany);
1723 * int port : port number (0 - 127)
1724 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1725 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1726 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1727 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1728 * int xany : S/W XANY flow control (0: no, 1: yes)
1731 * Function 16: Get ths line status of this port
1733 * int MoxaPortLineStatus(int port);
1734 * int port : port number (0 - 127)
1736 * return: Bit 0 - CTS state (0: off, 1: on)
1737 * Bit 1 - DSR state (0: off, 1: on)
1738 * Bit 2 - DCD state (0: off, 1: on)
1741 * Function 19: Flush the Rx/Tx buffer data of this port.
1743 * void MoxaPortFlushData(int port, int mode);
1744 * int port : port number (0 - 127)
1746 * 0 : flush the Rx buffer
1747 * 1 : flush the Tx buffer
1748 * 2 : flush the Rx and Tx buffer
1751 * Function 20: Write data.
1753 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1754 * int port : port number (0 - 127)
1755 * unsigned char * buffer : pointer to write data buffer.
1756 * int length : write data length
1758 * return: 0 - length : real write data length
1761 * Function 21: Read data.
1763 * int MoxaPortReadData(int port, struct tty_struct *tty);
1764 * int port : port number (0 - 127)
1765 * struct tty_struct *tty : tty for data
1767 * return: 0 - length : real read data length
1770 * Function 24: Get the Tx buffer current queued data bytes
1772 * int MoxaPortTxQueue(int port);
1773 * int port : port number (0 - 127)
1775 * return: .. : Tx buffer current queued data bytes
1778 * Function 25: Get the Tx buffer current free space
1780 * int MoxaPortTxFree(int port);
1781 * int port : port number (0 - 127)
1783 * return: .. : Tx buffer current free space
1786 * Function 26: Get the Rx buffer current queued data bytes
1788 * int MoxaPortRxQueue(int port);
1789 * int port : port number (0 - 127)
1791 * return: .. : Rx buffer current queued data bytes
1794 * Function 28: Disable port data transmission.
1796 * void MoxaPortTxDisable(int port);
1797 * int port : port number (0 - 127)
1800 * Function 29: Enable port data transmission.
1802 * void MoxaPortTxEnable(int port);
1803 * int port : port number (0 - 127)
1806 * Function 31: Get the received BREAK signal count and reset it.
1808 * int MoxaPortResetBrkCnt(int port);
1809 * int port : port number (0 - 127)
1811 * return: 0 - .. : BREAK signal count
1816 static void MoxaPortEnable(struct moxa_port *port)
1818 void __iomem *ofsAddr;
1821 ofsAddr = port->tableAddr;
1822 writew(lowwater, ofsAddr + Low_water);
1823 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1824 port->board->boardType == MOXA_BOARD_C320_PCI)
1825 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1827 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1828 ofsAddr + HostStat);
1830 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1831 moxafunc(ofsAddr, FC_FlushQueue, 2);
1833 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1834 MoxaPortLineStatus(port);
1837 static void MoxaPortDisable(struct moxa_port *port)
1839 void __iomem *ofsAddr = port->tableAddr;
1841 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1842 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1843 writew(0, ofsAddr + HostStat);
1844 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1847 static long MoxaPortGetMaxBaud(struct moxa_port *port)
1849 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1850 port->board->boardType == MOXA_BOARD_C320_PCI)
1857 static long MoxaPortSetBaud(struct moxa_port *port, long baud)
1859 void __iomem *ofsAddr;
1863 if (baud < 50L || (max = MoxaPortGetMaxBaud(port)) == 0)
1865 ofsAddr = port->tableAddr;
1869 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1870 else if (max == 57600L)
1871 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1873 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1875 moxafunc(ofsAddr, FC_SetBaud, val);
1880 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1883 void __iomem *ofsAddr;
1887 ofsAddr = port->tableAddr;
1888 cflag = termio->c_cflag; /* termio->c_cflag */
1890 mode = termio->c_cflag & CSIZE;
1893 else if (mode == CS6)
1895 else if (mode == CS7)
1897 else if (mode == CS8)
1900 if (termio->c_cflag & CSTOPB) {
1908 if (termio->c_cflag & PARENB) {
1909 if (termio->c_cflag & PARODD)
1916 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
1918 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1919 port->board->boardType == MOXA_BOARD_C320_PCI) {
1920 if (baud >= 921600L)
1923 baud = MoxaPortSetBaud(port, baud);
1925 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1926 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1927 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1928 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1929 moxa_wait_finish(ofsAddr);
1935 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1939 *dtrState = !!(port->lineCtrl & DTR_ON);
1941 *rtsState = !!(port->lineCtrl & RTS_ON);
1946 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1954 port->lineCtrl = mode;
1955 moxafunc(port->tableAddr, FC_LineControl, mode);
1958 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1959 int txflow, int rxflow, int txany)
1964 mode |= RTS_FlowCtl;
1966 mode |= CTS_FlowCtl;
1973 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
1976 static int MoxaPortLineStatus(struct moxa_port *port)
1978 void __iomem *ofsAddr;
1981 ofsAddr = port->tableAddr;
1982 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1983 port->board->boardType == MOXA_BOARD_C320_PCI) {
1984 moxafunc(ofsAddr, FC_LineStatus, 0);
1985 val = readw(ofsAddr + FuncArg);
1987 val = readw(ofsAddr + FlagStat) >> 4;
1992 spin_lock_bh(&moxa_lock);
1993 moxa_new_dcdstate(port, val & 8);
1994 spin_unlock_bh(&moxa_lock);
1999 static int MoxaPortWriteData(struct moxa_port *port,
2000 const unsigned char *buffer, int len)
2002 void __iomem *baseAddr, *ofsAddr, *ofs;
2003 unsigned int c, total;
2004 u16 head, tail, tx_mask, spage, epage;
2005 u16 pageno, pageofs, bufhead;
2007 ofsAddr = port->tableAddr;
2008 baseAddr = port->board->basemem;
2009 tx_mask = readw(ofsAddr + TX_mask);
2010 spage = readw(ofsAddr + Page_txb);
2011 epage = readw(ofsAddr + EndPage_txb);
2012 tail = readw(ofsAddr + TXwptr);
2013 head = readw(ofsAddr + TXrptr);
2014 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
2017 moxaLog.txcnt[port->tty->index] += c;
2019 if (spage == epage) {
2020 bufhead = readw(ofsAddr + Ofs_txb);
2021 writew(spage, baseAddr + Control_reg);
2024 len = head - tail - 1;
2026 len = tx_mask + 1 - tail;
2027 len = (c > len) ? len : c;
2028 ofs = baseAddr + DynPage_addr + bufhead + tail;
2029 memcpy_toio(ofs, buffer, len);
2031 tail = (tail + len) & tx_mask;
2035 pageno = spage + (tail >> 13);
2036 pageofs = tail & Page_mask;
2038 len = Page_size - pageofs;
2041 writeb(pageno, baseAddr + Control_reg);
2042 ofs = baseAddr + DynPage_addr + pageofs;
2043 memcpy_toio(ofs, buffer, len);
2045 if (++pageno == epage)
2050 tail = (tail + total) & tx_mask;
2052 writew(tail, ofsAddr + TXwptr);
2053 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2057 static int MoxaPortReadData(struct moxa_port *port)
2059 struct tty_struct *tty = port->tty;
2061 void __iomem *baseAddr, *ofsAddr, *ofs;
2062 unsigned int count, len, total;
2063 u16 tail, rx_mask, spage, epage;
2064 u16 pageno, pageofs, bufhead, head;
2066 ofsAddr = port->tableAddr;
2067 baseAddr = port->board->basemem;
2068 head = readw(ofsAddr + RXrptr);
2069 tail = readw(ofsAddr + RXwptr);
2070 rx_mask = readw(ofsAddr + RX_mask);
2071 spage = readw(ofsAddr + Page_rxb);
2072 epage = readw(ofsAddr + EndPage_rxb);
2073 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
2078 moxaLog.rxcnt[tty->index] += total;
2079 if (spage == epage) {
2080 bufhead = readw(ofsAddr + Ofs_rxb);
2081 writew(spage, baseAddr + Control_reg);
2083 ofs = baseAddr + DynPage_addr + bufhead + head;
2084 len = (tail >= head) ? (tail - head) :
2085 (rx_mask + 1 - head);
2086 len = tty_prepare_flip_string(tty, &dst,
2088 memcpy_fromio(dst, ofs, len);
2089 head = (head + len) & rx_mask;
2093 pageno = spage + (head >> 13);
2094 pageofs = head & Page_mask;
2096 writew(pageno, baseAddr + Control_reg);
2097 ofs = baseAddr + DynPage_addr + pageofs;
2098 len = tty_prepare_flip_string(tty, &dst,
2099 min(Page_size - pageofs, count));
2100 memcpy_fromio(dst, ofs, len);
2103 pageofs = (pageofs + len) & Page_mask;
2104 if (pageofs == 0 && ++pageno == epage)
2107 head = (head + total) & rx_mask;
2109 writew(head, ofsAddr + RXrptr);
2110 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2111 moxaLowWaterChk = 1;
2112 port->lowChkFlag = 1;
2118 static int MoxaPortTxQueue(struct moxa_port *port)
2120 void __iomem *ofsAddr = port->tableAddr;
2121 u16 rptr, wptr, mask;
2123 rptr = readw(ofsAddr + TXrptr);
2124 wptr = readw(ofsAddr + TXwptr);
2125 mask = readw(ofsAddr + TX_mask);
2126 return (wptr - rptr) & mask;
2129 static int MoxaPortTxFree(struct moxa_port *port)
2131 void __iomem *ofsAddr = port->tableAddr;
2132 u16 rptr, wptr, mask;
2134 rptr = readw(ofsAddr + TXrptr);
2135 wptr = readw(ofsAddr + TXwptr);
2136 mask = readw(ofsAddr + TX_mask);
2137 return mask - ((wptr - rptr) & mask);
2140 static int MoxaPortRxQueue(struct moxa_port *port)
2142 void __iomem *ofsAddr = port->tableAddr;
2143 u16 rptr, wptr, mask;
2145 rptr = readw(ofsAddr + RXrptr);
2146 wptr = readw(ofsAddr + RXwptr);
2147 mask = readw(ofsAddr + RX_mask);
2148 return (wptr - rptr) & mask;
2151 static void MoxaPortTxDisable(struct moxa_port *port)
2153 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2156 static void MoxaPortTxEnable(struct moxa_port *port)
2158 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2161 static int moxa_get_serial_info(struct moxa_port *info,
2162 struct serial_struct __user *retinfo)
2164 struct serial_struct tmp = {
2166 .line = info->tty->index,
2167 .flags = info->asyncflags,
2168 .baud_base = 921600,
2169 .close_delay = info->close_delay
2171 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2175 static int moxa_set_serial_info(struct moxa_port *info,
2176 struct serial_struct __user *new_info)
2178 struct serial_struct new_serial;
2180 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2183 if (new_serial.irq != 0 || new_serial.port != 0 ||
2184 new_serial.custom_divisor != 0 ||
2185 new_serial.baud_base != 921600)
2188 if (!capable(CAP_SYS_ADMIN)) {
2189 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2190 (info->asyncflags & ~ASYNC_USR_MASK)))
2193 info->close_delay = new_serial.close_delay * HZ / 100;
2195 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2196 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2198 MoxaSetFifo(info, new_serial.type == PORT_16550A);
2200 info->type = new_serial.type;
2206 /*****************************************************************************
2207 * Static local functions: *
2208 *****************************************************************************/
2210 static void MoxaSetFifo(struct moxa_port *port, int enable)
2212 void __iomem *ofsAddr = port->tableAddr;
2215 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2216 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2218 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2219 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);