1 // SPDX-License-Identifier: GPL-2.0+
2 /*****************************************************************************/
4 * moxa.c -- MOXA Intellio family multiport serial driver.
9 * This code is loosely based on the Linux serial driver, written by
10 * Linus Torvalds, Theodore T'so and others.
14 * MOXA Intellio Series Driver
20 #include <linux/module.h>
21 #include <linux/types.h>
23 #include <linux/ioport.h>
24 #include <linux/errno.h>
25 #include <linux/firmware.h>
26 #include <linux/signal.h>
27 #include <linux/sched.h>
28 #include <linux/timer.h>
29 #include <linux/interrupt.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 #include <linux/major.h>
33 #include <linux/string.h>
34 #include <linux/fcntl.h>
35 #include <linux/ptrace.h>
36 #include <linux/serial.h>
37 #include <linux/tty_driver.h>
38 #include <linux/delay.h>
39 #include <linux/pci.h>
40 #include <linux/init.h>
41 #include <linux/bitops.h>
42 #include <linux/slab.h>
43 #include <linux/ratelimit.h>
46 #include <linux/uaccess.h>
50 #define MOXA_VERSION "6.0k"
52 #define MOXA_FW_HDRLEN 32
56 #define MAX_BOARDS 4 /* Don't change this value */
57 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
58 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
60 #define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
61 (brd)->boardType == MOXA_BOARD_C320_PCI)
64 * Define the Moxa PCI vendor and device IDs.
66 #define MOXA_BUS_TYPE_ISA 0
67 #define MOXA_BUS_TYPE_PCI 1
70 MOXA_BOARD_C218_PCI = 1,
77 static char *moxa_brdname[] =
79 "C218 Turbo PCI series",
80 "C218 Turbo ISA series",
81 "C320 Turbo PCI series",
82 "C320 Turbo ISA series",
87 static const struct pci_device_id moxa_pcibrds[] = {
88 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
89 .driver_data = MOXA_BOARD_C218_PCI },
90 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
91 .driver_data = MOXA_BOARD_C320_PCI },
92 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
93 .driver_data = MOXA_BOARD_CP204J },
96 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
97 #endif /* CONFIG_PCI */
101 static struct moxa_board_conf {
108 struct moxa_port *ports;
110 void __iomem *basemem;
111 void __iomem *intNdx;
112 void __iomem *intPend;
113 void __iomem *intTable;
114 } moxa_boards[MAX_BOARDS];
116 struct mxser_mstatus {
130 struct tty_port port;
131 struct moxa_board_conf *board;
132 void __iomem *tableAddr;
136 unsigned long statusflags;
138 u8 DCDState; /* Protected by the port lock */
145 int rxcnt[MAX_PORTS];
146 int txcnt[MAX_PORTS];
155 #define WAKEUP_CHARS 256
157 static int ttymajor = MOXAMAJOR;
158 static struct mon_str moxaLog;
159 static unsigned int moxaFuncTout = HZ / 2;
160 static unsigned int moxaLowWaterChk;
161 static DEFINE_MUTEX(moxa_openlock);
162 static DEFINE_SPINLOCK(moxa_lock);
164 static unsigned long baseaddr[MAX_BOARDS];
165 static unsigned int type[MAX_BOARDS];
166 static unsigned int numports[MAX_BOARDS];
167 static struct tty_port moxa_service_port;
169 MODULE_AUTHOR("William Chen");
170 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
171 MODULE_LICENSE("GPL");
172 MODULE_FIRMWARE("c218tunx.cod");
173 MODULE_FIRMWARE("cp204unx.cod");
174 MODULE_FIRMWARE("c320tunx.cod");
176 module_param_array(type, uint, NULL, 0);
177 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
178 module_param_hw_array(baseaddr, ulong, ioport, NULL, 0);
179 MODULE_PARM_DESC(baseaddr, "base address");
180 module_param_array(numports, uint, NULL, 0);
181 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
183 module_param(ttymajor, int, 0);
188 static int moxa_open(struct tty_struct *, struct file *);
189 static void moxa_close(struct tty_struct *, struct file *);
190 static int moxa_write(struct tty_struct *, const unsigned char *, int);
191 static unsigned int moxa_write_room(struct tty_struct *);
192 static void moxa_flush_buffer(struct tty_struct *);
193 static unsigned int moxa_chars_in_buffer(struct tty_struct *);
194 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
195 static void moxa_stop(struct tty_struct *);
196 static void moxa_start(struct tty_struct *);
197 static void moxa_hangup(struct tty_struct *);
198 static int moxa_tiocmget(struct tty_struct *tty);
199 static int moxa_tiocmset(struct tty_struct *tty,
200 unsigned int set, unsigned int clear);
201 static void moxa_poll(struct timer_list *);
202 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
203 static void moxa_shutdown(struct tty_port *);
204 static int moxa_carrier_raised(struct tty_port *);
205 static void moxa_dtr_rts(struct tty_port *, int);
207 * moxa board interface functions:
209 static void MoxaPortEnable(struct moxa_port *);
210 static void MoxaPortDisable(struct moxa_port *);
211 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
212 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
213 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
214 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
215 static int MoxaPortLineStatus(struct moxa_port *);
216 static void MoxaPortFlushData(struct moxa_port *, int);
217 static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
218 static int MoxaPortReadData(struct moxa_port *);
219 static unsigned int MoxaPortTxQueue(struct moxa_port *);
220 static int MoxaPortRxQueue(struct moxa_port *);
221 static unsigned int MoxaPortTxFree(struct moxa_port *);
222 static void MoxaPortTxDisable(struct moxa_port *);
223 static void MoxaPortTxEnable(struct moxa_port *);
224 static int moxa_get_serial_info(struct tty_struct *, struct serial_struct *);
225 static int moxa_set_serial_info(struct tty_struct *, struct serial_struct *);
226 static void MoxaSetFifo(struct moxa_port *port, int enable);
232 static DEFINE_SPINLOCK(moxafunc_lock);
234 static void moxa_wait_finish(void __iomem *ofsAddr)
236 unsigned long end = jiffies + moxaFuncTout;
238 while (readw(ofsAddr + FuncCode) != 0)
239 if (time_after(jiffies, end))
241 if (readw(ofsAddr + FuncCode) != 0)
242 printk_ratelimited(KERN_WARNING "moxa function expired\n");
245 static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
248 spin_lock_irqsave(&moxafunc_lock, flags);
249 writew(arg, ofsAddr + FuncArg);
250 writew(cmd, ofsAddr + FuncCode);
251 moxa_wait_finish(ofsAddr);
252 spin_unlock_irqrestore(&moxafunc_lock, flags);
255 static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
259 spin_lock_irqsave(&moxafunc_lock, flags);
260 writew(arg, ofsAddr + FuncArg);
261 writew(cmd, ofsAddr + FuncCode);
262 moxa_wait_finish(ofsAddr);
263 ret = readw(ofsAddr + FuncArg);
264 spin_unlock_irqrestore(&moxafunc_lock, flags);
268 static void moxa_low_water_check(void __iomem *ofsAddr)
270 u16 rptr, wptr, mask, len;
272 if (readb(ofsAddr + FlagStat) & Xoff_state) {
273 rptr = readw(ofsAddr + RXrptr);
274 wptr = readw(ofsAddr + RXwptr);
275 mask = readw(ofsAddr + RX_mask);
276 len = (wptr - rptr) & mask;
277 if (len <= Low_water)
278 moxafunc(ofsAddr, FC_SendXon, 0);
286 static int moxa_ioctl(struct tty_struct *tty,
287 unsigned int cmd, unsigned long arg)
289 struct moxa_port *ch = tty->driver_data;
290 void __user *argp = (void __user *)arg;
293 if (tty->index == MAX_PORTS) {
294 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
295 cmd != MOXA_GETMSTATUS)
301 case MOXA_GETDATACOUNT:
302 moxaLog.tick = jiffies;
303 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
306 case MOXA_FLUSH_QUEUE:
307 MoxaPortFlushData(ch, arg);
309 case MOXA_GET_IOQUEUE: {
310 struct moxaq_str __user *argm = argp;
311 struct moxaq_str tmp;
315 for (i = 0; i < MAX_BOARDS; i++) {
316 p = moxa_boards[i].ports;
317 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
318 memset(&tmp, 0, sizeof(tmp));
319 spin_lock_bh(&moxa_lock);
320 if (moxa_boards[i].ready) {
321 tmp.inq = MoxaPortRxQueue(p);
322 tmp.outq = MoxaPortTxQueue(p);
324 spin_unlock_bh(&moxa_lock);
325 if (copy_to_user(argm, &tmp, sizeof(tmp)))
330 } case MOXA_GET_OQUEUE:
331 status = MoxaPortTxQueue(ch);
332 ret = put_user(status, (unsigned long __user *)argp);
334 case MOXA_GET_IQUEUE:
335 status = MoxaPortRxQueue(ch);
336 ret = put_user(status, (unsigned long __user *)argp);
338 case MOXA_GETMSTATUS: {
339 struct mxser_mstatus __user *argm = argp;
340 struct mxser_mstatus tmp;
344 for (i = 0; i < MAX_BOARDS; i++) {
345 p = moxa_boards[i].ports;
346 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
347 struct tty_struct *ttyp;
348 memset(&tmp, 0, sizeof(tmp));
349 spin_lock_bh(&moxa_lock);
350 if (!moxa_boards[i].ready) {
351 spin_unlock_bh(&moxa_lock);
355 status = MoxaPortLineStatus(p);
356 spin_unlock_bh(&moxa_lock);
365 ttyp = tty_port_tty_get(&p->port);
367 tmp.cflag = p->cflag;
369 tmp.cflag = ttyp->termios.c_cflag;
372 if (copy_to_user(argm, &tmp, sizeof(tmp)))
384 static int moxa_break_ctl(struct tty_struct *tty, int state)
386 struct moxa_port *port = tty->driver_data;
388 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
393 static const struct tty_operations moxa_ops = {
397 .write_room = moxa_write_room,
398 .flush_buffer = moxa_flush_buffer,
399 .chars_in_buffer = moxa_chars_in_buffer,
401 .set_termios = moxa_set_termios,
404 .hangup = moxa_hangup,
405 .break_ctl = moxa_break_ctl,
406 .tiocmget = moxa_tiocmget,
407 .tiocmset = moxa_tiocmset,
408 .set_serial = moxa_set_serial_info,
409 .get_serial = moxa_get_serial_info,
412 static const struct tty_port_operations moxa_port_ops = {
413 .carrier_raised = moxa_carrier_raised,
414 .dtr_rts = moxa_dtr_rts,
415 .shutdown = moxa_shutdown,
418 static struct tty_driver *moxaDriver;
419 static DEFINE_TIMER(moxaTimer, moxa_poll);
425 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
427 switch (brd->boardType) {
428 case MOXA_BOARD_C218_ISA:
429 case MOXA_BOARD_C218_PCI:
433 case MOXA_BOARD_CP204J:
447 static int moxa_check_fw(const void *ptr)
449 const __le16 *lptr = ptr;
451 if (*lptr != cpu_to_le16(0x7980))
457 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
460 void __iomem *baseAddr = brd->basemem;
463 writeb(HW_reset, baseAddr + Control_reg); /* reset */
465 memset_io(baseAddr, 0, 4096);
466 memcpy_toio(baseAddr, buf, len); /* download BIOS */
467 writeb(0, baseAddr + Control_reg); /* restart */
471 switch (brd->boardType) {
472 case MOXA_BOARD_C218_ISA:
473 case MOXA_BOARD_C218_PCI:
474 tmp = readw(baseAddr + C218_key);
475 if (tmp != C218_KeyCode)
478 case MOXA_BOARD_CP204J:
479 tmp = readw(baseAddr + C218_key);
480 if (tmp != CP204J_KeyCode)
484 tmp = readw(baseAddr + C320_key);
485 if (tmp != C320_KeyCode)
487 tmp = readw(baseAddr + C320_status);
488 if (tmp != STS_init) {
489 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
490 "module not found\n");
498 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
502 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
505 void __iomem *baseAddr = brd->basemem;
508 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
512 writew(len - 7168 - 2, baseAddr + C320bapi_len);
513 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
514 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
515 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
516 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
521 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
524 void __iomem *baseAddr = brd->basemem;
525 const __le16 *uptr = ptr;
526 size_t wlen, len2, j;
527 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
528 unsigned int i, retry;
531 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
534 switch (brd->boardType) {
535 case MOXA_BOARD_CP204J:
536 case MOXA_BOARD_C218_ISA:
537 case MOXA_BOARD_C218_PCI:
539 loadbuf = C218_LoadBuf;
540 loadlen = C218DLoad_len;
541 checksum = C218check_sum;
542 checksum_ok = C218chksum_ok;
546 keycode = C320_KeyCode;
547 loadbuf = C320_LoadBuf;
548 loadlen = C320DLoad_len;
549 checksum = C320check_sum;
550 checksum_ok = C320chksum_ok;
556 for (i = 0; i < wlen; i++)
557 usum += le16_to_cpu(uptr[i]);
563 len2 = (wlen > 2048) ? 2048 : wlen;
565 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
568 writew(len2, baseAddr + loadlen);
569 writew(0, baseAddr + key);
570 for (i = 0; i < 100; i++) {
571 if (readw(baseAddr + key) == keycode)
575 if (readw(baseAddr + key) != keycode)
578 writew(0, baseAddr + loadlen);
579 writew(usum, baseAddr + checksum);
580 writew(0, baseAddr + key);
581 for (i = 0; i < 100; i++) {
582 if (readw(baseAddr + key) == keycode)
587 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
588 if (readb(baseAddr + checksum_ok) != 1)
591 writew(0, baseAddr + key);
592 for (i = 0; i < 600; i++) {
593 if (readw(baseAddr + Magic_no) == Magic_code)
597 if (readw(baseAddr + Magic_no) != Magic_code)
600 if (MOXA_IS_320(brd)) {
601 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
602 writew(0x3800, baseAddr + TMS320_PORT1);
603 writew(0x3900, baseAddr + TMS320_PORT2);
604 writew(28499, baseAddr + TMS320_CLOCK);
606 writew(0x3200, baseAddr + TMS320_PORT1);
607 writew(0x3400, baseAddr + TMS320_PORT2);
608 writew(19999, baseAddr + TMS320_CLOCK);
611 writew(1, baseAddr + Disable_IRQ);
612 writew(0, baseAddr + Magic_no);
613 for (i = 0; i < 500; i++) {
614 if (readw(baseAddr + Magic_no) == Magic_code)
618 if (readw(baseAddr + Magic_no) != Magic_code)
621 if (MOXA_IS_320(brd)) {
622 j = readw(baseAddr + Module_cnt);
625 brd->numPorts = j * 8;
626 writew(j, baseAddr + Module_no);
627 writew(0, baseAddr + Magic_no);
628 for (i = 0; i < 600; i++) {
629 if (readw(baseAddr + Magic_no) == Magic_code)
633 if (readw(baseAddr + Magic_no) != Magic_code)
636 brd->intNdx = baseAddr + IRQindex;
637 brd->intPend = baseAddr + IRQpending;
638 brd->intTable = baseAddr + IRQtable;
643 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
646 void __iomem *ofsAddr, *baseAddr = brd->basemem;
647 struct moxa_port *port;
651 printk(KERN_ERR "MOXA: bios length is not even\n");
655 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
659 switch (brd->boardType) {
660 case MOXA_BOARD_C218_ISA:
661 case MOXA_BOARD_C218_PCI:
662 case MOXA_BOARD_CP204J:
664 for (i = 0; i < brd->numPorts; i++, port++) {
667 port->tableAddr = baseAddr + Extern_table +
669 ofsAddr = port->tableAddr;
670 writew(C218rx_mask, ofsAddr + RX_mask);
671 writew(C218tx_mask, ofsAddr + TX_mask);
672 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
673 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
675 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
676 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
682 for (i = 0; i < brd->numPorts; i++, port++) {
685 port->tableAddr = baseAddr + Extern_table +
687 ofsAddr = port->tableAddr;
688 switch (brd->numPorts) {
690 writew(C320p8rx_mask, ofsAddr + RX_mask);
691 writew(C320p8tx_mask, ofsAddr + TX_mask);
692 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
693 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
694 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
695 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
699 writew(C320p16rx_mask, ofsAddr + RX_mask);
700 writew(C320p16tx_mask, ofsAddr + TX_mask);
701 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
702 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
703 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
704 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
708 writew(C320p24rx_mask, ofsAddr + RX_mask);
709 writew(C320p24tx_mask, ofsAddr + TX_mask);
710 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
711 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
712 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
713 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
716 writew(C320p32rx_mask, ofsAddr + RX_mask);
717 writew(C320p32tx_mask, ofsAddr + TX_mask);
718 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
719 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
720 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
721 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
722 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
731 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
733 const void *ptr = fw->data;
737 unsigned int a, lenp, lencnt;
740 __le32 magic; /* 0x34303430 */
742 u8 type; /* UNIX = 3 */
743 u8 model; /* C218T=1, C320T=2, CP204=3 */
748 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
750 if (fw->size < MOXA_FW_HDRLEN) {
751 strcpy(rsn, "too short (even header won't fit)");
754 if (hdr->magic != cpu_to_le32(0x30343034)) {
755 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
758 if (hdr->type != 3) {
759 sprintf(rsn, "not for linux, type is %u", hdr->type);
762 if (moxa_check_fw_model(brd, hdr->model)) {
763 sprintf(rsn, "not for this card, model is %u", hdr->model);
767 len = MOXA_FW_HDRLEN;
768 lencnt = hdr->model == 2 ? 5 : 3;
769 for (a = 0; a < ARRAY_SIZE(lens); a++) {
770 lens[a] = le16_to_cpu(hdr->len[a]);
771 if (lens[a] && len + lens[a] <= fw->size &&
772 moxa_check_fw(&fw->data[len]))
773 printk(KERN_WARNING "MOXA firmware: unexpected input "
774 "at offset %u, but going on\n", (u32)len);
775 if (!lens[a] && a < lencnt) {
776 sprintf(rsn, "too few entries in fw file");
782 if (len != fw->size) {
783 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
788 ptr += MOXA_FW_HDRLEN;
791 strcpy(rsn, "read above");
793 ret = moxa_load_bios(brd, ptr, lens[lenp]);
797 /* we skip the tty section (lens[1]), since we don't need it */
798 ptr += lens[lenp] + lens[lenp + 1];
799 lenp += 2; /* comm */
801 if (hdr->model == 2) {
802 ret = moxa_load_320b(brd, ptr, lens[lenp]);
805 /* skip another tty */
806 ptr += lens[lenp] + lens[lenp + 1];
810 ret = moxa_load_code(brd, ptr, lens[lenp]);
816 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
820 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
822 const struct firmware *fw;
825 unsigned int i, first_idx;
828 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
830 if (brd->ports == NULL) {
831 printk(KERN_ERR "cannot allocate memory for ports\n");
836 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
837 tty_port_init(&p->port);
838 p->port.ops = &moxa_port_ops;
839 p->type = PORT_16550A;
840 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
843 switch (brd->boardType) {
844 case MOXA_BOARD_C218_ISA:
845 case MOXA_BOARD_C218_PCI:
846 file = "c218tunx.cod";
848 case MOXA_BOARD_CP204J:
849 file = "cp204unx.cod";
852 file = "c320tunx.cod";
856 ret = request_firmware(&fw, file, dev);
858 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
859 "you've placed '%s' file into your firmware "
860 "loader directory (e.g. /lib/firmware)\n",
865 ret = moxa_load_fw(brd, fw);
867 release_firmware(fw);
872 spin_lock_bh(&moxa_lock);
874 if (!timer_pending(&moxaTimer))
875 mod_timer(&moxaTimer, jiffies + HZ / 50);
876 spin_unlock_bh(&moxa_lock);
878 first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
879 for (i = 0; i < brd->numPorts; i++)
880 tty_port_register_device(&brd->ports[i].port, moxaDriver,
885 for (i = 0; i < MAX_PORTS_PER_BOARD; i++)
886 tty_port_destroy(&brd->ports[i].port);
892 static void moxa_board_deinit(struct moxa_board_conf *brd)
894 unsigned int a, opened, first_idx;
896 mutex_lock(&moxa_openlock);
897 spin_lock_bh(&moxa_lock);
899 spin_unlock_bh(&moxa_lock);
901 /* pci hot-un-plug support */
902 for (a = 0; a < brd->numPorts; a++)
903 if (tty_port_initialized(&brd->ports[a].port))
904 tty_port_tty_hangup(&brd->ports[a].port, false);
906 for (a = 0; a < MAX_PORTS_PER_BOARD; a++)
907 tty_port_destroy(&brd->ports[a].port);
911 for (a = 0; a < brd->numPorts; a++)
912 if (tty_port_initialized(&brd->ports[a].port))
914 mutex_unlock(&moxa_openlock);
918 mutex_lock(&moxa_openlock);
921 first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
922 for (a = 0; a < brd->numPorts; a++)
923 tty_unregister_device(moxaDriver, first_idx + a);
925 iounmap(brd->basemem);
931 static int moxa_pci_probe(struct pci_dev *pdev,
932 const struct pci_device_id *ent)
934 struct moxa_board_conf *board;
936 int board_type = ent->driver_data;
939 retval = pci_enable_device(pdev);
941 dev_err(&pdev->dev, "can't enable pci device\n");
945 for (i = 0; i < MAX_BOARDS; i++)
946 if (moxa_boards[i].basemem == NULL)
950 if (i >= MAX_BOARDS) {
951 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
952 "found. Board is ignored.\n", MAX_BOARDS);
956 board = &moxa_boards[i];
958 retval = pci_request_region(pdev, 2, "moxa-base");
960 dev_err(&pdev->dev, "can't request pci region 2\n");
964 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
965 if (board->basemem == NULL) {
966 dev_err(&pdev->dev, "can't remap io space 2\n");
971 board->boardType = board_type;
972 switch (board_type) {
973 case MOXA_BOARD_C218_ISA:
974 case MOXA_BOARD_C218_PCI:
978 case MOXA_BOARD_CP204J:
985 board->busType = MOXA_BUS_TYPE_PCI;
987 retval = moxa_init_board(board, &pdev->dev);
991 pci_set_drvdata(pdev, board);
993 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
994 moxa_brdname[board_type - 1], board->numPorts);
998 iounmap(board->basemem);
999 board->basemem = NULL;
1001 pci_release_region(pdev, 2);
1006 static void moxa_pci_remove(struct pci_dev *pdev)
1008 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
1010 moxa_board_deinit(brd);
1012 pci_release_region(pdev, 2);
1015 static struct pci_driver moxa_pci_driver = {
1017 .id_table = moxa_pcibrds,
1018 .probe = moxa_pci_probe,
1019 .remove = moxa_pci_remove
1021 #endif /* CONFIG_PCI */
1023 static int __init moxa_init(void)
1025 unsigned int isabrds = 0;
1027 struct moxa_board_conf *brd = moxa_boards;
1030 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1033 tty_port_init(&moxa_service_port);
1035 moxaDriver = tty_alloc_driver(MAX_PORTS + 1,
1036 TTY_DRIVER_REAL_RAW |
1037 TTY_DRIVER_DYNAMIC_DEV);
1038 if (IS_ERR(moxaDriver))
1039 return PTR_ERR(moxaDriver);
1041 moxaDriver->name = "ttyMX";
1042 moxaDriver->major = ttymajor;
1043 moxaDriver->minor_start = 0;
1044 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1045 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1046 moxaDriver->init_termios = tty_std_termios;
1047 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1048 moxaDriver->init_termios.c_ispeed = 9600;
1049 moxaDriver->init_termios.c_ospeed = 9600;
1050 tty_set_operations(moxaDriver, &moxa_ops);
1051 /* Having one more port only for ioctls is ugly */
1052 tty_port_link_device(&moxa_service_port, moxaDriver, MAX_PORTS);
1054 if (tty_register_driver(moxaDriver)) {
1055 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1056 tty_driver_kref_put(moxaDriver);
1060 /* Find the boards defined from module args. */
1062 for (i = 0; i < MAX_BOARDS; i++) {
1065 if (type[i] == MOXA_BOARD_C218_ISA ||
1066 type[i] == MOXA_BOARD_C320_ISA) {
1067 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1068 isabrds + 1, moxa_brdname[type[i] - 1],
1070 brd->boardType = type[i];
1071 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1073 brd->busType = MOXA_BUS_TYPE_ISA;
1074 brd->basemem = ioremap(baseaddr[i], 0x4000);
1075 if (!brd->basemem) {
1076 printk(KERN_ERR "MOXA: can't remap %lx\n",
1080 if (moxa_init_board(brd, NULL)) {
1081 iounmap(brd->basemem);
1082 brd->basemem = NULL;
1086 printk(KERN_INFO "MOXA isa board found at 0x%.8lx and "
1087 "ready (%u ports, firmware loaded)\n",
1088 baseaddr[i], brd->numPorts);
1096 retval = pci_register_driver(&moxa_pci_driver);
1098 printk(KERN_ERR "Can't register MOXA pci driver!\n");
1107 static void __exit moxa_exit(void)
1112 pci_unregister_driver(&moxa_pci_driver);
1115 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1116 if (moxa_boards[i].ready)
1117 moxa_board_deinit(&moxa_boards[i]);
1119 del_timer_sync(&moxaTimer);
1121 tty_unregister_driver(moxaDriver);
1122 tty_driver_kref_put(moxaDriver);
1125 module_init(moxa_init);
1126 module_exit(moxa_exit);
1128 static void moxa_shutdown(struct tty_port *port)
1130 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1131 MoxaPortDisable(ch);
1132 MoxaPortFlushData(ch, 2);
1135 static int moxa_carrier_raised(struct tty_port *port)
1137 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1140 spin_lock_irq(&port->lock);
1142 spin_unlock_irq(&port->lock);
1146 static void moxa_dtr_rts(struct tty_port *port, int onoff)
1148 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1149 MoxaPortLineCtrl(ch, onoff, onoff);
1153 static int moxa_open(struct tty_struct *tty, struct file *filp)
1155 struct moxa_board_conf *brd;
1156 struct moxa_port *ch;
1160 if (port == MAX_PORTS) {
1161 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1163 if (mutex_lock_interruptible(&moxa_openlock))
1164 return -ERESTARTSYS;
1165 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1167 mutex_unlock(&moxa_openlock);
1171 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1172 mutex_unlock(&moxa_openlock);
1176 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1178 tty->driver_data = ch;
1179 tty_port_tty_set(&ch->port, tty);
1180 mutex_lock(&ch->port.mutex);
1181 if (!tty_port_initialized(&ch->port)) {
1182 ch->statusflags = 0;
1183 moxa_set_tty_param(tty, &tty->termios);
1184 MoxaPortLineCtrl(ch, 1, 1);
1186 MoxaSetFifo(ch, ch->type == PORT_16550A);
1187 tty_port_set_initialized(&ch->port, 1);
1189 mutex_unlock(&ch->port.mutex);
1190 mutex_unlock(&moxa_openlock);
1192 return tty_port_block_til_ready(&ch->port, tty, filp);
1195 static void moxa_close(struct tty_struct *tty, struct file *filp)
1197 struct moxa_port *ch = tty->driver_data;
1198 ch->cflag = tty->termios.c_cflag;
1199 tty_port_close(&ch->port, tty, filp);
1202 static int moxa_write(struct tty_struct *tty,
1203 const unsigned char *buf, int count)
1205 struct moxa_port *ch = tty->driver_data;
1206 unsigned long flags;
1212 spin_lock_irqsave(&moxa_lock, flags);
1213 len = MoxaPortWriteData(tty, buf, count);
1214 spin_unlock_irqrestore(&moxa_lock, flags);
1216 set_bit(LOWWAIT, &ch->statusflags);
1220 static unsigned int moxa_write_room(struct tty_struct *tty)
1222 struct moxa_port *ch;
1224 if (tty->flow.stopped)
1226 ch = tty->driver_data;
1229 return MoxaPortTxFree(ch);
1232 static void moxa_flush_buffer(struct tty_struct *tty)
1234 struct moxa_port *ch = tty->driver_data;
1238 MoxaPortFlushData(ch, 1);
1242 static unsigned int moxa_chars_in_buffer(struct tty_struct *tty)
1244 struct moxa_port *ch = tty->driver_data;
1247 chars = MoxaPortTxQueue(ch);
1250 * Make it possible to wakeup anything waiting for output
1251 * in tty_ioctl.c, etc.
1253 set_bit(EMPTYWAIT, &ch->statusflags);
1257 static int moxa_tiocmget(struct tty_struct *tty)
1259 struct moxa_port *ch = tty->driver_data;
1260 int flag = 0, dtr, rts;
1262 MoxaPortGetLineOut(ch, &dtr, &rts);
1267 dtr = MoxaPortLineStatus(ch);
1277 static int moxa_tiocmset(struct tty_struct *tty,
1278 unsigned int set, unsigned int clear)
1280 struct moxa_port *ch;
1283 mutex_lock(&moxa_openlock);
1284 ch = tty->driver_data;
1286 mutex_unlock(&moxa_openlock);
1290 MoxaPortGetLineOut(ch, &dtr, &rts);
1291 if (set & TIOCM_RTS)
1293 if (set & TIOCM_DTR)
1295 if (clear & TIOCM_RTS)
1297 if (clear & TIOCM_DTR)
1299 MoxaPortLineCtrl(ch, dtr, rts);
1300 mutex_unlock(&moxa_openlock);
1304 static void moxa_set_termios(struct tty_struct *tty,
1305 struct ktermios *old_termios)
1307 struct moxa_port *ch = tty->driver_data;
1311 moxa_set_tty_param(tty, old_termios);
1312 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1313 wake_up_interruptible(&ch->port.open_wait);
1316 static void moxa_stop(struct tty_struct *tty)
1318 struct moxa_port *ch = tty->driver_data;
1322 MoxaPortTxDisable(ch);
1323 set_bit(TXSTOPPED, &ch->statusflags);
1327 static void moxa_start(struct tty_struct *tty)
1329 struct moxa_port *ch = tty->driver_data;
1334 if (!test_bit(TXSTOPPED, &ch->statusflags))
1337 MoxaPortTxEnable(ch);
1338 clear_bit(TXSTOPPED, &ch->statusflags);
1341 static void moxa_hangup(struct tty_struct *tty)
1343 struct moxa_port *ch = tty->driver_data;
1344 tty_port_hangup(&ch->port);
1347 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1349 unsigned long flags;
1352 spin_lock_irqsave(&p->port.lock, flags);
1353 if (dcd != p->DCDState) {
1355 spin_unlock_irqrestore(&p->port.lock, flags);
1357 tty_port_tty_hangup(&p->port, true);
1360 spin_unlock_irqrestore(&p->port.lock, flags);
1363 static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1366 struct tty_struct *tty = tty_port_tty_get(&p->port);
1367 void __iomem *ofsAddr;
1368 unsigned int inited = tty_port_initialized(&p->port);
1372 if (test_bit(EMPTYWAIT, &p->statusflags) &&
1373 MoxaPortTxQueue(p) == 0) {
1374 clear_bit(EMPTYWAIT, &p->statusflags);
1377 if (test_bit(LOWWAIT, &p->statusflags) && !tty->flow.stopped &&
1378 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1379 clear_bit(LOWWAIT, &p->statusflags);
1383 if (inited && !tty_throttled(tty) &&
1384 MoxaPortRxQueue(p) > 0) { /* RX */
1385 MoxaPortReadData(p);
1386 tty_schedule_flip(&p->port);
1389 clear_bit(EMPTYWAIT, &p->statusflags);
1390 MoxaPortFlushData(p, 0); /* flush RX */
1393 if (!handle) /* nothing else to do */
1396 intr = readw(ip); /* port irq status */
1400 writew(0, ip); /* ACK port */
1401 ofsAddr = p->tableAddr;
1402 if (intr & IntrTx) /* disable tx intr */
1403 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1404 ofsAddr + HostStat);
1409 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1410 tty_insert_flip_char(&p->port, 0, TTY_BREAK);
1411 tty_schedule_flip(&p->port);
1414 if (intr & IntrLine)
1415 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1422 static void moxa_poll(struct timer_list *unused)
1424 struct moxa_board_conf *brd;
1426 unsigned int card, port, served = 0;
1428 spin_lock(&moxa_lock);
1429 for (card = 0; card < MAX_BOARDS; card++) {
1430 brd = &moxa_boards[card];
1437 if (readb(brd->intPend) == 0xff)
1438 ip = brd->intTable + readb(brd->intNdx);
1440 for (port = 0; port < brd->numPorts; port++)
1441 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1444 writeb(0, brd->intPend); /* ACK */
1446 if (moxaLowWaterChk) {
1447 struct moxa_port *p = brd->ports;
1448 for (port = 0; port < brd->numPorts; port++, p++)
1449 if (p->lowChkFlag) {
1451 moxa_low_water_check(p->tableAddr);
1455 moxaLowWaterChk = 0;
1458 mod_timer(&moxaTimer, jiffies + HZ / 50);
1459 spin_unlock(&moxa_lock);
1462 /******************************************************************************/
1464 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1466 register struct ktermios *ts = &tty->termios;
1467 struct moxa_port *ch = tty->driver_data;
1468 int rts, cts, txflow, rxflow, xany, baud;
1470 rts = cts = txflow = rxflow = xany = 0;
1471 if (ts->c_cflag & CRTSCTS)
1473 if (ts->c_iflag & IXON)
1475 if (ts->c_iflag & IXOFF)
1477 if (ts->c_iflag & IXANY)
1480 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1481 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1483 baud = tty_termios_baud_rate(old_termios);
1484 /* Not put the baud rate into the termios data */
1485 tty_encode_baud_rate(tty, baud, baud);
1488 /*****************************************************************************
1489 * Driver level functions: *
1490 *****************************************************************************/
1492 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1494 void __iomem *ofsAddr;
1495 if (mode < 0 || mode > 2)
1497 ofsAddr = port->tableAddr;
1498 moxafunc(ofsAddr, FC_FlushQueue, mode);
1500 port->lowChkFlag = 0;
1501 moxa_low_water_check(ofsAddr);
1506 * Moxa Port Number Description:
1508 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1509 * the port number using in MOXA driver functions will be 0 to 31 for
1510 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1511 * to 127 for fourth. For example, if you setup three MOXA boards,
1512 * first board is C218, second board is C320-16 and third board is
1513 * C320-32. The port number of first board (C218 - 8 ports) is from
1514 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1515 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1516 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1517 * 127 will be invalid.
1520 * Moxa Functions Description:
1522 * Function 1: Driver initialization routine, this routine must be
1523 * called when initialized driver.
1525 * void MoxaDriverInit();
1528 * Function 2: Moxa driver private IOCTL command processing.
1530 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1532 * unsigned int cmd : IOCTL command
1533 * unsigned long arg : IOCTL argument
1534 * int port : port number (0 - 127)
1541 * Function 6: Enable this port to start Tx/Rx data.
1543 * void MoxaPortEnable(int port);
1544 * int port : port number (0 - 127)
1547 * Function 7: Disable this port
1549 * void MoxaPortDisable(int port);
1550 * int port : port number (0 - 127)
1553 * Function 10: Setting baud rate of this port.
1555 * speed_t MoxaPortSetBaud(int port, speed_t baud);
1556 * int port : port number (0 - 127)
1557 * long baud : baud rate (50 - 115200)
1559 * return: 0 : this port is invalid or baud < 50
1560 * 50 - 115200 : the real baud rate set to the port, if
1561 * the argument baud is large than maximun
1562 * available baud rate, the real setting
1563 * baud rate will be the maximun baud rate.
1566 * Function 12: Configure the port.
1568 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1569 * int port : port number (0 - 127)
1570 * struct ktermios * termio : termio structure pointer
1571 * speed_t baud : baud rate
1573 * return: -1 : this port is invalid or termio == NULL
1577 * Function 13: Get the DTR/RTS state of this port.
1579 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1580 * int port : port number (0 - 127)
1581 * int * dtrState : pointer to INT to receive the current DTR
1582 * state. (if NULL, this function will not
1583 * write to this address)
1584 * int * rtsState : pointer to INT to receive the current RTS
1585 * state. (if NULL, this function will not
1586 * write to this address)
1588 * return: -1 : this port is invalid
1592 * Function 14: Setting the DTR/RTS output state of this port.
1594 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1595 * int port : port number (0 - 127)
1596 * int dtrState : DTR output state (0: off, 1: on)
1597 * int rtsState : RTS output state (0: off, 1: on)
1600 * Function 15: Setting the flow control of this port.
1602 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1603 * int txFlow,int xany);
1604 * int port : port number (0 - 127)
1605 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1606 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1607 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1608 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1609 * int xany : S/W XANY flow control (0: no, 1: yes)
1612 * Function 16: Get ths line status of this port
1614 * int MoxaPortLineStatus(int port);
1615 * int port : port number (0 - 127)
1617 * return: Bit 0 - CTS state (0: off, 1: on)
1618 * Bit 1 - DSR state (0: off, 1: on)
1619 * Bit 2 - DCD state (0: off, 1: on)
1622 * Function 19: Flush the Rx/Tx buffer data of this port.
1624 * void MoxaPortFlushData(int port, int mode);
1625 * int port : port number (0 - 127)
1627 * 0 : flush the Rx buffer
1628 * 1 : flush the Tx buffer
1629 * 2 : flush the Rx and Tx buffer
1632 * Function 20: Write data.
1634 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1635 * int port : port number (0 - 127)
1636 * unsigned char * buffer : pointer to write data buffer.
1637 * int length : write data length
1639 * return: 0 - length : real write data length
1642 * Function 21: Read data.
1644 * int MoxaPortReadData(int port, struct tty_struct *tty);
1645 * int port : port number (0 - 127)
1646 * struct tty_struct *tty : tty for data
1648 * return: 0 - length : real read data length
1651 * Function 24: Get the Tx buffer current queued data bytes
1653 * int MoxaPortTxQueue(int port);
1654 * int port : port number (0 - 127)
1656 * return: .. : Tx buffer current queued data bytes
1659 * Function 25: Get the Tx buffer current free space
1661 * int MoxaPortTxFree(int port);
1662 * int port : port number (0 - 127)
1664 * return: .. : Tx buffer current free space
1667 * Function 26: Get the Rx buffer current queued data bytes
1669 * int MoxaPortRxQueue(int port);
1670 * int port : port number (0 - 127)
1672 * return: .. : Rx buffer current queued data bytes
1675 * Function 28: Disable port data transmission.
1677 * void MoxaPortTxDisable(int port);
1678 * int port : port number (0 - 127)
1681 * Function 29: Enable port data transmission.
1683 * void MoxaPortTxEnable(int port);
1684 * int port : port number (0 - 127)
1687 * Function 31: Get the received BREAK signal count and reset it.
1689 * int MoxaPortResetBrkCnt(int port);
1690 * int port : port number (0 - 127)
1692 * return: 0 - .. : BREAK signal count
1697 static void MoxaPortEnable(struct moxa_port *port)
1699 void __iomem *ofsAddr;
1702 ofsAddr = port->tableAddr;
1703 writew(lowwater, ofsAddr + Low_water);
1704 if (MOXA_IS_320(port->board))
1705 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1707 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1708 ofsAddr + HostStat);
1710 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1711 moxafunc(ofsAddr, FC_FlushQueue, 2);
1713 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1714 MoxaPortLineStatus(port);
1717 static void MoxaPortDisable(struct moxa_port *port)
1719 void __iomem *ofsAddr = port->tableAddr;
1721 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1722 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1723 writew(0, ofsAddr + HostStat);
1724 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1727 static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
1729 void __iomem *ofsAddr = port->tableAddr;
1730 unsigned int clock, val;
1733 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1740 moxafunc(ofsAddr, FC_SetBaud, val);
1745 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1748 void __iomem *ofsAddr;
1751 ofsAddr = port->tableAddr;
1753 mode = termio->c_cflag & CSIZE;
1756 else if (mode == CS6)
1758 else if (mode == CS7)
1760 else if (mode == CS8)
1763 if (termio->c_cflag & CSTOPB) {
1771 if (termio->c_cflag & PARENB) {
1772 if (termio->c_cflag & PARODD) {
1773 if (termio->c_cflag & CMSPAR)
1778 if (termio->c_cflag & CMSPAR)
1779 mode |= MX_PARSPACE;
1786 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
1788 if (MOXA_IS_320(port->board) && baud >= 921600)
1791 baud = MoxaPortSetBaud(port, baud);
1793 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1794 spin_lock_irq(&moxafunc_lock);
1795 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1796 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1797 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1798 moxa_wait_finish(ofsAddr);
1799 spin_unlock_irq(&moxafunc_lock);
1805 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1809 *dtrState = !!(port->lineCtrl & DTR_ON);
1811 *rtsState = !!(port->lineCtrl & RTS_ON);
1816 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1824 port->lineCtrl = mode;
1825 moxafunc(port->tableAddr, FC_LineControl, mode);
1828 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1829 int txflow, int rxflow, int txany)
1834 mode |= RTS_FlowCtl;
1836 mode |= CTS_FlowCtl;
1843 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
1846 static int MoxaPortLineStatus(struct moxa_port *port)
1848 void __iomem *ofsAddr;
1851 ofsAddr = port->tableAddr;
1852 if (MOXA_IS_320(port->board))
1853 val = moxafuncret(ofsAddr, FC_LineStatus, 0);
1855 val = readw(ofsAddr + FlagStat) >> 4;
1859 moxa_new_dcdstate(port, val & 8);
1864 static int MoxaPortWriteData(struct tty_struct *tty,
1865 const unsigned char *buffer, int len)
1867 struct moxa_port *port = tty->driver_data;
1868 void __iomem *baseAddr, *ofsAddr, *ofs;
1869 unsigned int c, total;
1870 u16 head, tail, tx_mask, spage, epage;
1871 u16 pageno, pageofs, bufhead;
1873 ofsAddr = port->tableAddr;
1874 baseAddr = port->board->basemem;
1875 tx_mask = readw(ofsAddr + TX_mask);
1876 spage = readw(ofsAddr + Page_txb);
1877 epage = readw(ofsAddr + EndPage_txb);
1878 tail = readw(ofsAddr + TXwptr);
1879 head = readw(ofsAddr + TXrptr);
1880 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
1883 moxaLog.txcnt[port->port.tty->index] += c;
1885 if (spage == epage) {
1886 bufhead = readw(ofsAddr + Ofs_txb);
1887 writew(spage, baseAddr + Control_reg);
1890 len = head - tail - 1;
1892 len = tx_mask + 1 - tail;
1893 len = (c > len) ? len : c;
1894 ofs = baseAddr + DynPage_addr + bufhead + tail;
1895 memcpy_toio(ofs, buffer, len);
1897 tail = (tail + len) & tx_mask;
1901 pageno = spage + (tail >> 13);
1902 pageofs = tail & Page_mask;
1904 len = Page_size - pageofs;
1907 writeb(pageno, baseAddr + Control_reg);
1908 ofs = baseAddr + DynPage_addr + pageofs;
1909 memcpy_toio(ofs, buffer, len);
1911 if (++pageno == epage)
1916 tail = (tail + total) & tx_mask;
1918 writew(tail, ofsAddr + TXwptr);
1919 writeb(1, ofsAddr + CD180TXirq); /* start to send */
1923 static int MoxaPortReadData(struct moxa_port *port)
1925 struct tty_struct *tty = port->port.tty;
1927 void __iomem *baseAddr, *ofsAddr, *ofs;
1928 unsigned int count, len, total;
1929 u16 tail, rx_mask, spage, epage;
1930 u16 pageno, pageofs, bufhead, head;
1932 ofsAddr = port->tableAddr;
1933 baseAddr = port->board->basemem;
1934 head = readw(ofsAddr + RXrptr);
1935 tail = readw(ofsAddr + RXwptr);
1936 rx_mask = readw(ofsAddr + RX_mask);
1937 spage = readw(ofsAddr + Page_rxb);
1938 epage = readw(ofsAddr + EndPage_rxb);
1939 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
1944 moxaLog.rxcnt[tty->index] += total;
1945 if (spage == epage) {
1946 bufhead = readw(ofsAddr + Ofs_rxb);
1947 writew(spage, baseAddr + Control_reg);
1949 ofs = baseAddr + DynPage_addr + bufhead + head;
1950 len = (tail >= head) ? (tail - head) :
1951 (rx_mask + 1 - head);
1952 len = tty_prepare_flip_string(&port->port, &dst,
1954 memcpy_fromio(dst, ofs, len);
1955 head = (head + len) & rx_mask;
1959 pageno = spage + (head >> 13);
1960 pageofs = head & Page_mask;
1962 writew(pageno, baseAddr + Control_reg);
1963 ofs = baseAddr + DynPage_addr + pageofs;
1964 len = tty_prepare_flip_string(&port->port, &dst,
1965 min(Page_size - pageofs, count));
1966 memcpy_fromio(dst, ofs, len);
1969 pageofs = (pageofs + len) & Page_mask;
1970 if (pageofs == 0 && ++pageno == epage)
1973 head = (head + total) & rx_mask;
1975 writew(head, ofsAddr + RXrptr);
1976 if (readb(ofsAddr + FlagStat) & Xoff_state) {
1977 moxaLowWaterChk = 1;
1978 port->lowChkFlag = 1;
1984 static unsigned int MoxaPortTxQueue(struct moxa_port *port)
1986 void __iomem *ofsAddr = port->tableAddr;
1987 u16 rptr, wptr, mask;
1989 rptr = readw(ofsAddr + TXrptr);
1990 wptr = readw(ofsAddr + TXwptr);
1991 mask = readw(ofsAddr + TX_mask);
1992 return (wptr - rptr) & mask;
1995 static unsigned int MoxaPortTxFree(struct moxa_port *port)
1997 void __iomem *ofsAddr = port->tableAddr;
1998 u16 rptr, wptr, mask;
2000 rptr = readw(ofsAddr + TXrptr);
2001 wptr = readw(ofsAddr + TXwptr);
2002 mask = readw(ofsAddr + TX_mask);
2003 return mask - ((wptr - rptr) & mask);
2006 static int MoxaPortRxQueue(struct moxa_port *port)
2008 void __iomem *ofsAddr = port->tableAddr;
2009 u16 rptr, wptr, mask;
2011 rptr = readw(ofsAddr + RXrptr);
2012 wptr = readw(ofsAddr + RXwptr);
2013 mask = readw(ofsAddr + RX_mask);
2014 return (wptr - rptr) & mask;
2017 static void MoxaPortTxDisable(struct moxa_port *port)
2019 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2022 static void MoxaPortTxEnable(struct moxa_port *port)
2024 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2027 static int moxa_get_serial_info(struct tty_struct *tty,
2028 struct serial_struct *ss)
2030 struct moxa_port *info = tty->driver_data;
2032 if (tty->index == MAX_PORTS)
2036 mutex_lock(&info->port.mutex);
2037 ss->type = info->type;
2038 ss->line = info->port.tty->index;
2039 ss->flags = info->port.flags;
2040 ss->baud_base = 921600;
2041 ss->close_delay = jiffies_to_msecs(info->port.close_delay) / 10;
2042 mutex_unlock(&info->port.mutex);
2047 static int moxa_set_serial_info(struct tty_struct *tty,
2048 struct serial_struct *ss)
2050 struct moxa_port *info = tty->driver_data;
2051 unsigned int close_delay;
2053 if (tty->index == MAX_PORTS)
2058 close_delay = msecs_to_jiffies(ss->close_delay * 10);
2060 mutex_lock(&info->port.mutex);
2061 if (!capable(CAP_SYS_ADMIN)) {
2062 if (close_delay != info->port.close_delay ||
2063 ss->type != info->type ||
2064 ((ss->flags & ~ASYNC_USR_MASK) !=
2065 (info->port.flags & ~ASYNC_USR_MASK))) {
2066 mutex_unlock(&info->port.mutex);
2070 info->port.close_delay = close_delay;
2072 MoxaSetFifo(info, ss->type == PORT_16550A);
2074 info->type = ss->type;
2076 mutex_unlock(&info->port.mutex);
2082 /*****************************************************************************
2083 * Static local functions: *
2084 *****************************************************************************/
2086 static void MoxaSetFifo(struct moxa_port *port, int enable)
2088 void __iomem *ofsAddr = port->tableAddr;
2091 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2092 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2094 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2095 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);