1 /*****************************************************************************/
3 * moxa.c -- MOXA Intellio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com).
6 * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
8 * This code is loosely based on the Linux serial driver, written by
9 * Linus Torvalds, Theodore T'so and others.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
18 * MOXA Intellio Series Driver
24 #include <linux/module.h>
25 #include <linux/types.h>
27 #include <linux/ioport.h>
28 #include <linux/errno.h>
29 #include <linux/firmware.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/timer.h>
33 #include <linux/interrupt.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/major.h>
37 #include <linux/string.h>
38 #include <linux/fcntl.h>
39 #include <linux/ptrace.h>
40 #include <linux/serial.h>
41 #include <linux/tty_driver.h>
42 #include <linux/delay.h>
43 #include <linux/pci.h>
44 #include <linux/init.h>
45 #include <linux/bitops.h>
46 #include <linux/slab.h>
47 #include <linux/ratelimit.h>
50 #include <asm/uaccess.h>
54 #define MOXA_VERSION "6.0k"
56 #define MOXA_FW_HDRLEN 32
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)
64 #define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
65 (brd)->boardType == MOXA_BOARD_C320_PCI)
68 * Define the Moxa PCI vendor and device IDs.
70 #define MOXA_BUS_TYPE_ISA 0
71 #define MOXA_BUS_TYPE_PCI 1
74 MOXA_BOARD_C218_PCI = 1,
81 static char *moxa_brdname[] =
83 "C218 Turbo PCI series",
84 "C218 Turbo ISA series",
85 "C320 Turbo PCI series",
86 "C320 Turbo ISA series",
91 static struct pci_device_id moxa_pcibrds[] = {
92 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
93 .driver_data = MOXA_BOARD_C218_PCI },
94 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
95 .driver_data = MOXA_BOARD_C320_PCI },
96 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
97 .driver_data = MOXA_BOARD_CP204J },
100 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
101 #endif /* CONFIG_PCI */
105 static struct moxa_board_conf {
112 struct moxa_port *ports;
114 void __iomem *basemem;
115 void __iomem *intNdx;
116 void __iomem *intPend;
117 void __iomem *intTable;
118 } moxa_boards[MAX_BOARDS];
120 struct mxser_mstatus {
134 struct tty_port port;
135 struct moxa_board_conf *board;
136 void __iomem *tableAddr;
140 unsigned long statusflags;
142 u8 DCDState; /* Protected by the port lock */
149 int rxcnt[MAX_PORTS];
150 int txcnt[MAX_PORTS];
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 static DEFINE_SPINLOCK(moxa_lock);
169 static unsigned long baseaddr[MAX_BOARDS];
170 static unsigned int type[MAX_BOARDS];
171 static unsigned int numports[MAX_BOARDS];
172 static struct tty_port moxa_service_port;
174 MODULE_AUTHOR("William Chen");
175 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
176 MODULE_LICENSE("GPL");
179 module_param_array(type, uint, NULL, 0);
180 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
181 module_param_array(baseaddr, ulong, NULL, 0);
182 MODULE_PARM_DESC(baseaddr, "base address");
183 module_param_array(numports, uint, NULL, 0);
184 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
186 module_param(ttymajor, int, 0);
191 static int moxa_open(struct tty_struct *, struct file *);
192 static void moxa_close(struct tty_struct *, struct file *);
193 static int moxa_write(struct tty_struct *, const unsigned char *, int);
194 static int moxa_write_room(struct tty_struct *);
195 static void moxa_flush_buffer(struct tty_struct *);
196 static int moxa_chars_in_buffer(struct tty_struct *);
197 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
198 static void moxa_stop(struct tty_struct *);
199 static void moxa_start(struct tty_struct *);
200 static void moxa_hangup(struct tty_struct *);
201 static int moxa_tiocmget(struct tty_struct *tty);
202 static int moxa_tiocmset(struct tty_struct *tty,
203 unsigned int set, unsigned int clear);
204 static void moxa_poll(unsigned long);
205 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
206 static void moxa_shutdown(struct tty_port *);
207 static int moxa_carrier_raised(struct tty_port *);
208 static void moxa_dtr_rts(struct tty_port *, int);
210 * moxa board interface functions:
212 static void MoxaPortEnable(struct moxa_port *);
213 static void MoxaPortDisable(struct moxa_port *);
214 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
215 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
216 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
217 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
218 static int MoxaPortLineStatus(struct moxa_port *);
219 static void MoxaPortFlushData(struct moxa_port *, int);
220 static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
221 static int MoxaPortReadData(struct moxa_port *);
222 static int MoxaPortTxQueue(struct moxa_port *);
223 static int MoxaPortRxQueue(struct moxa_port *);
224 static int MoxaPortTxFree(struct moxa_port *);
225 static void MoxaPortTxDisable(struct moxa_port *);
226 static void MoxaPortTxEnable(struct moxa_port *);
227 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
228 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
229 static void MoxaSetFifo(struct moxa_port *port, int enable);
235 static DEFINE_SPINLOCK(moxafunc_lock);
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)
245 printk_ratelimited(KERN_WARNING "moxa function expired\n");
248 static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
251 spin_lock_irqsave(&moxafunc_lock, flags);
252 writew(arg, ofsAddr + FuncArg);
253 writew(cmd, ofsAddr + FuncCode);
254 moxa_wait_finish(ofsAddr);
255 spin_unlock_irqrestore(&moxafunc_lock, flags);
258 static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
262 spin_lock_irqsave(&moxafunc_lock, flags);
263 writew(arg, ofsAddr + FuncArg);
264 writew(cmd, ofsAddr + FuncCode);
265 moxa_wait_finish(ofsAddr);
266 ret = readw(ofsAddr + FuncArg);
267 spin_unlock_irqrestore(&moxafunc_lock, flags);
271 static void moxa_low_water_check(void __iomem *ofsAddr)
273 u16 rptr, wptr, mask, len;
275 if (readb(ofsAddr + FlagStat) & Xoff_state) {
276 rptr = readw(ofsAddr + RXrptr);
277 wptr = readw(ofsAddr + RXwptr);
278 mask = readw(ofsAddr + RX_mask);
279 len = (wptr - rptr) & mask;
280 if (len <= Low_water)
281 moxafunc(ofsAddr, FC_SendXon, 0);
289 static int moxa_ioctl(struct tty_struct *tty,
290 unsigned int cmd, unsigned long arg)
292 struct moxa_port *ch = tty->driver_data;
293 void __user *argp = (void __user *)arg;
296 if (tty->index == MAX_PORTS) {
297 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
298 cmd != MOXA_GETMSTATUS)
304 case MOXA_GETDATACOUNT:
305 moxaLog.tick = jiffies;
306 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
309 case MOXA_FLUSH_QUEUE:
310 MoxaPortFlushData(ch, arg);
312 case MOXA_GET_IOQUEUE: {
313 struct moxaq_str __user *argm = argp;
314 struct moxaq_str tmp;
318 for (i = 0; i < MAX_BOARDS; i++) {
319 p = moxa_boards[i].ports;
320 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
321 memset(&tmp, 0, sizeof(tmp));
322 spin_lock_bh(&moxa_lock);
323 if (moxa_boards[i].ready) {
324 tmp.inq = MoxaPortRxQueue(p);
325 tmp.outq = MoxaPortTxQueue(p);
327 spin_unlock_bh(&moxa_lock);
328 if (copy_to_user(argm, &tmp, sizeof(tmp)))
333 } case MOXA_GET_OQUEUE:
334 status = MoxaPortTxQueue(ch);
335 ret = put_user(status, (unsigned long __user *)argp);
337 case MOXA_GET_IQUEUE:
338 status = MoxaPortRxQueue(ch);
339 ret = put_user(status, (unsigned long __user *)argp);
341 case MOXA_GETMSTATUS: {
342 struct mxser_mstatus __user *argm = argp;
343 struct mxser_mstatus tmp;
347 for (i = 0; i < MAX_BOARDS; i++) {
348 p = moxa_boards[i].ports;
349 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
350 struct tty_struct *ttyp;
351 memset(&tmp, 0, sizeof(tmp));
352 spin_lock_bh(&moxa_lock);
353 if (!moxa_boards[i].ready) {
354 spin_unlock_bh(&moxa_lock);
358 status = MoxaPortLineStatus(p);
359 spin_unlock_bh(&moxa_lock);
368 ttyp = tty_port_tty_get(&p->port);
370 tmp.cflag = p->cflag;
372 tmp.cflag = ttyp->termios.c_cflag;
375 if (copy_to_user(argm, &tmp, sizeof(tmp)))
382 mutex_lock(&ch->port.mutex);
383 ret = moxa_get_serial_info(ch, argp);
384 mutex_unlock(&ch->port.mutex);
387 mutex_lock(&ch->port.mutex);
388 ret = moxa_set_serial_info(ch, argp);
389 mutex_unlock(&ch->port.mutex);
397 static int moxa_break_ctl(struct tty_struct *tty, int state)
399 struct moxa_port *port = tty->driver_data;
401 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
406 static const struct tty_operations moxa_ops = {
410 .write_room = moxa_write_room,
411 .flush_buffer = moxa_flush_buffer,
412 .chars_in_buffer = moxa_chars_in_buffer,
414 .set_termios = moxa_set_termios,
417 .hangup = moxa_hangup,
418 .break_ctl = moxa_break_ctl,
419 .tiocmget = moxa_tiocmget,
420 .tiocmset = moxa_tiocmset,
423 static const struct tty_port_operations moxa_port_ops = {
424 .carrier_raised = moxa_carrier_raised,
425 .dtr_rts = moxa_dtr_rts,
426 .shutdown = moxa_shutdown,
429 static struct tty_driver *moxaDriver;
430 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
436 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
438 switch (brd->boardType) {
439 case MOXA_BOARD_C218_ISA:
440 case MOXA_BOARD_C218_PCI:
444 case MOXA_BOARD_CP204J:
458 static int moxa_check_fw(const void *ptr)
460 const __le16 *lptr = ptr;
462 if (*lptr != cpu_to_le16(0x7980))
468 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
471 void __iomem *baseAddr = brd->basemem;
474 writeb(HW_reset, baseAddr + Control_reg); /* reset */
476 memset_io(baseAddr, 0, 4096);
477 memcpy_toio(baseAddr, buf, len); /* download BIOS */
478 writeb(0, baseAddr + Control_reg); /* restart */
482 switch (brd->boardType) {
483 case MOXA_BOARD_C218_ISA:
484 case MOXA_BOARD_C218_PCI:
485 tmp = readw(baseAddr + C218_key);
486 if (tmp != C218_KeyCode)
489 case MOXA_BOARD_CP204J:
490 tmp = readw(baseAddr + C218_key);
491 if (tmp != CP204J_KeyCode)
495 tmp = readw(baseAddr + C320_key);
496 if (tmp != C320_KeyCode)
498 tmp = readw(baseAddr + C320_status);
499 if (tmp != STS_init) {
500 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
501 "module not found\n");
509 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
513 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
516 void __iomem *baseAddr = brd->basemem;
519 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
523 writew(len - 7168 - 2, baseAddr + C320bapi_len);
524 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
525 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
526 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
527 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
532 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
535 void __iomem *baseAddr = brd->basemem;
536 const __le16 *uptr = ptr;
537 size_t wlen, len2, j;
538 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
539 unsigned int i, retry;
542 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
545 switch (brd->boardType) {
546 case MOXA_BOARD_CP204J:
547 case MOXA_BOARD_C218_ISA:
548 case MOXA_BOARD_C218_PCI:
550 loadbuf = C218_LoadBuf;
551 loadlen = C218DLoad_len;
552 checksum = C218check_sum;
553 checksum_ok = C218chksum_ok;
557 keycode = C320_KeyCode;
558 loadbuf = C320_LoadBuf;
559 loadlen = C320DLoad_len;
560 checksum = C320check_sum;
561 checksum_ok = C320chksum_ok;
567 for (i = 0; i < wlen; i++)
568 usum += le16_to_cpu(uptr[i]);
574 len2 = (wlen > 2048) ? 2048 : wlen;
576 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
579 writew(len2, baseAddr + loadlen);
580 writew(0, baseAddr + key);
581 for (i = 0; i < 100; i++) {
582 if (readw(baseAddr + key) == keycode)
586 if (readw(baseAddr + key) != keycode)
589 writew(0, baseAddr + loadlen);
590 writew(usum, baseAddr + checksum);
591 writew(0, baseAddr + key);
592 for (i = 0; i < 100; i++) {
593 if (readw(baseAddr + key) == keycode)
598 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
599 if (readb(baseAddr + checksum_ok) != 1)
602 writew(0, baseAddr + key);
603 for (i = 0; i < 600; i++) {
604 if (readw(baseAddr + Magic_no) == Magic_code)
608 if (readw(baseAddr + Magic_no) != Magic_code)
611 if (MOXA_IS_320(brd)) {
612 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
613 writew(0x3800, baseAddr + TMS320_PORT1);
614 writew(0x3900, baseAddr + TMS320_PORT2);
615 writew(28499, baseAddr + TMS320_CLOCK);
617 writew(0x3200, baseAddr + TMS320_PORT1);
618 writew(0x3400, baseAddr + TMS320_PORT2);
619 writew(19999, baseAddr + TMS320_CLOCK);
622 writew(1, baseAddr + Disable_IRQ);
623 writew(0, baseAddr + Magic_no);
624 for (i = 0; i < 500; i++) {
625 if (readw(baseAddr + Magic_no) == Magic_code)
629 if (readw(baseAddr + Magic_no) != Magic_code)
632 if (MOXA_IS_320(brd)) {
633 j = readw(baseAddr + Module_cnt);
636 brd->numPorts = j * 8;
637 writew(j, baseAddr + Module_no);
638 writew(0, baseAddr + Magic_no);
639 for (i = 0; i < 600; i++) {
640 if (readw(baseAddr + Magic_no) == Magic_code)
644 if (readw(baseAddr + Magic_no) != Magic_code)
647 brd->intNdx = baseAddr + IRQindex;
648 brd->intPend = baseAddr + IRQpending;
649 brd->intTable = baseAddr + IRQtable;
654 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
657 void __iomem *ofsAddr, *baseAddr = brd->basemem;
658 struct moxa_port *port;
662 printk(KERN_ERR "MOXA: bios length is not even\n");
666 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
670 switch (brd->boardType) {
671 case MOXA_BOARD_C218_ISA:
672 case MOXA_BOARD_C218_PCI:
673 case MOXA_BOARD_CP204J:
675 for (i = 0; i < brd->numPorts; i++, port++) {
678 port->tableAddr = baseAddr + Extern_table +
680 ofsAddr = port->tableAddr;
681 writew(C218rx_mask, ofsAddr + RX_mask);
682 writew(C218tx_mask, ofsAddr + TX_mask);
683 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
684 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
686 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
687 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
693 for (i = 0; i < brd->numPorts; i++, port++) {
696 port->tableAddr = baseAddr + Extern_table +
698 ofsAddr = port->tableAddr;
699 switch (brd->numPorts) {
701 writew(C320p8rx_mask, ofsAddr + RX_mask);
702 writew(C320p8tx_mask, ofsAddr + TX_mask);
703 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
704 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
705 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
706 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
710 writew(C320p16rx_mask, ofsAddr + RX_mask);
711 writew(C320p16tx_mask, ofsAddr + TX_mask);
712 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
713 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
714 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
715 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
719 writew(C320p24rx_mask, ofsAddr + RX_mask);
720 writew(C320p24tx_mask, ofsAddr + TX_mask);
721 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
722 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
723 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
724 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
727 writew(C320p32rx_mask, ofsAddr + RX_mask);
728 writew(C320p32tx_mask, ofsAddr + TX_mask);
729 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
730 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
731 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
732 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
733 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
742 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
744 const void *ptr = fw->data;
748 unsigned int a, lenp, lencnt;
751 __le32 magic; /* 0x34303430 */
753 u8 type; /* UNIX = 3 */
754 u8 model; /* C218T=1, C320T=2, CP204=3 */
759 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
761 if (fw->size < MOXA_FW_HDRLEN) {
762 strcpy(rsn, "too short (even header won't fit)");
765 if (hdr->magic != cpu_to_le32(0x30343034)) {
766 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
769 if (hdr->type != 3) {
770 sprintf(rsn, "not for linux, type is %u", hdr->type);
773 if (moxa_check_fw_model(brd, hdr->model)) {
774 sprintf(rsn, "not for this card, model is %u", hdr->model);
778 len = MOXA_FW_HDRLEN;
779 lencnt = hdr->model == 2 ? 5 : 3;
780 for (a = 0; a < ARRAY_SIZE(lens); a++) {
781 lens[a] = le16_to_cpu(hdr->len[a]);
782 if (lens[a] && len + lens[a] <= fw->size &&
783 moxa_check_fw(&fw->data[len]))
784 printk(KERN_WARNING "MOXA firmware: unexpected input "
785 "at offset %u, but going on\n", (u32)len);
786 if (!lens[a] && a < lencnt) {
787 sprintf(rsn, "too few entries in fw file");
793 if (len != fw->size) {
794 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
799 ptr += MOXA_FW_HDRLEN;
802 strcpy(rsn, "read above");
804 ret = moxa_load_bios(brd, ptr, lens[lenp]);
808 /* we skip the tty section (lens[1]), since we don't need it */
809 ptr += lens[lenp] + lens[lenp + 1];
810 lenp += 2; /* comm */
812 if (hdr->model == 2) {
813 ret = moxa_load_320b(brd, ptr, lens[lenp]);
816 /* skip another tty */
817 ptr += lens[lenp] + lens[lenp + 1];
821 ret = moxa_load_code(brd, ptr, lens[lenp]);
827 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
831 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
833 const struct firmware *fw;
836 unsigned int i, first_idx;
839 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
841 if (brd->ports == NULL) {
842 printk(KERN_ERR "cannot allocate memory for ports\n");
847 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
848 tty_port_init(&p->port);
849 p->port.ops = &moxa_port_ops;
850 p->type = PORT_16550A;
851 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
854 switch (brd->boardType) {
855 case MOXA_BOARD_C218_ISA:
856 case MOXA_BOARD_C218_PCI:
857 file = "/*(DEBLOBBED)*/";
859 case MOXA_BOARD_CP204J:
860 file = "/*(DEBLOBBED)*/";
863 file = "/*(DEBLOBBED)*/";
867 ret = reject_firmware(&fw, file, dev);
869 printk(KERN_ERR "MOXA: reject_firmware failed. Make sure "
870 "you've placed '%s' file into your firmware "
871 "loader directory (e.g. /lib/firmware)\n",
876 ret = moxa_load_fw(brd, fw);
878 release_firmware(fw);
883 spin_lock_bh(&moxa_lock);
885 if (!timer_pending(&moxaTimer))
886 mod_timer(&moxaTimer, jiffies + HZ / 50);
887 spin_unlock_bh(&moxa_lock);
889 first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
890 for (i = 0; i < brd->numPorts; i++)
891 tty_port_register_device(&brd->ports[i].port, moxaDriver,
896 for (i = 0; i < MAX_PORTS_PER_BOARD; i++)
897 tty_port_destroy(&brd->ports[i].port);
903 static void moxa_board_deinit(struct moxa_board_conf *brd)
905 unsigned int a, opened, first_idx;
907 mutex_lock(&moxa_openlock);
908 spin_lock_bh(&moxa_lock);
910 spin_unlock_bh(&moxa_lock);
912 /* pci hot-un-plug support */
913 for (a = 0; a < brd->numPorts; a++)
914 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
915 tty_port_tty_hangup(&brd->ports[a].port, false);
917 for (a = 0; a < MAX_PORTS_PER_BOARD; a++)
918 tty_port_destroy(&brd->ports[a].port);
922 for (a = 0; a < brd->numPorts; a++)
923 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
925 mutex_unlock(&moxa_openlock);
929 mutex_lock(&moxa_openlock);
932 first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
933 for (a = 0; a < brd->numPorts; a++)
934 tty_unregister_device(moxaDriver, first_idx + a);
936 iounmap(brd->basemem);
942 static int moxa_pci_probe(struct pci_dev *pdev,
943 const struct pci_device_id *ent)
945 struct moxa_board_conf *board;
947 int board_type = ent->driver_data;
950 retval = pci_enable_device(pdev);
952 dev_err(&pdev->dev, "can't enable pci device\n");
956 for (i = 0; i < MAX_BOARDS; i++)
957 if (moxa_boards[i].basemem == NULL)
961 if (i >= MAX_BOARDS) {
962 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
963 "found. Board is ignored.\n", MAX_BOARDS);
967 board = &moxa_boards[i];
969 retval = pci_request_region(pdev, 2, "moxa-base");
971 dev_err(&pdev->dev, "can't request pci region 2\n");
975 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
976 if (board->basemem == NULL) {
977 dev_err(&pdev->dev, "can't remap io space 2\n");
982 board->boardType = board_type;
983 switch (board_type) {
984 case MOXA_BOARD_C218_ISA:
985 case MOXA_BOARD_C218_PCI:
989 case MOXA_BOARD_CP204J:
996 board->busType = MOXA_BUS_TYPE_PCI;
998 retval = moxa_init_board(board, &pdev->dev);
1002 pci_set_drvdata(pdev, board);
1004 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
1005 moxa_brdname[board_type - 1], board->numPorts);
1009 iounmap(board->basemem);
1010 board->basemem = NULL;
1012 pci_release_region(pdev, 2);
1017 static void moxa_pci_remove(struct pci_dev *pdev)
1019 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
1021 moxa_board_deinit(brd);
1023 pci_release_region(pdev, 2);
1026 static struct pci_driver moxa_pci_driver = {
1028 .id_table = moxa_pcibrds,
1029 .probe = moxa_pci_probe,
1030 .remove = moxa_pci_remove
1032 #endif /* CONFIG_PCI */
1034 static int __init moxa_init(void)
1036 unsigned int isabrds = 0;
1038 struct moxa_board_conf *brd = moxa_boards;
1041 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1044 tty_port_init(&moxa_service_port);
1046 moxaDriver = tty_alloc_driver(MAX_PORTS + 1,
1047 TTY_DRIVER_REAL_RAW |
1048 TTY_DRIVER_DYNAMIC_DEV);
1049 if (IS_ERR(moxaDriver))
1050 return PTR_ERR(moxaDriver);
1052 moxaDriver->name = "ttyMX";
1053 moxaDriver->major = ttymajor;
1054 moxaDriver->minor_start = 0;
1055 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1056 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1057 moxaDriver->init_termios = tty_std_termios;
1058 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1059 moxaDriver->init_termios.c_ispeed = 9600;
1060 moxaDriver->init_termios.c_ospeed = 9600;
1061 tty_set_operations(moxaDriver, &moxa_ops);
1062 /* Having one more port only for ioctls is ugly */
1063 tty_port_link_device(&moxa_service_port, moxaDriver, MAX_PORTS);
1065 if (tty_register_driver(moxaDriver)) {
1066 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1067 put_tty_driver(moxaDriver);
1071 /* Find the boards defined from module args. */
1073 for (i = 0; i < MAX_BOARDS; i++) {
1076 if (type[i] == MOXA_BOARD_C218_ISA ||
1077 type[i] == MOXA_BOARD_C320_ISA) {
1078 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1079 isabrds + 1, moxa_brdname[type[i] - 1],
1081 brd->boardType = type[i];
1082 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1084 brd->busType = MOXA_BUS_TYPE_ISA;
1085 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
1086 if (!brd->basemem) {
1087 printk(KERN_ERR "MOXA: can't remap %lx\n",
1091 if (moxa_init_board(brd, NULL)) {
1092 iounmap(brd->basemem);
1093 brd->basemem = NULL;
1097 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1098 "ready (%u ports, firmware loaded)\n",
1099 baseaddr[i], brd->numPorts);
1107 retval = pci_register_driver(&moxa_pci_driver);
1109 printk(KERN_ERR "Can't register MOXA pci driver!\n");
1118 static void __exit moxa_exit(void)
1123 pci_unregister_driver(&moxa_pci_driver);
1126 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1127 if (moxa_boards[i].ready)
1128 moxa_board_deinit(&moxa_boards[i]);
1130 del_timer_sync(&moxaTimer);
1132 if (tty_unregister_driver(moxaDriver))
1133 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1135 put_tty_driver(moxaDriver);
1138 module_init(moxa_init);
1139 module_exit(moxa_exit);
1141 static void moxa_shutdown(struct tty_port *port)
1143 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1144 MoxaPortDisable(ch);
1145 MoxaPortFlushData(ch, 2);
1148 static int moxa_carrier_raised(struct tty_port *port)
1150 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1153 spin_lock_irq(&port->lock);
1155 spin_unlock_irq(&port->lock);
1159 static void moxa_dtr_rts(struct tty_port *port, int onoff)
1161 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1162 MoxaPortLineCtrl(ch, onoff, onoff);
1166 static int moxa_open(struct tty_struct *tty, struct file *filp)
1168 struct moxa_board_conf *brd;
1169 struct moxa_port *ch;
1173 if (port == MAX_PORTS) {
1174 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1176 if (mutex_lock_interruptible(&moxa_openlock))
1177 return -ERESTARTSYS;
1178 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1180 mutex_unlock(&moxa_openlock);
1184 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1185 mutex_unlock(&moxa_openlock);
1189 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1191 tty->driver_data = ch;
1192 tty_port_tty_set(&ch->port, tty);
1193 mutex_lock(&ch->port.mutex);
1194 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
1195 ch->statusflags = 0;
1196 moxa_set_tty_param(tty, &tty->termios);
1197 MoxaPortLineCtrl(ch, 1, 1);
1199 MoxaSetFifo(ch, ch->type == PORT_16550A);
1200 ch->port.flags |= ASYNC_INITIALIZED;
1202 mutex_unlock(&ch->port.mutex);
1203 mutex_unlock(&moxa_openlock);
1205 return tty_port_block_til_ready(&ch->port, tty, filp);
1208 static void moxa_close(struct tty_struct *tty, struct file *filp)
1210 struct moxa_port *ch = tty->driver_data;
1211 ch->cflag = tty->termios.c_cflag;
1212 tty_port_close(&ch->port, tty, filp);
1215 static int moxa_write(struct tty_struct *tty,
1216 const unsigned char *buf, int count)
1218 struct moxa_port *ch = tty->driver_data;
1219 unsigned long flags;
1225 spin_lock_irqsave(&moxa_lock, flags);
1226 len = MoxaPortWriteData(tty, buf, count);
1227 spin_unlock_irqrestore(&moxa_lock, flags);
1229 set_bit(LOWWAIT, &ch->statusflags);
1233 static int moxa_write_room(struct tty_struct *tty)
1235 struct moxa_port *ch;
1239 ch = tty->driver_data;
1242 return MoxaPortTxFree(ch);
1245 static void moxa_flush_buffer(struct tty_struct *tty)
1247 struct moxa_port *ch = tty->driver_data;
1251 MoxaPortFlushData(ch, 1);
1255 static int moxa_chars_in_buffer(struct tty_struct *tty)
1257 struct moxa_port *ch = tty->driver_data;
1260 chars = MoxaPortTxQueue(ch);
1263 * Make it possible to wakeup anything waiting for output
1264 * in tty_ioctl.c, etc.
1266 set_bit(EMPTYWAIT, &ch->statusflags);
1270 static int moxa_tiocmget(struct tty_struct *tty)
1272 struct moxa_port *ch = tty->driver_data;
1273 int flag = 0, dtr, rts;
1275 MoxaPortGetLineOut(ch, &dtr, &rts);
1280 dtr = MoxaPortLineStatus(ch);
1290 static int moxa_tiocmset(struct tty_struct *tty,
1291 unsigned int set, unsigned int clear)
1293 struct moxa_port *ch;
1296 mutex_lock(&moxa_openlock);
1297 ch = tty->driver_data;
1299 mutex_unlock(&moxa_openlock);
1303 MoxaPortGetLineOut(ch, &dtr, &rts);
1304 if (set & TIOCM_RTS)
1306 if (set & TIOCM_DTR)
1308 if (clear & TIOCM_RTS)
1310 if (clear & TIOCM_DTR)
1312 MoxaPortLineCtrl(ch, dtr, rts);
1313 mutex_unlock(&moxa_openlock);
1317 static void moxa_set_termios(struct tty_struct *tty,
1318 struct ktermios *old_termios)
1320 struct moxa_port *ch = tty->driver_data;
1324 moxa_set_tty_param(tty, old_termios);
1325 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1326 wake_up_interruptible(&ch->port.open_wait);
1329 static void moxa_stop(struct tty_struct *tty)
1331 struct moxa_port *ch = tty->driver_data;
1335 MoxaPortTxDisable(ch);
1336 set_bit(TXSTOPPED, &ch->statusflags);
1340 static void moxa_start(struct tty_struct *tty)
1342 struct moxa_port *ch = tty->driver_data;
1347 if (!test_bit(TXSTOPPED, &ch->statusflags))
1350 MoxaPortTxEnable(ch);
1351 clear_bit(TXSTOPPED, &ch->statusflags);
1354 static void moxa_hangup(struct tty_struct *tty)
1356 struct moxa_port *ch = tty->driver_data;
1357 tty_port_hangup(&ch->port);
1360 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1362 unsigned long flags;
1365 spin_lock_irqsave(&p->port.lock, flags);
1366 if (dcd != p->DCDState) {
1368 spin_unlock_irqrestore(&p->port.lock, flags);
1370 tty_port_tty_hangup(&p->port, true);
1373 spin_unlock_irqrestore(&p->port.lock, flags);
1376 static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1379 struct tty_struct *tty = tty_port_tty_get(&p->port);
1380 void __iomem *ofsAddr;
1381 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
1385 if (test_bit(EMPTYWAIT, &p->statusflags) &&
1386 MoxaPortTxQueue(p) == 0) {
1387 clear_bit(EMPTYWAIT, &p->statusflags);
1390 if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
1391 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1392 clear_bit(LOWWAIT, &p->statusflags);
1396 if (inited && !test_bit(TTY_THROTTLED, &tty->flags) &&
1397 MoxaPortRxQueue(p) > 0) { /* RX */
1398 MoxaPortReadData(p);
1399 tty_schedule_flip(&p->port);
1402 clear_bit(EMPTYWAIT, &p->statusflags);
1403 MoxaPortFlushData(p, 0); /* flush RX */
1406 if (!handle) /* nothing else to do */
1409 intr = readw(ip); /* port irq status */
1413 writew(0, ip); /* ACK port */
1414 ofsAddr = p->tableAddr;
1415 if (intr & IntrTx) /* disable tx intr */
1416 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1417 ofsAddr + HostStat);
1422 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1423 tty_insert_flip_char(&p->port, 0, TTY_BREAK);
1424 tty_schedule_flip(&p->port);
1427 if (intr & IntrLine)
1428 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1435 static void moxa_poll(unsigned long ignored)
1437 struct moxa_board_conf *brd;
1439 unsigned int card, port, served = 0;
1441 spin_lock(&moxa_lock);
1442 for (card = 0; card < MAX_BOARDS; card++) {
1443 brd = &moxa_boards[card];
1450 if (readb(brd->intPend) == 0xff)
1451 ip = brd->intTable + readb(brd->intNdx);
1453 for (port = 0; port < brd->numPorts; port++)
1454 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1457 writeb(0, brd->intPend); /* ACK */
1459 if (moxaLowWaterChk) {
1460 struct moxa_port *p = brd->ports;
1461 for (port = 0; port < brd->numPorts; port++, p++)
1462 if (p->lowChkFlag) {
1464 moxa_low_water_check(p->tableAddr);
1468 moxaLowWaterChk = 0;
1471 mod_timer(&moxaTimer, jiffies + HZ / 50);
1472 spin_unlock(&moxa_lock);
1475 /******************************************************************************/
1477 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1479 register struct ktermios *ts = &tty->termios;
1480 struct moxa_port *ch = tty->driver_data;
1481 int rts, cts, txflow, rxflow, xany, baud;
1483 rts = cts = txflow = rxflow = xany = 0;
1484 if (ts->c_cflag & CRTSCTS)
1486 if (ts->c_iflag & IXON)
1488 if (ts->c_iflag & IXOFF)
1490 if (ts->c_iflag & IXANY)
1493 /* Clear the features we don't support */
1494 ts->c_cflag &= ~CMSPAR;
1495 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1496 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1498 baud = tty_termios_baud_rate(old_termios);
1499 /* Not put the baud rate into the termios data */
1500 tty_encode_baud_rate(tty, baud, baud);
1503 /*****************************************************************************
1504 * Driver level functions: *
1505 *****************************************************************************/
1507 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1509 void __iomem *ofsAddr;
1510 if (mode < 0 || mode > 2)
1512 ofsAddr = port->tableAddr;
1513 moxafunc(ofsAddr, FC_FlushQueue, mode);
1515 port->lowChkFlag = 0;
1516 moxa_low_water_check(ofsAddr);
1521 * Moxa Port Number Description:
1523 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1524 * the port number using in MOXA driver functions will be 0 to 31 for
1525 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1526 * to 127 for fourth. For example, if you setup three MOXA boards,
1527 * first board is C218, second board is C320-16 and third board is
1528 * C320-32. The port number of first board (C218 - 8 ports) is from
1529 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1530 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1531 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1532 * 127 will be invalid.
1535 * Moxa Functions Description:
1537 * Function 1: Driver initialization routine, this routine must be
1538 * called when initialized driver.
1540 * void MoxaDriverInit();
1543 * Function 2: Moxa driver private IOCTL command processing.
1545 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1547 * unsigned int cmd : IOCTL command
1548 * unsigned long arg : IOCTL argument
1549 * int port : port number (0 - 127)
1556 * Function 6: Enable this port to start Tx/Rx data.
1558 * void MoxaPortEnable(int port);
1559 * int port : port number (0 - 127)
1562 * Function 7: Disable this port
1564 * void MoxaPortDisable(int port);
1565 * int port : port number (0 - 127)
1568 * Function 10: Setting baud rate of this port.
1570 * speed_t MoxaPortSetBaud(int port, speed_t baud);
1571 * int port : port number (0 - 127)
1572 * long baud : baud rate (50 - 115200)
1574 * return: 0 : this port is invalid or baud < 50
1575 * 50 - 115200 : the real baud rate set to the port, if
1576 * the argument baud is large than maximun
1577 * available baud rate, the real setting
1578 * baud rate will be the maximun baud rate.
1581 * Function 12: Configure the port.
1583 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1584 * int port : port number (0 - 127)
1585 * struct ktermios * termio : termio structure pointer
1586 * speed_t baud : baud rate
1588 * return: -1 : this port is invalid or termio == NULL
1592 * Function 13: Get the DTR/RTS state of this port.
1594 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1595 * int port : port number (0 - 127)
1596 * int * dtrState : pointer to INT to receive the current DTR
1597 * state. (if NULL, this function will not
1598 * write to this address)
1599 * int * rtsState : pointer to INT to receive the current RTS
1600 * state. (if NULL, this function will not
1601 * write to this address)
1603 * return: -1 : this port is invalid
1607 * Function 14: Setting the DTR/RTS output state of this port.
1609 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1610 * int port : port number (0 - 127)
1611 * int dtrState : DTR output state (0: off, 1: on)
1612 * int rtsState : RTS output state (0: off, 1: on)
1615 * Function 15: Setting the flow control of this port.
1617 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1618 * int txFlow,int xany);
1619 * int port : port number (0 - 127)
1620 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1621 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1622 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1623 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1624 * int xany : S/W XANY flow control (0: no, 1: yes)
1627 * Function 16: Get ths line status of this port
1629 * int MoxaPortLineStatus(int port);
1630 * int port : port number (0 - 127)
1632 * return: Bit 0 - CTS state (0: off, 1: on)
1633 * Bit 1 - DSR state (0: off, 1: on)
1634 * Bit 2 - DCD state (0: off, 1: on)
1637 * Function 19: Flush the Rx/Tx buffer data of this port.
1639 * void MoxaPortFlushData(int port, int mode);
1640 * int port : port number (0 - 127)
1642 * 0 : flush the Rx buffer
1643 * 1 : flush the Tx buffer
1644 * 2 : flush the Rx and Tx buffer
1647 * Function 20: Write data.
1649 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1650 * int port : port number (0 - 127)
1651 * unsigned char * buffer : pointer to write data buffer.
1652 * int length : write data length
1654 * return: 0 - length : real write data length
1657 * Function 21: Read data.
1659 * int MoxaPortReadData(int port, struct tty_struct *tty);
1660 * int port : port number (0 - 127)
1661 * struct tty_struct *tty : tty for data
1663 * return: 0 - length : real read data length
1666 * Function 24: Get the Tx buffer current queued data bytes
1668 * int MoxaPortTxQueue(int port);
1669 * int port : port number (0 - 127)
1671 * return: .. : Tx buffer current queued data bytes
1674 * Function 25: Get the Tx buffer current free space
1676 * int MoxaPortTxFree(int port);
1677 * int port : port number (0 - 127)
1679 * return: .. : Tx buffer current free space
1682 * Function 26: Get the Rx buffer current queued data bytes
1684 * int MoxaPortRxQueue(int port);
1685 * int port : port number (0 - 127)
1687 * return: .. : Rx buffer current queued data bytes
1690 * Function 28: Disable port data transmission.
1692 * void MoxaPortTxDisable(int port);
1693 * int port : port number (0 - 127)
1696 * Function 29: Enable port data transmission.
1698 * void MoxaPortTxEnable(int port);
1699 * int port : port number (0 - 127)
1702 * Function 31: Get the received BREAK signal count and reset it.
1704 * int MoxaPortResetBrkCnt(int port);
1705 * int port : port number (0 - 127)
1707 * return: 0 - .. : BREAK signal count
1712 static void MoxaPortEnable(struct moxa_port *port)
1714 void __iomem *ofsAddr;
1717 ofsAddr = port->tableAddr;
1718 writew(lowwater, ofsAddr + Low_water);
1719 if (MOXA_IS_320(port->board))
1720 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1722 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1723 ofsAddr + HostStat);
1725 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1726 moxafunc(ofsAddr, FC_FlushQueue, 2);
1728 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1729 MoxaPortLineStatus(port);
1732 static void MoxaPortDisable(struct moxa_port *port)
1734 void __iomem *ofsAddr = port->tableAddr;
1736 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1737 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1738 writew(0, ofsAddr + HostStat);
1739 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1742 static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
1744 void __iomem *ofsAddr = port->tableAddr;
1745 unsigned int clock, val;
1748 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1755 moxafunc(ofsAddr, FC_SetBaud, val);
1760 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1763 void __iomem *ofsAddr;
1766 ofsAddr = port->tableAddr;
1768 mode = termio->c_cflag & CSIZE;
1771 else if (mode == CS6)
1773 else if (mode == CS7)
1775 else if (mode == CS8)
1778 if (termio->c_cflag & CSTOPB) {
1786 if (termio->c_cflag & PARENB) {
1787 if (termio->c_cflag & PARODD)
1794 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
1796 if (MOXA_IS_320(port->board) && baud >= 921600)
1799 baud = MoxaPortSetBaud(port, baud);
1801 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1802 spin_lock_irq(&moxafunc_lock);
1803 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1804 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1805 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1806 moxa_wait_finish(ofsAddr);
1807 spin_unlock_irq(&moxafunc_lock);
1813 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1817 *dtrState = !!(port->lineCtrl & DTR_ON);
1819 *rtsState = !!(port->lineCtrl & RTS_ON);
1824 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1832 port->lineCtrl = mode;
1833 moxafunc(port->tableAddr, FC_LineControl, mode);
1836 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1837 int txflow, int rxflow, int txany)
1842 mode |= RTS_FlowCtl;
1844 mode |= CTS_FlowCtl;
1851 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
1854 static int MoxaPortLineStatus(struct moxa_port *port)
1856 void __iomem *ofsAddr;
1859 ofsAddr = port->tableAddr;
1860 if (MOXA_IS_320(port->board))
1861 val = moxafuncret(ofsAddr, FC_LineStatus, 0);
1863 val = readw(ofsAddr + FlagStat) >> 4;
1867 moxa_new_dcdstate(port, val & 8);
1872 static int MoxaPortWriteData(struct tty_struct *tty,
1873 const unsigned char *buffer, int len)
1875 struct moxa_port *port = tty->driver_data;
1876 void __iomem *baseAddr, *ofsAddr, *ofs;
1877 unsigned int c, total;
1878 u16 head, tail, tx_mask, spage, epage;
1879 u16 pageno, pageofs, bufhead;
1881 ofsAddr = port->tableAddr;
1882 baseAddr = port->board->basemem;
1883 tx_mask = readw(ofsAddr + TX_mask);
1884 spage = readw(ofsAddr + Page_txb);
1885 epage = readw(ofsAddr + EndPage_txb);
1886 tail = readw(ofsAddr + TXwptr);
1887 head = readw(ofsAddr + TXrptr);
1888 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
1891 moxaLog.txcnt[port->port.tty->index] += c;
1893 if (spage == epage) {
1894 bufhead = readw(ofsAddr + Ofs_txb);
1895 writew(spage, baseAddr + Control_reg);
1898 len = head - tail - 1;
1900 len = tx_mask + 1 - tail;
1901 len = (c > len) ? len : c;
1902 ofs = baseAddr + DynPage_addr + bufhead + tail;
1903 memcpy_toio(ofs, buffer, len);
1905 tail = (tail + len) & tx_mask;
1909 pageno = spage + (tail >> 13);
1910 pageofs = tail & Page_mask;
1912 len = Page_size - pageofs;
1915 writeb(pageno, baseAddr + Control_reg);
1916 ofs = baseAddr + DynPage_addr + pageofs;
1917 memcpy_toio(ofs, buffer, len);
1919 if (++pageno == epage)
1924 tail = (tail + total) & tx_mask;
1926 writew(tail, ofsAddr + TXwptr);
1927 writeb(1, ofsAddr + CD180TXirq); /* start to send */
1931 static int MoxaPortReadData(struct moxa_port *port)
1933 struct tty_struct *tty = port->port.tty;
1935 void __iomem *baseAddr, *ofsAddr, *ofs;
1936 unsigned int count, len, total;
1937 u16 tail, rx_mask, spage, epage;
1938 u16 pageno, pageofs, bufhead, head;
1940 ofsAddr = port->tableAddr;
1941 baseAddr = port->board->basemem;
1942 head = readw(ofsAddr + RXrptr);
1943 tail = readw(ofsAddr + RXwptr);
1944 rx_mask = readw(ofsAddr + RX_mask);
1945 spage = readw(ofsAddr + Page_rxb);
1946 epage = readw(ofsAddr + EndPage_rxb);
1947 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
1952 moxaLog.rxcnt[tty->index] += total;
1953 if (spage == epage) {
1954 bufhead = readw(ofsAddr + Ofs_rxb);
1955 writew(spage, baseAddr + Control_reg);
1957 ofs = baseAddr + DynPage_addr + bufhead + head;
1958 len = (tail >= head) ? (tail - head) :
1959 (rx_mask + 1 - head);
1960 len = tty_prepare_flip_string(&port->port, &dst,
1962 memcpy_fromio(dst, ofs, len);
1963 head = (head + len) & rx_mask;
1967 pageno = spage + (head >> 13);
1968 pageofs = head & Page_mask;
1970 writew(pageno, baseAddr + Control_reg);
1971 ofs = baseAddr + DynPage_addr + pageofs;
1972 len = tty_prepare_flip_string(&port->port, &dst,
1973 min(Page_size - pageofs, count));
1974 memcpy_fromio(dst, ofs, len);
1977 pageofs = (pageofs + len) & Page_mask;
1978 if (pageofs == 0 && ++pageno == epage)
1981 head = (head + total) & rx_mask;
1983 writew(head, ofsAddr + RXrptr);
1984 if (readb(ofsAddr + FlagStat) & Xoff_state) {
1985 moxaLowWaterChk = 1;
1986 port->lowChkFlag = 1;
1992 static int MoxaPortTxQueue(struct moxa_port *port)
1994 void __iomem *ofsAddr = port->tableAddr;
1995 u16 rptr, wptr, mask;
1997 rptr = readw(ofsAddr + TXrptr);
1998 wptr = readw(ofsAddr + TXwptr);
1999 mask = readw(ofsAddr + TX_mask);
2000 return (wptr - rptr) & mask;
2003 static int MoxaPortTxFree(struct moxa_port *port)
2005 void __iomem *ofsAddr = port->tableAddr;
2006 u16 rptr, wptr, mask;
2008 rptr = readw(ofsAddr + TXrptr);
2009 wptr = readw(ofsAddr + TXwptr);
2010 mask = readw(ofsAddr + TX_mask);
2011 return mask - ((wptr - rptr) & mask);
2014 static int MoxaPortRxQueue(struct moxa_port *port)
2016 void __iomem *ofsAddr = port->tableAddr;
2017 u16 rptr, wptr, mask;
2019 rptr = readw(ofsAddr + RXrptr);
2020 wptr = readw(ofsAddr + RXwptr);
2021 mask = readw(ofsAddr + RX_mask);
2022 return (wptr - rptr) & mask;
2025 static void MoxaPortTxDisable(struct moxa_port *port)
2027 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2030 static void MoxaPortTxEnable(struct moxa_port *port)
2032 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2035 static int moxa_get_serial_info(struct moxa_port *info,
2036 struct serial_struct __user *retinfo)
2038 struct serial_struct tmp = {
2040 .line = info->port.tty->index,
2041 .flags = info->port.flags,
2042 .baud_base = 921600,
2043 .close_delay = info->port.close_delay
2045 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2049 static int moxa_set_serial_info(struct moxa_port *info,
2050 struct serial_struct __user *new_info)
2052 struct serial_struct new_serial;
2054 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2057 if (new_serial.irq != 0 || new_serial.port != 0 ||
2058 new_serial.custom_divisor != 0 ||
2059 new_serial.baud_base != 921600)
2062 if (!capable(CAP_SYS_ADMIN)) {
2063 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2064 (info->port.flags & ~ASYNC_USR_MASK)))
2067 info->port.close_delay = new_serial.close_delay * HZ / 100;
2069 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2070 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
2072 MoxaSetFifo(info, new_serial.type == PORT_16550A);
2074 info->type = new_serial.type;
2080 /*****************************************************************************
2081 * Static local functions: *
2082 *****************************************************************************/
2084 static void MoxaSetFifo(struct moxa_port *port, int enable)
2086 void __iomem *ofsAddr = port->tableAddr;
2089 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2090 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2092 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2093 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);