1 /*****************************************************************************/
3 * moxa.c -- MOXA Intellio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com).
6 * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
8 * This code is loosely based on the Linux serial driver, written by
9 * Linus Torvalds, Theodore T'so and others.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
18 * MOXA Intellio Series Driver
24 #include <linux/module.h>
25 #include <linux/types.h>
27 #include <linux/ioport.h>
28 #include <linux/errno.h>
29 #include <linux/firmware.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/timer.h>
33 #include <linux/interrupt.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/major.h>
37 #include <linux/string.h>
38 #include <linux/fcntl.h>
39 #include <linux/ptrace.h>
40 #include <linux/serial.h>
41 #include <linux/tty_driver.h>
42 #include <linux/delay.h>
43 #include <linux/pci.h>
44 #include <linux/init.h>
45 #include <linux/bitops.h>
47 #include <asm/system.h>
49 #include <asm/uaccess.h>
53 #define MOXA_VERSION "6.0k"
55 #define MOXA_FW_HDRLEN 32
59 #define MAX_BOARDS 4 /* Don't change this value */
60 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
61 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
63 #define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
64 (brd)->boardType == MOXA_BOARD_C320_PCI)
67 * Define the Moxa PCI vendor and device IDs.
69 #define MOXA_BUS_TYPE_ISA 0
70 #define MOXA_BUS_TYPE_PCI 1
73 MOXA_BOARD_C218_PCI = 1,
80 static char *moxa_brdname[] =
82 "C218 Turbo PCI series",
83 "C218 Turbo ISA series",
84 "C320 Turbo PCI series",
85 "C320 Turbo ISA series",
90 static struct pci_device_id moxa_pcibrds[] = {
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
92 .driver_data = MOXA_BOARD_C218_PCI },
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
94 .driver_data = MOXA_BOARD_C320_PCI },
95 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
96 .driver_data = MOXA_BOARD_CP204J },
99 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
100 #endif /* CONFIG_PCI */
104 static struct moxa_board_conf {
111 struct moxa_port *ports;
113 void __iomem *basemem;
114 void __iomem *intNdx;
115 void __iomem *intPend;
116 void __iomem *intTable;
117 } moxa_boards[MAX_BOARDS];
119 struct mxser_mstatus {
133 struct tty_port port;
134 struct moxa_board_conf *board;
135 void __iomem *tableAddr;
139 unsigned long statusflags;
148 int rxcnt[MAX_PORTS];
149 int txcnt[MAX_PORTS];
153 #define TXSTOPPED 0x1
155 #define EMPTYWAIT 0x4
158 #define SERIAL_DO_RESTART
160 #define WAKEUP_CHARS 256
162 static int ttymajor = MOXAMAJOR;
163 static struct mon_str moxaLog;
164 static unsigned int moxaFuncTout = HZ / 2;
165 static unsigned int moxaLowWaterChk;
166 static DEFINE_MUTEX(moxa_openlock);
167 /* Variables for insmod */
169 static unsigned long baseaddr[MAX_BOARDS];
170 static unsigned int type[MAX_BOARDS];
171 static unsigned int numports[MAX_BOARDS];
174 MODULE_AUTHOR("William Chen");
175 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
176 MODULE_LICENSE("GPL");
178 module_param_array(type, uint, NULL, 0);
179 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
180 module_param_array(baseaddr, ulong, NULL, 0);
181 MODULE_PARM_DESC(baseaddr, "base address");
182 module_param_array(numports, uint, NULL, 0);
183 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
185 module_param(ttymajor, int, 0);
190 static int moxa_open(struct tty_struct *, struct file *);
191 static void moxa_close(struct tty_struct *, struct file *);
192 static int moxa_write(struct tty_struct *, const unsigned char *, int);
193 static int moxa_write_room(struct tty_struct *);
194 static void moxa_flush_buffer(struct tty_struct *);
195 static int moxa_chars_in_buffer(struct tty_struct *);
196 static void moxa_throttle(struct tty_struct *);
197 static void moxa_unthrottle(struct tty_struct *);
198 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
199 static void moxa_stop(struct tty_struct *);
200 static void moxa_start(struct tty_struct *);
201 static void moxa_hangup(struct tty_struct *);
202 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
203 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
204 unsigned int set, unsigned int clear);
205 static void moxa_poll(unsigned long);
206 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
207 static void moxa_setup_empty_event(struct tty_struct *);
208 static void moxa_shut_down(struct tty_struct *);
209 static int moxa_carrier_raised(struct tty_port *);
211 * moxa board interface functions:
213 static void MoxaPortEnable(struct moxa_port *);
214 static void MoxaPortDisable(struct moxa_port *);
215 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
216 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
217 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
218 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
219 static int MoxaPortLineStatus(struct moxa_port *);
220 static void MoxaPortFlushData(struct moxa_port *, int);
221 static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
222 static int MoxaPortReadData(struct moxa_port *);
223 static int MoxaPortTxQueue(struct moxa_port *);
224 static int MoxaPortRxQueue(struct moxa_port *);
225 static int MoxaPortTxFree(struct moxa_port *);
226 static void MoxaPortTxDisable(struct moxa_port *);
227 static void MoxaPortTxEnable(struct moxa_port *);
228 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
229 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
230 static void MoxaSetFifo(struct moxa_port *port, int enable);
236 static void moxa_wait_finish(void __iomem *ofsAddr)
238 unsigned long end = jiffies + moxaFuncTout;
240 while (readw(ofsAddr + FuncCode) != 0)
241 if (time_after(jiffies, end))
243 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
244 printk(KERN_WARNING "moxa function expired\n");
247 static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
249 writew(arg, ofsAddr + FuncArg);
250 writew(cmd, ofsAddr + FuncCode);
251 moxa_wait_finish(ofsAddr);
254 static void moxa_low_water_check(void __iomem *ofsAddr)
256 u16 rptr, wptr, mask, len;
258 if (readb(ofsAddr + FlagStat) & Xoff_state) {
259 rptr = readw(ofsAddr + RXrptr);
260 wptr = readw(ofsAddr + RXwptr);
261 mask = readw(ofsAddr + RX_mask);
262 len = (wptr - rptr) & mask;
263 if (len <= Low_water)
264 moxafunc(ofsAddr, FC_SendXon, 0);
272 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
273 unsigned int cmd, unsigned long arg)
275 struct moxa_port *ch = tty->driver_data;
276 void __user *argp = (void __user *)arg;
279 if (tty->index == MAX_PORTS) {
280 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
281 cmd != MOXA_GETMSTATUS)
287 case MOXA_GETDATACOUNT:
288 moxaLog.tick = jiffies;
289 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
292 case MOXA_FLUSH_QUEUE:
293 MoxaPortFlushData(ch, arg);
295 case MOXA_GET_IOQUEUE: {
296 struct moxaq_str __user *argm = argp;
297 struct moxaq_str tmp;
301 mutex_lock(&moxa_openlock);
302 for (i = 0; i < MAX_BOARDS; i++) {
303 p = moxa_boards[i].ports;
304 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
305 memset(&tmp, 0, sizeof(tmp));
306 if (moxa_boards[i].ready) {
307 tmp.inq = MoxaPortRxQueue(p);
308 tmp.outq = MoxaPortTxQueue(p);
310 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
311 mutex_unlock(&moxa_openlock);
316 mutex_unlock(&moxa_openlock);
318 } case MOXA_GET_OQUEUE:
319 status = MoxaPortTxQueue(ch);
320 ret = put_user(status, (unsigned long __user *)argp);
322 case MOXA_GET_IQUEUE:
323 status = MoxaPortRxQueue(ch);
324 ret = put_user(status, (unsigned long __user *)argp);
326 case MOXA_GETMSTATUS: {
327 struct mxser_mstatus __user *argm = argp;
328 struct mxser_mstatus tmp;
332 mutex_lock(&moxa_openlock);
333 for (i = 0; i < MAX_BOARDS; i++) {
334 p = moxa_boards[i].ports;
335 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
336 struct tty_struct *ttyp;
337 memset(&tmp, 0, sizeof(tmp));
338 if (!moxa_boards[i].ready)
341 status = MoxaPortLineStatus(p);
349 ttyp = tty_port_tty_get(&p->port);
350 if (!ttyp || !ttyp->termios)
351 tmp.cflag = p->cflag;
353 tmp.cflag = ttyp->termios->c_cflag;
356 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
357 mutex_unlock(&moxa_openlock);
362 mutex_unlock(&moxa_openlock);
366 mutex_lock(&moxa_openlock);
367 ret = moxa_get_serial_info(ch, argp);
368 mutex_unlock(&moxa_openlock);
371 mutex_lock(&moxa_openlock);
372 ret = moxa_set_serial_info(ch, argp);
373 mutex_unlock(&moxa_openlock);
381 static int moxa_break_ctl(struct tty_struct *tty, int state)
383 struct moxa_port *port = tty->driver_data;
385 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
390 static const struct tty_operations moxa_ops = {
394 .write_room = moxa_write_room,
395 .flush_buffer = moxa_flush_buffer,
396 .chars_in_buffer = moxa_chars_in_buffer,
398 .throttle = moxa_throttle,
399 .unthrottle = moxa_unthrottle,
400 .set_termios = moxa_set_termios,
403 .hangup = moxa_hangup,
404 .break_ctl = moxa_break_ctl,
405 .tiocmget = moxa_tiocmget,
406 .tiocmset = moxa_tiocmset,
409 static const struct tty_port_operations moxa_port_ops = {
410 .carrier_raised = moxa_carrier_raised,
413 static struct tty_driver *moxaDriver;
414 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
415 static DEFINE_SPINLOCK(moxa_lock);
421 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
423 switch (brd->boardType) {
424 case MOXA_BOARD_C218_ISA:
425 case MOXA_BOARD_C218_PCI:
429 case MOXA_BOARD_CP204J:
443 static int moxa_check_fw(const void *ptr)
445 const __le16 *lptr = ptr;
447 if (*lptr != cpu_to_le16(0x7980))
453 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
456 void __iomem *baseAddr = brd->basemem;
459 writeb(HW_reset, baseAddr + Control_reg); /* reset */
461 memset_io(baseAddr, 0, 4096);
462 memcpy_toio(baseAddr, buf, len); /* download BIOS */
463 writeb(0, baseAddr + Control_reg); /* restart */
467 switch (brd->boardType) {
468 case MOXA_BOARD_C218_ISA:
469 case MOXA_BOARD_C218_PCI:
470 tmp = readw(baseAddr + C218_key);
471 if (tmp != C218_KeyCode)
474 case MOXA_BOARD_CP204J:
475 tmp = readw(baseAddr + C218_key);
476 if (tmp != CP204J_KeyCode)
480 tmp = readw(baseAddr + C320_key);
481 if (tmp != C320_KeyCode)
483 tmp = readw(baseAddr + C320_status);
484 if (tmp != STS_init) {
485 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
486 "module not found\n");
494 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
498 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
501 void __iomem *baseAddr = brd->basemem;
504 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
508 writew(len - 7168 - 2, baseAddr + C320bapi_len);
509 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
510 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
511 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
512 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
517 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
520 void __iomem *baseAddr = brd->basemem;
521 const __le16 *uptr = ptr;
522 size_t wlen, len2, j;
523 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
524 unsigned int i, retry;
527 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
530 switch (brd->boardType) {
531 case MOXA_BOARD_CP204J:
532 case MOXA_BOARD_C218_ISA:
533 case MOXA_BOARD_C218_PCI:
535 loadbuf = C218_LoadBuf;
536 loadlen = C218DLoad_len;
537 checksum = C218check_sum;
538 checksum_ok = C218chksum_ok;
542 keycode = C320_KeyCode;
543 loadbuf = C320_LoadBuf;
544 loadlen = C320DLoad_len;
545 checksum = C320check_sum;
546 checksum_ok = C320chksum_ok;
552 for (i = 0; i < wlen; i++)
553 usum += le16_to_cpu(uptr[i]);
559 len2 = (wlen > 2048) ? 2048 : wlen;
561 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
564 writew(len2, baseAddr + loadlen);
565 writew(0, baseAddr + key);
566 for (i = 0; i < 100; i++) {
567 if (readw(baseAddr + key) == keycode)
571 if (readw(baseAddr + key) != keycode)
574 writew(0, baseAddr + loadlen);
575 writew(usum, baseAddr + checksum);
576 writew(0, baseAddr + key);
577 for (i = 0; i < 100; i++) {
578 if (readw(baseAddr + key) == keycode)
583 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
584 if (readb(baseAddr + checksum_ok) != 1)
587 writew(0, baseAddr + key);
588 for (i = 0; i < 600; i++) {
589 if (readw(baseAddr + Magic_no) == Magic_code)
593 if (readw(baseAddr + Magic_no) != Magic_code)
596 if (MOXA_IS_320(brd)) {
597 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
598 writew(0x3800, baseAddr + TMS320_PORT1);
599 writew(0x3900, baseAddr + TMS320_PORT2);
600 writew(28499, baseAddr + TMS320_CLOCK);
602 writew(0x3200, baseAddr + TMS320_PORT1);
603 writew(0x3400, baseAddr + TMS320_PORT2);
604 writew(19999, baseAddr + TMS320_CLOCK);
607 writew(1, baseAddr + Disable_IRQ);
608 writew(0, baseAddr + Magic_no);
609 for (i = 0; i < 500; i++) {
610 if (readw(baseAddr + Magic_no) == Magic_code)
614 if (readw(baseAddr + Magic_no) != Magic_code)
617 if (MOXA_IS_320(brd)) {
618 j = readw(baseAddr + Module_cnt);
621 brd->numPorts = j * 8;
622 writew(j, baseAddr + Module_no);
623 writew(0, baseAddr + Magic_no);
624 for (i = 0; i < 600; i++) {
625 if (readw(baseAddr + Magic_no) == Magic_code)
629 if (readw(baseAddr + Magic_no) != Magic_code)
632 brd->intNdx = baseAddr + IRQindex;
633 brd->intPend = baseAddr + IRQpending;
634 brd->intTable = baseAddr + IRQtable;
639 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
642 void __iomem *ofsAddr, *baseAddr = brd->basemem;
643 struct moxa_port *port;
647 printk(KERN_ERR "MOXA: bios length is not even\n");
651 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
655 switch (brd->boardType) {
656 case MOXA_BOARD_C218_ISA:
657 case MOXA_BOARD_C218_PCI:
658 case MOXA_BOARD_CP204J:
660 for (i = 0; i < brd->numPorts; i++, port++) {
663 port->tableAddr = baseAddr + Extern_table +
665 ofsAddr = port->tableAddr;
666 writew(C218rx_mask, ofsAddr + RX_mask);
667 writew(C218tx_mask, ofsAddr + TX_mask);
668 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
669 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
671 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
672 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
678 for (i = 0; i < brd->numPorts; i++, port++) {
681 port->tableAddr = baseAddr + Extern_table +
683 ofsAddr = port->tableAddr;
684 switch (brd->numPorts) {
686 writew(C320p8rx_mask, ofsAddr + RX_mask);
687 writew(C320p8tx_mask, ofsAddr + TX_mask);
688 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
689 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
690 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
691 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
695 writew(C320p16rx_mask, ofsAddr + RX_mask);
696 writew(C320p16tx_mask, ofsAddr + TX_mask);
697 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
698 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
699 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
700 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
704 writew(C320p24rx_mask, ofsAddr + RX_mask);
705 writew(C320p24tx_mask, ofsAddr + TX_mask);
706 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
707 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
708 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
709 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
712 writew(C320p32rx_mask, ofsAddr + RX_mask);
713 writew(C320p32tx_mask, ofsAddr + TX_mask);
714 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
715 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
716 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
717 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
718 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
727 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
729 const void *ptr = fw->data;
733 unsigned int a, lenp, lencnt;
736 __le32 magic; /* 0x34303430 */
738 u8 type; /* UNIX = 3 */
739 u8 model; /* C218T=1, C320T=2, CP204=3 */
744 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
746 if (fw->size < MOXA_FW_HDRLEN) {
747 strcpy(rsn, "too short (even header won't fit)");
750 if (hdr->magic != cpu_to_le32(0x30343034)) {
751 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
754 if (hdr->type != 3) {
755 sprintf(rsn, "not for linux, type is %u", hdr->type);
758 if (moxa_check_fw_model(brd, hdr->model)) {
759 sprintf(rsn, "not for this card, model is %u", hdr->model);
763 len = MOXA_FW_HDRLEN;
764 lencnt = hdr->model == 2 ? 5 : 3;
765 for (a = 0; a < ARRAY_SIZE(lens); a++) {
766 lens[a] = le16_to_cpu(hdr->len[a]);
767 if (lens[a] && len + lens[a] <= fw->size &&
768 moxa_check_fw(&fw->data[len]))
769 printk(KERN_WARNING "MOXA firmware: unexpected input "
770 "at offset %u, but going on\n", (u32)len);
771 if (!lens[a] && a < lencnt) {
772 sprintf(rsn, "too few entries in fw file");
778 if (len != fw->size) {
779 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
784 ptr += MOXA_FW_HDRLEN;
787 strcpy(rsn, "read above");
789 ret = moxa_load_bios(brd, ptr, lens[lenp]);
793 /* we skip the tty section (lens[1]), since we don't need it */
794 ptr += lens[lenp] + lens[lenp + 1];
795 lenp += 2; /* comm */
797 if (hdr->model == 2) {
798 ret = moxa_load_320b(brd, ptr, lens[lenp]);
801 /* skip another tty */
802 ptr += lens[lenp] + lens[lenp + 1];
806 ret = moxa_load_code(brd, ptr, lens[lenp]);
812 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
816 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
818 const struct firmware *fw;
824 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
826 if (brd->ports == NULL) {
827 printk(KERN_ERR "cannot allocate memory for ports\n");
832 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
833 tty_port_init(&p->port);
834 p->port.ops = &moxa_port_ops;
835 p->type = PORT_16550A;
836 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
839 switch (brd->boardType) {
840 case MOXA_BOARD_C218_ISA:
841 case MOXA_BOARD_C218_PCI:
842 file = "c218tunx.cod";
844 case MOXA_BOARD_CP204J:
845 file = "cp204unx.cod";
848 file = "c320tunx.cod";
852 ret = request_firmware(&fw, file, dev);
854 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
855 "you've placed '%s' file into your firmware "
856 "loader directory (e.g. /lib/firmware)\n",
861 ret = moxa_load_fw(brd, fw);
863 release_firmware(fw);
868 spin_lock_bh(&moxa_lock);
870 if (!timer_pending(&moxaTimer))
871 mod_timer(&moxaTimer, jiffies + HZ / 50);
872 spin_unlock_bh(&moxa_lock);
881 static void moxa_board_deinit(struct moxa_board_conf *brd)
883 unsigned int a, opened;
885 mutex_lock(&moxa_openlock);
886 spin_lock_bh(&moxa_lock);
888 spin_unlock_bh(&moxa_lock);
890 /* pci hot-un-plug support */
891 for (a = 0; a < brd->numPorts; a++)
892 if (brd->ports[a].port.flags & ASYNC_INITIALIZED) {
893 struct tty_struct *tty = tty_port_tty_get(
894 &brd->ports[a].port);
902 for (a = 0; a < brd->numPorts; a++)
903 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
905 mutex_unlock(&moxa_openlock);
909 mutex_lock(&moxa_openlock);
912 iounmap(brd->basemem);
918 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
919 const struct pci_device_id *ent)
921 struct moxa_board_conf *board;
923 int board_type = ent->driver_data;
926 retval = pci_enable_device(pdev);
928 dev_err(&pdev->dev, "can't enable pci device\n");
932 for (i = 0; i < MAX_BOARDS; i++)
933 if (moxa_boards[i].basemem == NULL)
937 if (i >= MAX_BOARDS) {
938 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
939 "found. Board is ignored.\n", MAX_BOARDS);
943 board = &moxa_boards[i];
945 retval = pci_request_region(pdev, 2, "moxa-base");
947 dev_err(&pdev->dev, "can't request pci region 2\n");
951 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
952 if (board->basemem == NULL) {
953 dev_err(&pdev->dev, "can't remap io space 2\n");
957 board->boardType = board_type;
958 switch (board_type) {
959 case MOXA_BOARD_C218_ISA:
960 case MOXA_BOARD_C218_PCI:
964 case MOXA_BOARD_CP204J:
971 board->busType = MOXA_BUS_TYPE_PCI;
973 retval = moxa_init_board(board, &pdev->dev);
977 pci_set_drvdata(pdev, board);
979 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
980 moxa_brdname[board_type - 1], board->numPorts);
984 iounmap(board->basemem);
985 board->basemem = NULL;
987 pci_release_region(pdev, 2);
992 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
994 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
996 moxa_board_deinit(brd);
998 pci_release_region(pdev, 2);
1001 static struct pci_driver moxa_pci_driver = {
1003 .id_table = moxa_pcibrds,
1004 .probe = moxa_pci_probe,
1005 .remove = __devexit_p(moxa_pci_remove)
1007 #endif /* CONFIG_PCI */
1009 static int __init moxa_init(void)
1011 unsigned int isabrds = 0;
1014 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1016 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1020 moxaDriver->owner = THIS_MODULE;
1021 moxaDriver->name = "ttyMX";
1022 moxaDriver->major = ttymajor;
1023 moxaDriver->minor_start = 0;
1024 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1025 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1026 moxaDriver->init_termios = tty_std_termios;
1027 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1028 moxaDriver->init_termios.c_ispeed = 9600;
1029 moxaDriver->init_termios.c_ospeed = 9600;
1030 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1031 tty_set_operations(moxaDriver, &moxa_ops);
1033 if (tty_register_driver(moxaDriver)) {
1034 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1035 put_tty_driver(moxaDriver);
1039 /* Find the boards defined from module args. */
1042 struct moxa_board_conf *brd = moxa_boards;
1044 for (i = 0; i < MAX_BOARDS; i++) {
1047 if (type[i] == MOXA_BOARD_C218_ISA ||
1048 type[i] == MOXA_BOARD_C320_ISA) {
1049 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1050 isabrds + 1, moxa_brdname[type[i] - 1],
1052 brd->boardType = type[i];
1053 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1055 brd->busType = MOXA_BUS_TYPE_ISA;
1056 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
1057 if (!brd->basemem) {
1058 printk(KERN_ERR "MOXA: can't remap %lx\n",
1062 if (moxa_init_board(brd, NULL)) {
1063 iounmap(brd->basemem);
1064 brd->basemem = NULL;
1068 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1069 "ready (%u ports, firmware loaded)\n",
1070 baseaddr[i], brd->numPorts);
1080 retval = pci_register_driver(&moxa_pci_driver);
1082 printk(KERN_ERR "Can't register MOXA pci driver!\n");
1091 static void __exit moxa_exit(void)
1096 pci_unregister_driver(&moxa_pci_driver);
1099 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1100 if (moxa_boards[i].ready)
1101 moxa_board_deinit(&moxa_boards[i]);
1103 del_timer_sync(&moxaTimer);
1105 if (tty_unregister_driver(moxaDriver))
1106 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1108 put_tty_driver(moxaDriver);
1111 module_init(moxa_init);
1112 module_exit(moxa_exit);
1114 static void moxa_close_port(struct tty_struct *tty)
1116 struct moxa_port *ch = tty->driver_data;
1117 moxa_shut_down(tty);
1118 MoxaPortFlushData(ch, 2);
1119 ch->port.flags &= ~ASYNC_NORMAL_ACTIVE;
1120 tty->driver_data = NULL;
1121 tty_port_tty_set(&ch->port, NULL);
1124 static int moxa_carrier_raised(struct tty_port *port)
1126 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1129 spin_lock_bh(&moxa_lock);
1131 spin_unlock_bh(&moxa_lock);
1135 static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1136 struct moxa_port *ch)
1138 struct tty_port *port = &ch->port;
1144 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
1145 if (tty_hung_up_p(filp)) {
1146 #ifdef SERIAL_DO_RESTART
1147 retval = -ERESTARTSYS;
1153 dcd = tty_port_carrier_raised(port);
1157 if (signal_pending(current)) {
1158 retval = -ERESTARTSYS;
1163 finish_wait(&port->open_wait, &wait);
1168 static int moxa_open(struct tty_struct *tty, struct file *filp)
1170 struct moxa_board_conf *brd;
1171 struct moxa_port *ch;
1176 if (port == MAX_PORTS) {
1177 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1179 if (mutex_lock_interruptible(&moxa_openlock))
1180 return -ERESTARTSYS;
1181 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1183 mutex_unlock(&moxa_openlock);
1187 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1188 mutex_unlock(&moxa_openlock);
1192 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1194 tty->driver_data = ch;
1195 tty_port_tty_set(&ch->port, tty);
1196 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
1197 ch->statusflags = 0;
1198 moxa_set_tty_param(tty, tty->termios);
1199 MoxaPortLineCtrl(ch, 1, 1);
1201 MoxaSetFifo(ch, ch->type == PORT_16550A);
1202 ch->port.flags |= ASYNC_INITIALIZED;
1204 mutex_unlock(&moxa_openlock);
1207 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1208 retval = moxa_block_till_ready(tty, filp, ch);
1209 mutex_lock(&moxa_openlock);
1211 if (ch->port.count) /* 0 means already hung up... */
1212 if (--ch->port.count == 0)
1213 moxa_close_port(tty);
1215 ch->port.flags |= ASYNC_NORMAL_ACTIVE;
1216 mutex_unlock(&moxa_openlock);
1221 static void moxa_close(struct tty_struct *tty, struct file *filp)
1223 struct moxa_port *ch;
1227 if (port == MAX_PORTS || tty_hung_up_p(filp))
1230 mutex_lock(&moxa_openlock);
1231 ch = tty->driver_data;
1234 if (tty->count == 1 && ch->port.count != 1) {
1235 printk(KERN_WARNING "moxa_close: bad serial port count; "
1236 "tty->count is 1, ch->port.count is %d\n", ch->port.count);
1239 if (--ch->port.count < 0) {
1240 printk(KERN_WARNING "moxa_close: bad serial port count, "
1241 "device=%s\n", tty->name);
1247 ch->cflag = tty->termios->c_cflag;
1248 if (ch->port.flags & ASYNC_INITIALIZED) {
1249 moxa_setup_empty_event(tty);
1250 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
1253 moxa_close_port(tty);
1255 mutex_unlock(&moxa_openlock);
1258 static int moxa_write(struct tty_struct *tty,
1259 const unsigned char *buf, int count)
1261 struct moxa_port *ch = tty->driver_data;
1267 spin_lock_bh(&moxa_lock);
1268 len = MoxaPortWriteData(tty, buf, count);
1269 spin_unlock_bh(&moxa_lock);
1271 ch->statusflags |= LOWWAIT;
1275 static int moxa_write_room(struct tty_struct *tty)
1277 struct moxa_port *ch;
1281 ch = tty->driver_data;
1284 return MoxaPortTxFree(ch);
1287 static void moxa_flush_buffer(struct tty_struct *tty)
1289 struct moxa_port *ch = tty->driver_data;
1293 MoxaPortFlushData(ch, 1);
1297 static int moxa_chars_in_buffer(struct tty_struct *tty)
1299 struct moxa_port *ch = tty->driver_data;
1303 * Sigh...I have to check if driver_data is NULL here, because
1304 * if an open() fails, the TTY subsystem eventually calls
1305 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1306 * routine. And since the open() failed, we return 0 here. TDJ
1311 chars = MoxaPortTxQueue(ch);
1314 * Make it possible to wakeup anything waiting for output
1315 * in tty_ioctl.c, etc.
1317 if (!(ch->statusflags & EMPTYWAIT))
1318 moxa_setup_empty_event(tty);
1324 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1326 struct moxa_port *ch;
1327 int flag = 0, dtr, rts;
1329 mutex_lock(&moxa_openlock);
1330 ch = tty->driver_data;
1332 mutex_unlock(&moxa_openlock);
1336 MoxaPortGetLineOut(ch, &dtr, &rts);
1341 dtr = MoxaPortLineStatus(ch);
1348 mutex_unlock(&moxa_openlock);
1352 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1353 unsigned int set, unsigned int clear)
1355 struct moxa_port *ch;
1360 mutex_lock(&moxa_openlock);
1361 ch = tty->driver_data;
1363 mutex_unlock(&moxa_openlock);
1367 MoxaPortGetLineOut(ch, &dtr, &rts);
1368 if (set & TIOCM_RTS)
1370 if (set & TIOCM_DTR)
1372 if (clear & TIOCM_RTS)
1374 if (clear & TIOCM_DTR)
1376 MoxaPortLineCtrl(ch, dtr, rts);
1377 mutex_unlock(&moxa_openlock);
1381 static void moxa_throttle(struct tty_struct *tty)
1383 struct moxa_port *ch = tty->driver_data;
1385 ch->statusflags |= THROTTLE;
1388 static void moxa_unthrottle(struct tty_struct *tty)
1390 struct moxa_port *ch = tty->driver_data;
1392 ch->statusflags &= ~THROTTLE;
1395 static void moxa_set_termios(struct tty_struct *tty,
1396 struct ktermios *old_termios)
1398 struct moxa_port *ch = tty->driver_data;
1402 moxa_set_tty_param(tty, old_termios);
1403 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1404 wake_up_interruptible(&ch->port.open_wait);
1407 static void moxa_stop(struct tty_struct *tty)
1409 struct moxa_port *ch = tty->driver_data;
1413 MoxaPortTxDisable(ch);
1414 ch->statusflags |= TXSTOPPED;
1418 static void moxa_start(struct tty_struct *tty)
1420 struct moxa_port *ch = tty->driver_data;
1425 if (!(ch->statusflags & TXSTOPPED))
1428 MoxaPortTxEnable(ch);
1429 ch->statusflags &= ~TXSTOPPED;
1432 static void moxa_hangup(struct tty_struct *tty)
1434 struct moxa_port *ch;
1436 mutex_lock(&moxa_openlock);
1437 ch = tty->driver_data;
1439 mutex_unlock(&moxa_openlock);
1443 moxa_close_port(tty);
1444 mutex_unlock(&moxa_openlock);
1446 wake_up_interruptible(&ch->port.open_wait);
1449 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1451 struct tty_struct *tty;
1454 if (dcd != p->DCDState) {
1455 tty = tty_port_tty_get(&p->port);
1456 if (tty && C_CLOCAL(tty) && !dcd)
1463 static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1466 struct tty_struct *tty = tty_port_tty_get(&p->port);
1467 void __iomem *ofsAddr;
1468 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
1472 if ((p->statusflags & EMPTYWAIT) &&
1473 MoxaPortTxQueue(p) == 0) {
1474 p->statusflags &= ~EMPTYWAIT;
1477 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1478 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1479 p->statusflags &= ~LOWWAIT;
1483 if (inited && !(p->statusflags & THROTTLE) &&
1484 MoxaPortRxQueue(p) > 0) { /* RX */
1485 MoxaPortReadData(p);
1486 tty_schedule_flip(tty);
1489 p->statusflags &= ~EMPTYWAIT;
1490 MoxaPortFlushData(p, 0); /* flush RX */
1493 if (!handle) /* nothing else to do */
1496 intr = readw(ip); /* port irq status */
1500 writew(0, ip); /* ACK port */
1501 ofsAddr = p->tableAddr;
1502 if (intr & IntrTx) /* disable tx intr */
1503 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1504 ofsAddr + HostStat);
1509 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1510 tty_insert_flip_char(tty, 0, TTY_BREAK);
1511 tty_schedule_flip(tty);
1514 if (intr & IntrLine)
1515 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1522 static void moxa_poll(unsigned long ignored)
1524 struct moxa_board_conf *brd;
1526 unsigned int card, port, served = 0;
1528 spin_lock(&moxa_lock);
1529 for (card = 0; card < MAX_BOARDS; card++) {
1530 brd = &moxa_boards[card];
1537 if (readb(brd->intPend) == 0xff)
1538 ip = brd->intTable + readb(brd->intNdx);
1540 for (port = 0; port < brd->numPorts; port++)
1541 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1544 writeb(0, brd->intPend); /* ACK */
1546 if (moxaLowWaterChk) {
1547 struct moxa_port *p = brd->ports;
1548 for (port = 0; port < brd->numPorts; port++, p++)
1549 if (p->lowChkFlag) {
1551 moxa_low_water_check(p->tableAddr);
1555 moxaLowWaterChk = 0;
1558 mod_timer(&moxaTimer, jiffies + HZ / 50);
1559 spin_unlock(&moxa_lock);
1562 /******************************************************************************/
1564 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1566 register struct ktermios *ts = tty->termios;
1567 struct moxa_port *ch = tty->driver_data;
1568 int rts, cts, txflow, rxflow, xany, baud;
1570 rts = cts = txflow = rxflow = xany = 0;
1571 if (ts->c_cflag & CRTSCTS)
1573 if (ts->c_iflag & IXON)
1575 if (ts->c_iflag & IXOFF)
1577 if (ts->c_iflag & IXANY)
1580 /* Clear the features we don't support */
1581 ts->c_cflag &= ~CMSPAR;
1582 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1583 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1585 baud = tty_termios_baud_rate(old_termios);
1586 /* Not put the baud rate into the termios data */
1587 tty_encode_baud_rate(tty, baud, baud);
1590 static void moxa_setup_empty_event(struct tty_struct *tty)
1592 struct moxa_port *ch = tty->driver_data;
1594 spin_lock_bh(&moxa_lock);
1595 ch->statusflags |= EMPTYWAIT;
1596 spin_unlock_bh(&moxa_lock);
1599 static void moxa_shut_down(struct tty_struct *tty)
1601 struct moxa_port *ch = tty->driver_data;
1603 if (!(ch->port.flags & ASYNC_INITIALIZED))
1606 MoxaPortDisable(ch);
1609 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1612 MoxaPortLineCtrl(ch, 0, 0);
1614 spin_lock_bh(&moxa_lock);
1615 ch->port.flags &= ~ASYNC_INITIALIZED;
1616 spin_unlock_bh(&moxa_lock);
1619 /*****************************************************************************
1620 * Driver level functions: *
1621 *****************************************************************************/
1623 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1625 void __iomem *ofsAddr;
1626 if (mode < 0 || mode > 2)
1628 ofsAddr = port->tableAddr;
1629 moxafunc(ofsAddr, FC_FlushQueue, mode);
1631 port->lowChkFlag = 0;
1632 moxa_low_water_check(ofsAddr);
1637 * Moxa Port Number Description:
1639 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1640 * the port number using in MOXA driver functions will be 0 to 31 for
1641 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1642 * to 127 for fourth. For example, if you setup three MOXA boards,
1643 * first board is C218, second board is C320-16 and third board is
1644 * C320-32. The port number of first board (C218 - 8 ports) is from
1645 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1646 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1647 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1648 * 127 will be invalid.
1651 * Moxa Functions Description:
1653 * Function 1: Driver initialization routine, this routine must be
1654 * called when initialized driver.
1656 * void MoxaDriverInit();
1659 * Function 2: Moxa driver private IOCTL command processing.
1661 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1663 * unsigned int cmd : IOCTL command
1664 * unsigned long arg : IOCTL argument
1665 * int port : port number (0 - 127)
1672 * Function 6: Enable this port to start Tx/Rx data.
1674 * void MoxaPortEnable(int port);
1675 * int port : port number (0 - 127)
1678 * Function 7: Disable this port
1680 * void MoxaPortDisable(int port);
1681 * int port : port number (0 - 127)
1684 * Function 10: Setting baud rate of this port.
1686 * speed_t MoxaPortSetBaud(int port, speed_t baud);
1687 * int port : port number (0 - 127)
1688 * long baud : baud rate (50 - 115200)
1690 * return: 0 : this port is invalid or baud < 50
1691 * 50 - 115200 : the real baud rate set to the port, if
1692 * the argument baud is large than maximun
1693 * available baud rate, the real setting
1694 * baud rate will be the maximun baud rate.
1697 * Function 12: Configure the port.
1699 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1700 * int port : port number (0 - 127)
1701 * struct ktermios * termio : termio structure pointer
1702 * speed_t baud : baud rate
1704 * return: -1 : this port is invalid or termio == NULL
1708 * Function 13: Get the DTR/RTS state of this port.
1710 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1711 * int port : port number (0 - 127)
1712 * int * dtrState : pointer to INT to receive the current DTR
1713 * state. (if NULL, this function will not
1714 * write to this address)
1715 * int * rtsState : pointer to INT to receive the current RTS
1716 * state. (if NULL, this function will not
1717 * write to this address)
1719 * return: -1 : this port is invalid
1723 * Function 14: Setting the DTR/RTS output state of this port.
1725 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1726 * int port : port number (0 - 127)
1727 * int dtrState : DTR output state (0: off, 1: on)
1728 * int rtsState : RTS output state (0: off, 1: on)
1731 * Function 15: Setting the flow control of this port.
1733 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1734 * int txFlow,int xany);
1735 * int port : port number (0 - 127)
1736 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1737 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1738 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1739 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1740 * int xany : S/W XANY flow control (0: no, 1: yes)
1743 * Function 16: Get ths line status of this port
1745 * int MoxaPortLineStatus(int port);
1746 * int port : port number (0 - 127)
1748 * return: Bit 0 - CTS state (0: off, 1: on)
1749 * Bit 1 - DSR state (0: off, 1: on)
1750 * Bit 2 - DCD state (0: off, 1: on)
1753 * Function 19: Flush the Rx/Tx buffer data of this port.
1755 * void MoxaPortFlushData(int port, int mode);
1756 * int port : port number (0 - 127)
1758 * 0 : flush the Rx buffer
1759 * 1 : flush the Tx buffer
1760 * 2 : flush the Rx and Tx buffer
1763 * Function 20: Write data.
1765 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1766 * int port : port number (0 - 127)
1767 * unsigned char * buffer : pointer to write data buffer.
1768 * int length : write data length
1770 * return: 0 - length : real write data length
1773 * Function 21: Read data.
1775 * int MoxaPortReadData(int port, struct tty_struct *tty);
1776 * int port : port number (0 - 127)
1777 * struct tty_struct *tty : tty for data
1779 * return: 0 - length : real read data length
1782 * Function 24: Get the Tx buffer current queued data bytes
1784 * int MoxaPortTxQueue(int port);
1785 * int port : port number (0 - 127)
1787 * return: .. : Tx buffer current queued data bytes
1790 * Function 25: Get the Tx buffer current free space
1792 * int MoxaPortTxFree(int port);
1793 * int port : port number (0 - 127)
1795 * return: .. : Tx buffer current free space
1798 * Function 26: Get the Rx buffer current queued data bytes
1800 * int MoxaPortRxQueue(int port);
1801 * int port : port number (0 - 127)
1803 * return: .. : Rx buffer current queued data bytes
1806 * Function 28: Disable port data transmission.
1808 * void MoxaPortTxDisable(int port);
1809 * int port : port number (0 - 127)
1812 * Function 29: Enable port data transmission.
1814 * void MoxaPortTxEnable(int port);
1815 * int port : port number (0 - 127)
1818 * Function 31: Get the received BREAK signal count and reset it.
1820 * int MoxaPortResetBrkCnt(int port);
1821 * int port : port number (0 - 127)
1823 * return: 0 - .. : BREAK signal count
1828 static void MoxaPortEnable(struct moxa_port *port)
1830 void __iomem *ofsAddr;
1833 ofsAddr = port->tableAddr;
1834 writew(lowwater, ofsAddr + Low_water);
1835 if (MOXA_IS_320(port->board))
1836 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1838 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1839 ofsAddr + HostStat);
1841 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1842 moxafunc(ofsAddr, FC_FlushQueue, 2);
1844 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1845 MoxaPortLineStatus(port);
1848 static void MoxaPortDisable(struct moxa_port *port)
1850 void __iomem *ofsAddr = port->tableAddr;
1852 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1853 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1854 writew(0, ofsAddr + HostStat);
1855 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1858 static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
1860 void __iomem *ofsAddr = port->tableAddr;
1861 unsigned int clock, val;
1864 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1871 moxafunc(ofsAddr, FC_SetBaud, val);
1876 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1879 void __iomem *ofsAddr;
1883 ofsAddr = port->tableAddr;
1884 cflag = termio->c_cflag; /* termio->c_cflag */
1886 mode = termio->c_cflag & CSIZE;
1889 else if (mode == CS6)
1891 else if (mode == CS7)
1893 else if (mode == CS8)
1896 if (termio->c_cflag & CSTOPB) {
1904 if (termio->c_cflag & PARENB) {
1905 if (termio->c_cflag & PARODD)
1912 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
1914 if (MOXA_IS_320(port->board) && baud >= 921600)
1917 baud = MoxaPortSetBaud(port, baud);
1919 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1920 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1921 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1922 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1923 moxa_wait_finish(ofsAddr);
1929 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1933 *dtrState = !!(port->lineCtrl & DTR_ON);
1935 *rtsState = !!(port->lineCtrl & RTS_ON);
1940 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1948 port->lineCtrl = mode;
1949 moxafunc(port->tableAddr, FC_LineControl, mode);
1952 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1953 int txflow, int rxflow, int txany)
1958 mode |= RTS_FlowCtl;
1960 mode |= CTS_FlowCtl;
1967 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
1970 static int MoxaPortLineStatus(struct moxa_port *port)
1972 void __iomem *ofsAddr;
1975 ofsAddr = port->tableAddr;
1976 if (MOXA_IS_320(port->board)) {
1977 moxafunc(ofsAddr, FC_LineStatus, 0);
1978 val = readw(ofsAddr + FuncArg);
1980 val = readw(ofsAddr + FlagStat) >> 4;
1985 spin_lock_bh(&moxa_lock);
1986 moxa_new_dcdstate(port, val & 8);
1987 spin_unlock_bh(&moxa_lock);
1992 static int MoxaPortWriteData(struct tty_struct *tty,
1993 const unsigned char *buffer, int len)
1995 struct moxa_port *port = tty->driver_data;
1996 void __iomem *baseAddr, *ofsAddr, *ofs;
1997 unsigned int c, total;
1998 u16 head, tail, tx_mask, spage, epage;
1999 u16 pageno, pageofs, bufhead;
2001 ofsAddr = port->tableAddr;
2002 baseAddr = port->board->basemem;
2003 tx_mask = readw(ofsAddr + TX_mask);
2004 spage = readw(ofsAddr + Page_txb);
2005 epage = readw(ofsAddr + EndPage_txb);
2006 tail = readw(ofsAddr + TXwptr);
2007 head = readw(ofsAddr + TXrptr);
2008 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
2011 moxaLog.txcnt[port->port.tty->index] += c;
2013 if (spage == epage) {
2014 bufhead = readw(ofsAddr + Ofs_txb);
2015 writew(spage, baseAddr + Control_reg);
2018 len = head - tail - 1;
2020 len = tx_mask + 1 - tail;
2021 len = (c > len) ? len : c;
2022 ofs = baseAddr + DynPage_addr + bufhead + tail;
2023 memcpy_toio(ofs, buffer, len);
2025 tail = (tail + len) & tx_mask;
2029 pageno = spage + (tail >> 13);
2030 pageofs = tail & Page_mask;
2032 len = Page_size - pageofs;
2035 writeb(pageno, baseAddr + Control_reg);
2036 ofs = baseAddr + DynPage_addr + pageofs;
2037 memcpy_toio(ofs, buffer, len);
2039 if (++pageno == epage)
2044 tail = (tail + total) & tx_mask;
2046 writew(tail, ofsAddr + TXwptr);
2047 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2051 static int MoxaPortReadData(struct moxa_port *port)
2053 struct tty_struct *tty = port->port.tty;
2055 void __iomem *baseAddr, *ofsAddr, *ofs;
2056 unsigned int count, len, total;
2057 u16 tail, rx_mask, spage, epage;
2058 u16 pageno, pageofs, bufhead, head;
2060 ofsAddr = port->tableAddr;
2061 baseAddr = port->board->basemem;
2062 head = readw(ofsAddr + RXrptr);
2063 tail = readw(ofsAddr + RXwptr);
2064 rx_mask = readw(ofsAddr + RX_mask);
2065 spage = readw(ofsAddr + Page_rxb);
2066 epage = readw(ofsAddr + EndPage_rxb);
2067 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
2072 moxaLog.rxcnt[tty->index] += total;
2073 if (spage == epage) {
2074 bufhead = readw(ofsAddr + Ofs_rxb);
2075 writew(spage, baseAddr + Control_reg);
2077 ofs = baseAddr + DynPage_addr + bufhead + head;
2078 len = (tail >= head) ? (tail - head) :
2079 (rx_mask + 1 - head);
2080 len = tty_prepare_flip_string(tty, &dst,
2082 memcpy_fromio(dst, ofs, len);
2083 head = (head + len) & rx_mask;
2087 pageno = spage + (head >> 13);
2088 pageofs = head & Page_mask;
2090 writew(pageno, baseAddr + Control_reg);
2091 ofs = baseAddr + DynPage_addr + pageofs;
2092 len = tty_prepare_flip_string(tty, &dst,
2093 min(Page_size - pageofs, count));
2094 memcpy_fromio(dst, ofs, len);
2097 pageofs = (pageofs + len) & Page_mask;
2098 if (pageofs == 0 && ++pageno == epage)
2101 head = (head + total) & rx_mask;
2103 writew(head, ofsAddr + RXrptr);
2104 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2105 moxaLowWaterChk = 1;
2106 port->lowChkFlag = 1;
2112 static int MoxaPortTxQueue(struct moxa_port *port)
2114 void __iomem *ofsAddr = port->tableAddr;
2115 u16 rptr, wptr, mask;
2117 rptr = readw(ofsAddr + TXrptr);
2118 wptr = readw(ofsAddr + TXwptr);
2119 mask = readw(ofsAddr + TX_mask);
2120 return (wptr - rptr) & mask;
2123 static int MoxaPortTxFree(struct moxa_port *port)
2125 void __iomem *ofsAddr = port->tableAddr;
2126 u16 rptr, wptr, mask;
2128 rptr = readw(ofsAddr + TXrptr);
2129 wptr = readw(ofsAddr + TXwptr);
2130 mask = readw(ofsAddr + TX_mask);
2131 return mask - ((wptr - rptr) & mask);
2134 static int MoxaPortRxQueue(struct moxa_port *port)
2136 void __iomem *ofsAddr = port->tableAddr;
2137 u16 rptr, wptr, mask;
2139 rptr = readw(ofsAddr + RXrptr);
2140 wptr = readw(ofsAddr + RXwptr);
2141 mask = readw(ofsAddr + RX_mask);
2142 return (wptr - rptr) & mask;
2145 static void MoxaPortTxDisable(struct moxa_port *port)
2147 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2150 static void MoxaPortTxEnable(struct moxa_port *port)
2152 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2155 static int moxa_get_serial_info(struct moxa_port *info,
2156 struct serial_struct __user *retinfo)
2158 struct serial_struct tmp = {
2160 .line = info->port.tty->index,
2161 .flags = info->port.flags,
2162 .baud_base = 921600,
2163 .close_delay = info->port.close_delay
2165 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2169 static int moxa_set_serial_info(struct moxa_port *info,
2170 struct serial_struct __user *new_info)
2172 struct serial_struct new_serial;
2174 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2177 if (new_serial.irq != 0 || new_serial.port != 0 ||
2178 new_serial.custom_divisor != 0 ||
2179 new_serial.baud_base != 921600)
2182 if (!capable(CAP_SYS_ADMIN)) {
2183 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2184 (info->port.flags & ~ASYNC_USR_MASK)))
2187 info->port.close_delay = new_serial.close_delay * HZ / 100;
2189 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2190 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
2192 MoxaSetFifo(info, new_serial.type == PORT_16550A);
2194 info->type = new_serial.type;
2200 /*****************************************************************************
2201 * Static local functions: *
2202 *****************************************************************************/
2204 static void MoxaSetFifo(struct moxa_port *port, int enable)
2206 void __iomem *ofsAddr = port->tableAddr;
2209 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2210 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2212 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2213 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);