1 // SPDX-License-Identifier: GPL-2.0+
2 /*****************************************************************************/
4 * moxa.c -- MOXA Intellio family multiport serial driver.
6 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com).
7 * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
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");
174 module_param_array(type, uint, NULL, 0);
175 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
176 module_param_hw_array(baseaddr, ulong, ioport, NULL, 0);
177 MODULE_PARM_DESC(baseaddr, "base address");
178 module_param_array(numports, uint, NULL, 0);
179 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
181 module_param(ttymajor, int, 0);
186 static int moxa_open(struct tty_struct *, struct file *);
187 static void moxa_close(struct tty_struct *, struct file *);
188 static int moxa_write(struct tty_struct *, const unsigned char *, int);
189 static int moxa_write_room(struct tty_struct *);
190 static void moxa_flush_buffer(struct tty_struct *);
191 static int moxa_chars_in_buffer(struct tty_struct *);
192 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
193 static void moxa_stop(struct tty_struct *);
194 static void moxa_start(struct tty_struct *);
195 static void moxa_hangup(struct tty_struct *);
196 static int moxa_tiocmget(struct tty_struct *tty);
197 static int moxa_tiocmset(struct tty_struct *tty,
198 unsigned int set, unsigned int clear);
199 static void moxa_poll(struct timer_list *);
200 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
201 static void moxa_shutdown(struct tty_port *);
202 static int moxa_carrier_raised(struct tty_port *);
203 static void moxa_dtr_rts(struct tty_port *, int);
205 * moxa board interface functions:
207 static void MoxaPortEnable(struct moxa_port *);
208 static void MoxaPortDisable(struct moxa_port *);
209 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
210 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
211 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
212 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
213 static int MoxaPortLineStatus(struct moxa_port *);
214 static void MoxaPortFlushData(struct moxa_port *, int);
215 static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
216 static int MoxaPortReadData(struct moxa_port *);
217 static int MoxaPortTxQueue(struct moxa_port *);
218 static int MoxaPortRxQueue(struct moxa_port *);
219 static int MoxaPortTxFree(struct moxa_port *);
220 static void MoxaPortTxDisable(struct moxa_port *);
221 static void MoxaPortTxEnable(struct moxa_port *);
222 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
223 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
224 static void MoxaSetFifo(struct moxa_port *port, int enable);
230 static DEFINE_SPINLOCK(moxafunc_lock);
232 static void moxa_wait_finish(void __iomem *ofsAddr)
234 unsigned long end = jiffies + moxaFuncTout;
236 while (readw(ofsAddr + FuncCode) != 0)
237 if (time_after(jiffies, end))
239 if (readw(ofsAddr + FuncCode) != 0)
240 printk_ratelimited(KERN_WARNING "moxa function expired\n");
243 static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
246 spin_lock_irqsave(&moxafunc_lock, flags);
247 writew(arg, ofsAddr + FuncArg);
248 writew(cmd, ofsAddr + FuncCode);
249 moxa_wait_finish(ofsAddr);
250 spin_unlock_irqrestore(&moxafunc_lock, flags);
253 static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
257 spin_lock_irqsave(&moxafunc_lock, flags);
258 writew(arg, ofsAddr + FuncArg);
259 writew(cmd, ofsAddr + FuncCode);
260 moxa_wait_finish(ofsAddr);
261 ret = readw(ofsAddr + FuncArg);
262 spin_unlock_irqrestore(&moxafunc_lock, flags);
266 static void moxa_low_water_check(void __iomem *ofsAddr)
268 u16 rptr, wptr, mask, len;
270 if (readb(ofsAddr + FlagStat) & Xoff_state) {
271 rptr = readw(ofsAddr + RXrptr);
272 wptr = readw(ofsAddr + RXwptr);
273 mask = readw(ofsAddr + RX_mask);
274 len = (wptr - rptr) & mask;
275 if (len <= Low_water)
276 moxafunc(ofsAddr, FC_SendXon, 0);
284 static int moxa_ioctl(struct tty_struct *tty,
285 unsigned int cmd, unsigned long arg)
287 struct moxa_port *ch = tty->driver_data;
288 void __user *argp = (void __user *)arg;
291 if (tty->index == MAX_PORTS) {
292 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
293 cmd != MOXA_GETMSTATUS)
299 case MOXA_GETDATACOUNT:
300 moxaLog.tick = jiffies;
301 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
304 case MOXA_FLUSH_QUEUE:
305 MoxaPortFlushData(ch, arg);
307 case MOXA_GET_IOQUEUE: {
308 struct moxaq_str __user *argm = argp;
309 struct moxaq_str tmp;
313 for (i = 0; i < MAX_BOARDS; i++) {
314 p = moxa_boards[i].ports;
315 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
316 memset(&tmp, 0, sizeof(tmp));
317 spin_lock_bh(&moxa_lock);
318 if (moxa_boards[i].ready) {
319 tmp.inq = MoxaPortRxQueue(p);
320 tmp.outq = MoxaPortTxQueue(p);
322 spin_unlock_bh(&moxa_lock);
323 if (copy_to_user(argm, &tmp, sizeof(tmp)))
328 } case MOXA_GET_OQUEUE:
329 status = MoxaPortTxQueue(ch);
330 ret = put_user(status, (unsigned long __user *)argp);
332 case MOXA_GET_IQUEUE:
333 status = MoxaPortRxQueue(ch);
334 ret = put_user(status, (unsigned long __user *)argp);
336 case MOXA_GETMSTATUS: {
337 struct mxser_mstatus __user *argm = argp;
338 struct mxser_mstatus tmp;
342 for (i = 0; i < MAX_BOARDS; i++) {
343 p = moxa_boards[i].ports;
344 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
345 struct tty_struct *ttyp;
346 memset(&tmp, 0, sizeof(tmp));
347 spin_lock_bh(&moxa_lock);
348 if (!moxa_boards[i].ready) {
349 spin_unlock_bh(&moxa_lock);
353 status = MoxaPortLineStatus(p);
354 spin_unlock_bh(&moxa_lock);
363 ttyp = tty_port_tty_get(&p->port);
365 tmp.cflag = p->cflag;
367 tmp.cflag = ttyp->termios.c_cflag;
370 if (copy_to_user(argm, &tmp, sizeof(tmp)))
377 mutex_lock(&ch->port.mutex);
378 ret = moxa_get_serial_info(ch, argp);
379 mutex_unlock(&ch->port.mutex);
382 mutex_lock(&ch->port.mutex);
383 ret = moxa_set_serial_info(ch, argp);
384 mutex_unlock(&ch->port.mutex);
392 static int moxa_break_ctl(struct tty_struct *tty, int state)
394 struct moxa_port *port = tty->driver_data;
396 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
401 static const struct tty_operations moxa_ops = {
405 .write_room = moxa_write_room,
406 .flush_buffer = moxa_flush_buffer,
407 .chars_in_buffer = moxa_chars_in_buffer,
409 .set_termios = moxa_set_termios,
412 .hangup = moxa_hangup,
413 .break_ctl = moxa_break_ctl,
414 .tiocmget = moxa_tiocmget,
415 .tiocmset = moxa_tiocmset,
418 static const struct tty_port_operations moxa_port_ops = {
419 .carrier_raised = moxa_carrier_raised,
420 .dtr_rts = moxa_dtr_rts,
421 .shutdown = moxa_shutdown,
424 static struct tty_driver *moxaDriver;
425 static DEFINE_TIMER(moxaTimer, moxa_poll);
431 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
433 switch (brd->boardType) {
434 case MOXA_BOARD_C218_ISA:
435 case MOXA_BOARD_C218_PCI:
439 case MOXA_BOARD_CP204J:
453 static int moxa_check_fw(const void *ptr)
455 const __le16 *lptr = ptr;
457 if (*lptr != cpu_to_le16(0x7980))
463 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
466 void __iomem *baseAddr = brd->basemem;
469 writeb(HW_reset, baseAddr + Control_reg); /* reset */
471 memset_io(baseAddr, 0, 4096);
472 memcpy_toio(baseAddr, buf, len); /* download BIOS */
473 writeb(0, baseAddr + Control_reg); /* restart */
477 switch (brd->boardType) {
478 case MOXA_BOARD_C218_ISA:
479 case MOXA_BOARD_C218_PCI:
480 tmp = readw(baseAddr + C218_key);
481 if (tmp != C218_KeyCode)
484 case MOXA_BOARD_CP204J:
485 tmp = readw(baseAddr + C218_key);
486 if (tmp != CP204J_KeyCode)
490 tmp = readw(baseAddr + C320_key);
491 if (tmp != C320_KeyCode)
493 tmp = readw(baseAddr + C320_status);
494 if (tmp != STS_init) {
495 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
496 "module not found\n");
504 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
508 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
511 void __iomem *baseAddr = brd->basemem;
514 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
518 writew(len - 7168 - 2, baseAddr + C320bapi_len);
519 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
520 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
521 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
522 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
527 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
530 void __iomem *baseAddr = brd->basemem;
531 const __le16 *uptr = ptr;
532 size_t wlen, len2, j;
533 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
534 unsigned int i, retry;
537 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
540 switch (brd->boardType) {
541 case MOXA_BOARD_CP204J:
542 case MOXA_BOARD_C218_ISA:
543 case MOXA_BOARD_C218_PCI:
545 loadbuf = C218_LoadBuf;
546 loadlen = C218DLoad_len;
547 checksum = C218check_sum;
548 checksum_ok = C218chksum_ok;
552 keycode = C320_KeyCode;
553 loadbuf = C320_LoadBuf;
554 loadlen = C320DLoad_len;
555 checksum = C320check_sum;
556 checksum_ok = C320chksum_ok;
562 for (i = 0; i < wlen; i++)
563 usum += le16_to_cpu(uptr[i]);
569 len2 = (wlen > 2048) ? 2048 : wlen;
571 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
574 writew(len2, baseAddr + loadlen);
575 writew(0, baseAddr + key);
576 for (i = 0; i < 100; i++) {
577 if (readw(baseAddr + key) == keycode)
581 if (readw(baseAddr + key) != keycode)
584 writew(0, baseAddr + loadlen);
585 writew(usum, baseAddr + checksum);
586 writew(0, baseAddr + key);
587 for (i = 0; i < 100; i++) {
588 if (readw(baseAddr + key) == keycode)
593 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
594 if (readb(baseAddr + checksum_ok) != 1)
597 writew(0, baseAddr + key);
598 for (i = 0; i < 600; i++) {
599 if (readw(baseAddr + Magic_no) == Magic_code)
603 if (readw(baseAddr + Magic_no) != Magic_code)
606 if (MOXA_IS_320(brd)) {
607 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
608 writew(0x3800, baseAddr + TMS320_PORT1);
609 writew(0x3900, baseAddr + TMS320_PORT2);
610 writew(28499, baseAddr + TMS320_CLOCK);
612 writew(0x3200, baseAddr + TMS320_PORT1);
613 writew(0x3400, baseAddr + TMS320_PORT2);
614 writew(19999, baseAddr + TMS320_CLOCK);
617 writew(1, baseAddr + Disable_IRQ);
618 writew(0, baseAddr + Magic_no);
619 for (i = 0; i < 500; i++) {
620 if (readw(baseAddr + Magic_no) == Magic_code)
624 if (readw(baseAddr + Magic_no) != Magic_code)
627 if (MOXA_IS_320(brd)) {
628 j = readw(baseAddr + Module_cnt);
631 brd->numPorts = j * 8;
632 writew(j, baseAddr + Module_no);
633 writew(0, baseAddr + Magic_no);
634 for (i = 0; i < 600; i++) {
635 if (readw(baseAddr + Magic_no) == Magic_code)
639 if (readw(baseAddr + Magic_no) != Magic_code)
642 brd->intNdx = baseAddr + IRQindex;
643 brd->intPend = baseAddr + IRQpending;
644 brd->intTable = baseAddr + IRQtable;
649 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
652 void __iomem *ofsAddr, *baseAddr = brd->basemem;
653 struct moxa_port *port;
657 printk(KERN_ERR "MOXA: bios length is not even\n");
661 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
665 switch (brd->boardType) {
666 case MOXA_BOARD_C218_ISA:
667 case MOXA_BOARD_C218_PCI:
668 case MOXA_BOARD_CP204J:
670 for (i = 0; i < brd->numPorts; i++, port++) {
673 port->tableAddr = baseAddr + Extern_table +
675 ofsAddr = port->tableAddr;
676 writew(C218rx_mask, ofsAddr + RX_mask);
677 writew(C218tx_mask, ofsAddr + TX_mask);
678 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
679 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
681 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
682 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
688 for (i = 0; i < brd->numPorts; i++, port++) {
691 port->tableAddr = baseAddr + Extern_table +
693 ofsAddr = port->tableAddr;
694 switch (brd->numPorts) {
696 writew(C320p8rx_mask, ofsAddr + RX_mask);
697 writew(C320p8tx_mask, ofsAddr + TX_mask);
698 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
699 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
700 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
701 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
705 writew(C320p16rx_mask, ofsAddr + RX_mask);
706 writew(C320p16tx_mask, ofsAddr + TX_mask);
707 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
708 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
709 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
710 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
714 writew(C320p24rx_mask, ofsAddr + RX_mask);
715 writew(C320p24tx_mask, ofsAddr + TX_mask);
716 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
717 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
718 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
719 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
722 writew(C320p32rx_mask, ofsAddr + RX_mask);
723 writew(C320p32tx_mask, ofsAddr + TX_mask);
724 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
725 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
726 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
727 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
728 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
737 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
739 const void *ptr = fw->data;
743 unsigned int a, lenp, lencnt;
746 __le32 magic; /* 0x34303430 */
748 u8 type; /* UNIX = 3 */
749 u8 model; /* C218T=1, C320T=2, CP204=3 */
754 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
756 if (fw->size < MOXA_FW_HDRLEN) {
757 strcpy(rsn, "too short (even header won't fit)");
760 if (hdr->magic != cpu_to_le32(0x30343034)) {
761 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
764 if (hdr->type != 3) {
765 sprintf(rsn, "not for linux, type is %u", hdr->type);
768 if (moxa_check_fw_model(brd, hdr->model)) {
769 sprintf(rsn, "not for this card, model is %u", hdr->model);
773 len = MOXA_FW_HDRLEN;
774 lencnt = hdr->model == 2 ? 5 : 3;
775 for (a = 0; a < ARRAY_SIZE(lens); a++) {
776 lens[a] = le16_to_cpu(hdr->len[a]);
777 if (lens[a] && len + lens[a] <= fw->size &&
778 moxa_check_fw(&fw->data[len]))
779 printk(KERN_WARNING "MOXA firmware: unexpected input "
780 "at offset %u, but going on\n", (u32)len);
781 if (!lens[a] && a < lencnt) {
782 sprintf(rsn, "too few entries in fw file");
788 if (len != fw->size) {
789 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
794 ptr += MOXA_FW_HDRLEN;
797 strcpy(rsn, "read above");
799 ret = moxa_load_bios(brd, ptr, lens[lenp]);
803 /* we skip the tty section (lens[1]), since we don't need it */
804 ptr += lens[lenp] + lens[lenp + 1];
805 lenp += 2; /* comm */
807 if (hdr->model == 2) {
808 ret = moxa_load_320b(brd, ptr, lens[lenp]);
811 /* skip another tty */
812 ptr += lens[lenp] + lens[lenp + 1];
816 ret = moxa_load_code(brd, ptr, lens[lenp]);
822 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
826 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
828 const struct firmware *fw;
831 unsigned int i, first_idx;
834 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
836 if (brd->ports == NULL) {
837 printk(KERN_ERR "cannot allocate memory for ports\n");
842 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
843 tty_port_init(&p->port);
844 p->port.ops = &moxa_port_ops;
845 p->type = PORT_16550A;
846 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
849 switch (brd->boardType) {
850 case MOXA_BOARD_C218_ISA:
851 case MOXA_BOARD_C218_PCI:
852 file = "/*(DEBLOBBED)*/";
854 case MOXA_BOARD_CP204J:
855 file = "/*(DEBLOBBED)*/";
858 file = "/*(DEBLOBBED)*/";
862 ret = reject_firmware(&fw, file, dev);
864 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
865 "you've placed '%s' file into your firmware "
866 "loader directory (e.g. /lib/firmware)\n",
871 ret = moxa_load_fw(brd, fw);
873 release_firmware(fw);
878 spin_lock_bh(&moxa_lock);
880 if (!timer_pending(&moxaTimer))
881 mod_timer(&moxaTimer, jiffies + HZ / 50);
882 spin_unlock_bh(&moxa_lock);
884 first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
885 for (i = 0; i < brd->numPorts; i++)
886 tty_port_register_device(&brd->ports[i].port, moxaDriver,
891 for (i = 0; i < MAX_PORTS_PER_BOARD; i++)
892 tty_port_destroy(&brd->ports[i].port);
898 static void moxa_board_deinit(struct moxa_board_conf *brd)
900 unsigned int a, opened, first_idx;
902 mutex_lock(&moxa_openlock);
903 spin_lock_bh(&moxa_lock);
905 spin_unlock_bh(&moxa_lock);
907 /* pci hot-un-plug support */
908 for (a = 0; a < brd->numPorts; a++)
909 if (tty_port_initialized(&brd->ports[a].port))
910 tty_port_tty_hangup(&brd->ports[a].port, false);
912 for (a = 0; a < MAX_PORTS_PER_BOARD; a++)
913 tty_port_destroy(&brd->ports[a].port);
917 for (a = 0; a < brd->numPorts; a++)
918 if (tty_port_initialized(&brd->ports[a].port))
920 mutex_unlock(&moxa_openlock);
924 mutex_lock(&moxa_openlock);
927 first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
928 for (a = 0; a < brd->numPorts; a++)
929 tty_unregister_device(moxaDriver, first_idx + a);
931 iounmap(brd->basemem);
937 static int moxa_pci_probe(struct pci_dev *pdev,
938 const struct pci_device_id *ent)
940 struct moxa_board_conf *board;
942 int board_type = ent->driver_data;
945 retval = pci_enable_device(pdev);
947 dev_err(&pdev->dev, "can't enable pci device\n");
951 for (i = 0; i < MAX_BOARDS; i++)
952 if (moxa_boards[i].basemem == NULL)
956 if (i >= MAX_BOARDS) {
957 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
958 "found. Board is ignored.\n", MAX_BOARDS);
962 board = &moxa_boards[i];
964 retval = pci_request_region(pdev, 2, "moxa-base");
966 dev_err(&pdev->dev, "can't request pci region 2\n");
970 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
971 if (board->basemem == NULL) {
972 dev_err(&pdev->dev, "can't remap io space 2\n");
977 board->boardType = board_type;
978 switch (board_type) {
979 case MOXA_BOARD_C218_ISA:
980 case MOXA_BOARD_C218_PCI:
984 case MOXA_BOARD_CP204J:
991 board->busType = MOXA_BUS_TYPE_PCI;
993 retval = moxa_init_board(board, &pdev->dev);
997 pci_set_drvdata(pdev, board);
999 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
1000 moxa_brdname[board_type - 1], board->numPorts);
1004 iounmap(board->basemem);
1005 board->basemem = NULL;
1007 pci_release_region(pdev, 2);
1012 static void moxa_pci_remove(struct pci_dev *pdev)
1014 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
1016 moxa_board_deinit(brd);
1018 pci_release_region(pdev, 2);
1021 static struct pci_driver moxa_pci_driver = {
1023 .id_table = moxa_pcibrds,
1024 .probe = moxa_pci_probe,
1025 .remove = moxa_pci_remove
1027 #endif /* CONFIG_PCI */
1029 static int __init moxa_init(void)
1031 unsigned int isabrds = 0;
1033 struct moxa_board_conf *brd = moxa_boards;
1036 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1039 tty_port_init(&moxa_service_port);
1041 moxaDriver = tty_alloc_driver(MAX_PORTS + 1,
1042 TTY_DRIVER_REAL_RAW |
1043 TTY_DRIVER_DYNAMIC_DEV);
1044 if (IS_ERR(moxaDriver))
1045 return PTR_ERR(moxaDriver);
1047 moxaDriver->name = "ttyMX";
1048 moxaDriver->major = ttymajor;
1049 moxaDriver->minor_start = 0;
1050 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1051 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1052 moxaDriver->init_termios = tty_std_termios;
1053 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1054 moxaDriver->init_termios.c_ispeed = 9600;
1055 moxaDriver->init_termios.c_ospeed = 9600;
1056 tty_set_operations(moxaDriver, &moxa_ops);
1057 /* Having one more port only for ioctls is ugly */
1058 tty_port_link_device(&moxa_service_port, moxaDriver, MAX_PORTS);
1060 if (tty_register_driver(moxaDriver)) {
1061 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1062 put_tty_driver(moxaDriver);
1066 /* Find the boards defined from module args. */
1068 for (i = 0; i < MAX_BOARDS; i++) {
1071 if (type[i] == MOXA_BOARD_C218_ISA ||
1072 type[i] == MOXA_BOARD_C320_ISA) {
1073 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1074 isabrds + 1, moxa_brdname[type[i] - 1],
1076 brd->boardType = type[i];
1077 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1079 brd->busType = MOXA_BUS_TYPE_ISA;
1080 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
1081 if (!brd->basemem) {
1082 printk(KERN_ERR "MOXA: can't remap %lx\n",
1086 if (moxa_init_board(brd, NULL)) {
1087 iounmap(brd->basemem);
1088 brd->basemem = NULL;
1092 printk(KERN_INFO "MOXA isa board found at 0x%.8lx and "
1093 "ready (%u ports, firmware loaded)\n",
1094 baseaddr[i], brd->numPorts);
1102 retval = pci_register_driver(&moxa_pci_driver);
1104 printk(KERN_ERR "Can't register MOXA pci driver!\n");
1113 static void __exit moxa_exit(void)
1118 pci_unregister_driver(&moxa_pci_driver);
1121 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1122 if (moxa_boards[i].ready)
1123 moxa_board_deinit(&moxa_boards[i]);
1125 del_timer_sync(&moxaTimer);
1127 if (tty_unregister_driver(moxaDriver))
1128 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1130 put_tty_driver(moxaDriver);
1133 module_init(moxa_init);
1134 module_exit(moxa_exit);
1136 static void moxa_shutdown(struct tty_port *port)
1138 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1139 MoxaPortDisable(ch);
1140 MoxaPortFlushData(ch, 2);
1143 static int moxa_carrier_raised(struct tty_port *port)
1145 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1148 spin_lock_irq(&port->lock);
1150 spin_unlock_irq(&port->lock);
1154 static void moxa_dtr_rts(struct tty_port *port, int onoff)
1156 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1157 MoxaPortLineCtrl(ch, onoff, onoff);
1161 static int moxa_open(struct tty_struct *tty, struct file *filp)
1163 struct moxa_board_conf *brd;
1164 struct moxa_port *ch;
1168 if (port == MAX_PORTS) {
1169 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1171 if (mutex_lock_interruptible(&moxa_openlock))
1172 return -ERESTARTSYS;
1173 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1175 mutex_unlock(&moxa_openlock);
1179 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1180 mutex_unlock(&moxa_openlock);
1184 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1186 tty->driver_data = ch;
1187 tty_port_tty_set(&ch->port, tty);
1188 mutex_lock(&ch->port.mutex);
1189 if (!tty_port_initialized(&ch->port)) {
1190 ch->statusflags = 0;
1191 moxa_set_tty_param(tty, &tty->termios);
1192 MoxaPortLineCtrl(ch, 1, 1);
1194 MoxaSetFifo(ch, ch->type == PORT_16550A);
1195 tty_port_set_initialized(&ch->port, 1);
1197 mutex_unlock(&ch->port.mutex);
1198 mutex_unlock(&moxa_openlock);
1200 return tty_port_block_til_ready(&ch->port, tty, filp);
1203 static void moxa_close(struct tty_struct *tty, struct file *filp)
1205 struct moxa_port *ch = tty->driver_data;
1206 ch->cflag = tty->termios.c_cflag;
1207 tty_port_close(&ch->port, tty, filp);
1210 static int moxa_write(struct tty_struct *tty,
1211 const unsigned char *buf, int count)
1213 struct moxa_port *ch = tty->driver_data;
1214 unsigned long flags;
1220 spin_lock_irqsave(&moxa_lock, flags);
1221 len = MoxaPortWriteData(tty, buf, count);
1222 spin_unlock_irqrestore(&moxa_lock, flags);
1224 set_bit(LOWWAIT, &ch->statusflags);
1228 static int moxa_write_room(struct tty_struct *tty)
1230 struct moxa_port *ch;
1234 ch = tty->driver_data;
1237 return MoxaPortTxFree(ch);
1240 static void moxa_flush_buffer(struct tty_struct *tty)
1242 struct moxa_port *ch = tty->driver_data;
1246 MoxaPortFlushData(ch, 1);
1250 static int moxa_chars_in_buffer(struct tty_struct *tty)
1252 struct moxa_port *ch = tty->driver_data;
1255 chars = MoxaPortTxQueue(ch);
1258 * Make it possible to wakeup anything waiting for output
1259 * in tty_ioctl.c, etc.
1261 set_bit(EMPTYWAIT, &ch->statusflags);
1265 static int moxa_tiocmget(struct tty_struct *tty)
1267 struct moxa_port *ch = tty->driver_data;
1268 int flag = 0, dtr, rts;
1270 MoxaPortGetLineOut(ch, &dtr, &rts);
1275 dtr = MoxaPortLineStatus(ch);
1285 static int moxa_tiocmset(struct tty_struct *tty,
1286 unsigned int set, unsigned int clear)
1288 struct moxa_port *ch;
1291 mutex_lock(&moxa_openlock);
1292 ch = tty->driver_data;
1294 mutex_unlock(&moxa_openlock);
1298 MoxaPortGetLineOut(ch, &dtr, &rts);
1299 if (set & TIOCM_RTS)
1301 if (set & TIOCM_DTR)
1303 if (clear & TIOCM_RTS)
1305 if (clear & TIOCM_DTR)
1307 MoxaPortLineCtrl(ch, dtr, rts);
1308 mutex_unlock(&moxa_openlock);
1312 static void moxa_set_termios(struct tty_struct *tty,
1313 struct ktermios *old_termios)
1315 struct moxa_port *ch = tty->driver_data;
1319 moxa_set_tty_param(tty, old_termios);
1320 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1321 wake_up_interruptible(&ch->port.open_wait);
1324 static void moxa_stop(struct tty_struct *tty)
1326 struct moxa_port *ch = tty->driver_data;
1330 MoxaPortTxDisable(ch);
1331 set_bit(TXSTOPPED, &ch->statusflags);
1335 static void moxa_start(struct tty_struct *tty)
1337 struct moxa_port *ch = tty->driver_data;
1342 if (!test_bit(TXSTOPPED, &ch->statusflags))
1345 MoxaPortTxEnable(ch);
1346 clear_bit(TXSTOPPED, &ch->statusflags);
1349 static void moxa_hangup(struct tty_struct *tty)
1351 struct moxa_port *ch = tty->driver_data;
1352 tty_port_hangup(&ch->port);
1355 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1357 unsigned long flags;
1360 spin_lock_irqsave(&p->port.lock, flags);
1361 if (dcd != p->DCDState) {
1363 spin_unlock_irqrestore(&p->port.lock, flags);
1365 tty_port_tty_hangup(&p->port, true);
1368 spin_unlock_irqrestore(&p->port.lock, flags);
1371 static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1374 struct tty_struct *tty = tty_port_tty_get(&p->port);
1375 void __iomem *ofsAddr;
1376 unsigned int inited = tty_port_initialized(&p->port);
1380 if (test_bit(EMPTYWAIT, &p->statusflags) &&
1381 MoxaPortTxQueue(p) == 0) {
1382 clear_bit(EMPTYWAIT, &p->statusflags);
1385 if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
1386 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1387 clear_bit(LOWWAIT, &p->statusflags);
1391 if (inited && !tty_throttled(tty) &&
1392 MoxaPortRxQueue(p) > 0) { /* RX */
1393 MoxaPortReadData(p);
1394 tty_schedule_flip(&p->port);
1397 clear_bit(EMPTYWAIT, &p->statusflags);
1398 MoxaPortFlushData(p, 0); /* flush RX */
1401 if (!handle) /* nothing else to do */
1404 intr = readw(ip); /* port irq status */
1408 writew(0, ip); /* ACK port */
1409 ofsAddr = p->tableAddr;
1410 if (intr & IntrTx) /* disable tx intr */
1411 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1412 ofsAddr + HostStat);
1417 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1418 tty_insert_flip_char(&p->port, 0, TTY_BREAK);
1419 tty_schedule_flip(&p->port);
1422 if (intr & IntrLine)
1423 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1430 static void moxa_poll(struct timer_list *unused)
1432 struct moxa_board_conf *brd;
1434 unsigned int card, port, served = 0;
1436 spin_lock(&moxa_lock);
1437 for (card = 0; card < MAX_BOARDS; card++) {
1438 brd = &moxa_boards[card];
1445 if (readb(brd->intPend) == 0xff)
1446 ip = brd->intTable + readb(brd->intNdx);
1448 for (port = 0; port < brd->numPorts; port++)
1449 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1452 writeb(0, brd->intPend); /* ACK */
1454 if (moxaLowWaterChk) {
1455 struct moxa_port *p = brd->ports;
1456 for (port = 0; port < brd->numPorts; port++, p++)
1457 if (p->lowChkFlag) {
1459 moxa_low_water_check(p->tableAddr);
1463 moxaLowWaterChk = 0;
1466 mod_timer(&moxaTimer, jiffies + HZ / 50);
1467 spin_unlock(&moxa_lock);
1470 /******************************************************************************/
1472 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1474 register struct ktermios *ts = &tty->termios;
1475 struct moxa_port *ch = tty->driver_data;
1476 int rts, cts, txflow, rxflow, xany, baud;
1478 rts = cts = txflow = rxflow = xany = 0;
1479 if (ts->c_cflag & CRTSCTS)
1481 if (ts->c_iflag & IXON)
1483 if (ts->c_iflag & IXOFF)
1485 if (ts->c_iflag & IXANY)
1488 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1489 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1491 baud = tty_termios_baud_rate(old_termios);
1492 /* Not put the baud rate into the termios data */
1493 tty_encode_baud_rate(tty, baud, baud);
1496 /*****************************************************************************
1497 * Driver level functions: *
1498 *****************************************************************************/
1500 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1502 void __iomem *ofsAddr;
1503 if (mode < 0 || mode > 2)
1505 ofsAddr = port->tableAddr;
1506 moxafunc(ofsAddr, FC_FlushQueue, mode);
1508 port->lowChkFlag = 0;
1509 moxa_low_water_check(ofsAddr);
1514 * Moxa Port Number Description:
1516 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1517 * the port number using in MOXA driver functions will be 0 to 31 for
1518 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1519 * to 127 for fourth. For example, if you setup three MOXA boards,
1520 * first board is C218, second board is C320-16 and third board is
1521 * C320-32. The port number of first board (C218 - 8 ports) is from
1522 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1523 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1524 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1525 * 127 will be invalid.
1528 * Moxa Functions Description:
1530 * Function 1: Driver initialization routine, this routine must be
1531 * called when initialized driver.
1533 * void MoxaDriverInit();
1536 * Function 2: Moxa driver private IOCTL command processing.
1538 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1540 * unsigned int cmd : IOCTL command
1541 * unsigned long arg : IOCTL argument
1542 * int port : port number (0 - 127)
1549 * Function 6: Enable this port to start Tx/Rx data.
1551 * void MoxaPortEnable(int port);
1552 * int port : port number (0 - 127)
1555 * Function 7: Disable this port
1557 * void MoxaPortDisable(int port);
1558 * int port : port number (0 - 127)
1561 * Function 10: Setting baud rate of this port.
1563 * speed_t MoxaPortSetBaud(int port, speed_t baud);
1564 * int port : port number (0 - 127)
1565 * long baud : baud rate (50 - 115200)
1567 * return: 0 : this port is invalid or baud < 50
1568 * 50 - 115200 : the real baud rate set to the port, if
1569 * the argument baud is large than maximun
1570 * available baud rate, the real setting
1571 * baud rate will be the maximun baud rate.
1574 * Function 12: Configure the port.
1576 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1577 * int port : port number (0 - 127)
1578 * struct ktermios * termio : termio structure pointer
1579 * speed_t baud : baud rate
1581 * return: -1 : this port is invalid or termio == NULL
1585 * Function 13: Get the DTR/RTS state of this port.
1587 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1588 * int port : port number (0 - 127)
1589 * int * dtrState : pointer to INT to receive the current DTR
1590 * state. (if NULL, this function will not
1591 * write to this address)
1592 * int * rtsState : pointer to INT to receive the current RTS
1593 * state. (if NULL, this function will not
1594 * write to this address)
1596 * return: -1 : this port is invalid
1600 * Function 14: Setting the DTR/RTS output state of this port.
1602 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1603 * int port : port number (0 - 127)
1604 * int dtrState : DTR output state (0: off, 1: on)
1605 * int rtsState : RTS output state (0: off, 1: on)
1608 * Function 15: Setting the flow control of this port.
1610 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1611 * int txFlow,int xany);
1612 * int port : port number (0 - 127)
1613 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1614 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1615 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1616 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1617 * int xany : S/W XANY flow control (0: no, 1: yes)
1620 * Function 16: Get ths line status of this port
1622 * int MoxaPortLineStatus(int port);
1623 * int port : port number (0 - 127)
1625 * return: Bit 0 - CTS state (0: off, 1: on)
1626 * Bit 1 - DSR state (0: off, 1: on)
1627 * Bit 2 - DCD state (0: off, 1: on)
1630 * Function 19: Flush the Rx/Tx buffer data of this port.
1632 * void MoxaPortFlushData(int port, int mode);
1633 * int port : port number (0 - 127)
1635 * 0 : flush the Rx buffer
1636 * 1 : flush the Tx buffer
1637 * 2 : flush the Rx and Tx buffer
1640 * Function 20: Write data.
1642 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1643 * int port : port number (0 - 127)
1644 * unsigned char * buffer : pointer to write data buffer.
1645 * int length : write data length
1647 * return: 0 - length : real write data length
1650 * Function 21: Read data.
1652 * int MoxaPortReadData(int port, struct tty_struct *tty);
1653 * int port : port number (0 - 127)
1654 * struct tty_struct *tty : tty for data
1656 * return: 0 - length : real read data length
1659 * Function 24: Get the Tx buffer current queued data bytes
1661 * int MoxaPortTxQueue(int port);
1662 * int port : port number (0 - 127)
1664 * return: .. : Tx buffer current queued data bytes
1667 * Function 25: Get the Tx buffer current free space
1669 * int MoxaPortTxFree(int port);
1670 * int port : port number (0 - 127)
1672 * return: .. : Tx buffer current free space
1675 * Function 26: Get the Rx buffer current queued data bytes
1677 * int MoxaPortRxQueue(int port);
1678 * int port : port number (0 - 127)
1680 * return: .. : Rx buffer current queued data bytes
1683 * Function 28: Disable port data transmission.
1685 * void MoxaPortTxDisable(int port);
1686 * int port : port number (0 - 127)
1689 * Function 29: Enable port data transmission.
1691 * void MoxaPortTxEnable(int port);
1692 * int port : port number (0 - 127)
1695 * Function 31: Get the received BREAK signal count and reset it.
1697 * int MoxaPortResetBrkCnt(int port);
1698 * int port : port number (0 - 127)
1700 * return: 0 - .. : BREAK signal count
1705 static void MoxaPortEnable(struct moxa_port *port)
1707 void __iomem *ofsAddr;
1710 ofsAddr = port->tableAddr;
1711 writew(lowwater, ofsAddr + Low_water);
1712 if (MOXA_IS_320(port->board))
1713 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1715 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1716 ofsAddr + HostStat);
1718 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1719 moxafunc(ofsAddr, FC_FlushQueue, 2);
1721 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1722 MoxaPortLineStatus(port);
1725 static void MoxaPortDisable(struct moxa_port *port)
1727 void __iomem *ofsAddr = port->tableAddr;
1729 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1730 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1731 writew(0, ofsAddr + HostStat);
1732 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1735 static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
1737 void __iomem *ofsAddr = port->tableAddr;
1738 unsigned int clock, val;
1741 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1748 moxafunc(ofsAddr, FC_SetBaud, val);
1753 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1756 void __iomem *ofsAddr;
1759 ofsAddr = port->tableAddr;
1761 mode = termio->c_cflag & CSIZE;
1764 else if (mode == CS6)
1766 else if (mode == CS7)
1768 else if (mode == CS8)
1771 if (termio->c_cflag & CSTOPB) {
1779 if (termio->c_cflag & PARENB) {
1780 if (termio->c_cflag & PARODD) {
1781 if (termio->c_cflag & CMSPAR)
1786 if (termio->c_cflag & CMSPAR)
1787 mode |= MX_PARSPACE;
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);