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 moxa_port *);
210 * moxa board interface functions:
212 static void MoxaPortEnable(struct moxa_port *);
213 static void MoxaPortDisable(struct moxa_port *);
214 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
215 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
216 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
217 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
218 static int MoxaPortLineStatus(struct moxa_port *);
219 static void MoxaPortFlushData(struct moxa_port *, int);
220 static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int);
221 static int MoxaPortReadData(struct moxa_port *);
222 static int MoxaPortTxQueue(struct moxa_port *);
223 static int MoxaPortRxQueue(struct moxa_port *);
224 static int MoxaPortTxFree(struct moxa_port *);
225 static void MoxaPortTxDisable(struct moxa_port *);
226 static void MoxaPortTxEnable(struct moxa_port *);
227 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
228 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
229 static void MoxaSetFifo(struct moxa_port *port, int enable);
235 static void moxa_wait_finish(void __iomem *ofsAddr)
237 unsigned long end = jiffies + moxaFuncTout;
239 while (readw(ofsAddr + FuncCode) != 0)
240 if (time_after(jiffies, end))
242 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
243 printk(KERN_WARNING "moxa function expired\n");
246 static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
248 writew(arg, ofsAddr + FuncArg);
249 writew(cmd, ofsAddr + FuncCode);
250 moxa_wait_finish(ofsAddr);
253 static void moxa_low_water_check(void __iomem *ofsAddr)
255 u16 rptr, wptr, mask, len;
257 if (readb(ofsAddr + FlagStat) & Xoff_state) {
258 rptr = readw(ofsAddr + RXrptr);
259 wptr = readw(ofsAddr + RXwptr);
260 mask = readw(ofsAddr + RX_mask);
261 len = (wptr - rptr) & mask;
262 if (len <= Low_water)
263 moxafunc(ofsAddr, FC_SendXon, 0);
271 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
272 unsigned int cmd, unsigned long arg)
274 struct moxa_port *ch = tty->driver_data;
275 void __user *argp = (void __user *)arg;
278 if (tty->index == MAX_PORTS) {
279 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
280 cmd != MOXA_GETMSTATUS)
286 case MOXA_GETDATACOUNT:
287 moxaLog.tick = jiffies;
288 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
291 case MOXA_FLUSH_QUEUE:
292 MoxaPortFlushData(ch, arg);
294 case MOXA_GET_IOQUEUE: {
295 struct moxaq_str __user *argm = argp;
296 struct moxaq_str tmp;
300 mutex_lock(&moxa_openlock);
301 for (i = 0; i < MAX_BOARDS; i++) {
302 p = moxa_boards[i].ports;
303 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
304 memset(&tmp, 0, sizeof(tmp));
305 if (moxa_boards[i].ready) {
306 tmp.inq = MoxaPortRxQueue(p);
307 tmp.outq = MoxaPortTxQueue(p);
309 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
310 mutex_unlock(&moxa_openlock);
315 mutex_unlock(&moxa_openlock);
317 } case MOXA_GET_OQUEUE:
318 status = MoxaPortTxQueue(ch);
319 ret = put_user(status, (unsigned long __user *)argp);
321 case MOXA_GET_IQUEUE:
322 status = MoxaPortRxQueue(ch);
323 ret = put_user(status, (unsigned long __user *)argp);
325 case MOXA_GETMSTATUS: {
326 struct mxser_mstatus __user *argm = argp;
327 struct mxser_mstatus tmp;
331 mutex_lock(&moxa_openlock);
332 for (i = 0; i < MAX_BOARDS; i++) {
333 p = moxa_boards[i].ports;
334 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
335 memset(&tmp, 0, sizeof(tmp));
336 if (!moxa_boards[i].ready)
339 status = MoxaPortLineStatus(p);
347 if (!p->port.tty || !p->port.tty->termios)
348 tmp.cflag = p->cflag;
350 tmp.cflag = p->port.tty->termios->c_cflag;
352 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
353 mutex_unlock(&moxa_openlock);
358 mutex_unlock(&moxa_openlock);
362 mutex_lock(&moxa_openlock);
363 ret = moxa_get_serial_info(ch, argp);
364 mutex_unlock(&moxa_openlock);
367 mutex_lock(&moxa_openlock);
368 ret = moxa_set_serial_info(ch, argp);
369 mutex_unlock(&moxa_openlock);
377 static int moxa_break_ctl(struct tty_struct *tty, int state)
379 struct moxa_port *port = tty->driver_data;
381 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
386 static const struct tty_operations moxa_ops = {
390 .write_room = moxa_write_room,
391 .flush_buffer = moxa_flush_buffer,
392 .chars_in_buffer = moxa_chars_in_buffer,
394 .throttle = moxa_throttle,
395 .unthrottle = moxa_unthrottle,
396 .set_termios = moxa_set_termios,
399 .hangup = moxa_hangup,
400 .break_ctl = moxa_break_ctl,
401 .tiocmget = moxa_tiocmget,
402 .tiocmset = moxa_tiocmset,
405 static struct tty_driver *moxaDriver;
406 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
407 static DEFINE_SPINLOCK(moxa_lock);
413 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
415 switch (brd->boardType) {
416 case MOXA_BOARD_C218_ISA:
417 case MOXA_BOARD_C218_PCI:
421 case MOXA_BOARD_CP204J:
435 static int moxa_check_fw(const void *ptr)
437 const __le16 *lptr = ptr;
439 if (*lptr != cpu_to_le16(0x7980))
445 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
448 void __iomem *baseAddr = brd->basemem;
451 writeb(HW_reset, baseAddr + Control_reg); /* reset */
453 memset_io(baseAddr, 0, 4096);
454 memcpy_toio(baseAddr, buf, len); /* download BIOS */
455 writeb(0, baseAddr + Control_reg); /* restart */
459 switch (brd->boardType) {
460 case MOXA_BOARD_C218_ISA:
461 case MOXA_BOARD_C218_PCI:
462 tmp = readw(baseAddr + C218_key);
463 if (tmp != C218_KeyCode)
466 case MOXA_BOARD_CP204J:
467 tmp = readw(baseAddr + C218_key);
468 if (tmp != CP204J_KeyCode)
472 tmp = readw(baseAddr + C320_key);
473 if (tmp != C320_KeyCode)
475 tmp = readw(baseAddr + C320_status);
476 if (tmp != STS_init) {
477 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
478 "module not found\n");
486 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
490 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
493 void __iomem *baseAddr = brd->basemem;
496 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
500 writew(len - 7168 - 2, baseAddr + C320bapi_len);
501 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
502 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
503 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
504 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
509 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
512 void __iomem *baseAddr = brd->basemem;
513 const u16 *uptr = ptr;
514 size_t wlen, len2, j;
515 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
516 unsigned int i, retry;
519 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
522 switch (brd->boardType) {
523 case MOXA_BOARD_CP204J:
524 case MOXA_BOARD_C218_ISA:
525 case MOXA_BOARD_C218_PCI:
527 loadbuf = C218_LoadBuf;
528 loadlen = C218DLoad_len;
529 checksum = C218check_sum;
530 checksum_ok = C218chksum_ok;
534 keycode = C320_KeyCode;
535 loadbuf = C320_LoadBuf;
536 loadlen = C320DLoad_len;
537 checksum = C320check_sum;
538 checksum_ok = C320chksum_ok;
544 for (i = 0; i < wlen; i++)
545 usum += le16_to_cpu(uptr[i]);
551 len2 = (wlen > 2048) ? 2048 : wlen;
553 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
556 writew(len2, baseAddr + loadlen);
557 writew(0, baseAddr + key);
558 for (i = 0; i < 100; i++) {
559 if (readw(baseAddr + key) == keycode)
563 if (readw(baseAddr + key) != keycode)
566 writew(0, baseAddr + loadlen);
567 writew(usum, baseAddr + checksum);
568 writew(0, baseAddr + key);
569 for (i = 0; i < 100; i++) {
570 if (readw(baseAddr + key) == keycode)
575 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
576 if (readb(baseAddr + checksum_ok) != 1)
579 writew(0, baseAddr + key);
580 for (i = 0; i < 600; i++) {
581 if (readw(baseAddr + Magic_no) == Magic_code)
585 if (readw(baseAddr + Magic_no) != Magic_code)
588 if (MOXA_IS_320(brd)) {
589 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
590 writew(0x3800, baseAddr + TMS320_PORT1);
591 writew(0x3900, baseAddr + TMS320_PORT2);
592 writew(28499, baseAddr + TMS320_CLOCK);
594 writew(0x3200, baseAddr + TMS320_PORT1);
595 writew(0x3400, baseAddr + TMS320_PORT2);
596 writew(19999, baseAddr + TMS320_CLOCK);
599 writew(1, baseAddr + Disable_IRQ);
600 writew(0, baseAddr + Magic_no);
601 for (i = 0; i < 500; i++) {
602 if (readw(baseAddr + Magic_no) == Magic_code)
606 if (readw(baseAddr + Magic_no) != Magic_code)
609 if (MOXA_IS_320(brd)) {
610 j = readw(baseAddr + Module_cnt);
613 brd->numPorts = j * 8;
614 writew(j, baseAddr + Module_no);
615 writew(0, baseAddr + Magic_no);
616 for (i = 0; i < 600; i++) {
617 if (readw(baseAddr + Magic_no) == Magic_code)
621 if (readw(baseAddr + Magic_no) != Magic_code)
624 brd->intNdx = baseAddr + IRQindex;
625 brd->intPend = baseAddr + IRQpending;
626 brd->intTable = baseAddr + IRQtable;
631 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
634 void __iomem *ofsAddr, *baseAddr = brd->basemem;
635 struct moxa_port *port;
639 printk(KERN_ERR "MOXA: bios length is not even\n");
643 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
647 switch (brd->boardType) {
648 case MOXA_BOARD_C218_ISA:
649 case MOXA_BOARD_C218_PCI:
650 case MOXA_BOARD_CP204J:
652 for (i = 0; i < brd->numPorts; i++, port++) {
655 port->tableAddr = baseAddr + Extern_table +
657 ofsAddr = port->tableAddr;
658 writew(C218rx_mask, ofsAddr + RX_mask);
659 writew(C218tx_mask, ofsAddr + TX_mask);
660 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
661 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
663 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
664 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
670 for (i = 0; i < brd->numPorts; i++, port++) {
673 port->tableAddr = baseAddr + Extern_table +
675 ofsAddr = port->tableAddr;
676 switch (brd->numPorts) {
678 writew(C320p8rx_mask, ofsAddr + RX_mask);
679 writew(C320p8tx_mask, ofsAddr + TX_mask);
680 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
681 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
682 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
683 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
687 writew(C320p16rx_mask, ofsAddr + RX_mask);
688 writew(C320p16tx_mask, ofsAddr + TX_mask);
689 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
690 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
691 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
692 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
696 writew(C320p24rx_mask, ofsAddr + RX_mask);
697 writew(C320p24tx_mask, ofsAddr + TX_mask);
698 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
699 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
700 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
701 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
704 writew(C320p32rx_mask, ofsAddr + RX_mask);
705 writew(C320p32tx_mask, ofsAddr + TX_mask);
706 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
707 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
708 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
709 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
710 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
719 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
721 const void *ptr = fw->data;
725 unsigned int a, lenp, lencnt;
728 __le32 magic; /* 0x34303430 */
730 u8 type; /* UNIX = 3 */
731 u8 model; /* C218T=1, C320T=2, CP204=3 */
736 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
738 if (fw->size < MOXA_FW_HDRLEN) {
739 strcpy(rsn, "too short (even header won't fit)");
742 if (hdr->magic != cpu_to_le32(0x30343034)) {
743 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
746 if (hdr->type != 3) {
747 sprintf(rsn, "not for linux, type is %u", hdr->type);
750 if (moxa_check_fw_model(brd, hdr->model)) {
751 sprintf(rsn, "not for this card, model is %u", hdr->model);
755 len = MOXA_FW_HDRLEN;
756 lencnt = hdr->model == 2 ? 5 : 3;
757 for (a = 0; a < ARRAY_SIZE(lens); a++) {
758 lens[a] = le16_to_cpu(hdr->len[a]);
759 if (lens[a] && len + lens[a] <= fw->size &&
760 moxa_check_fw(&fw->data[len]))
761 printk(KERN_WARNING "MOXA firmware: unexpected input "
762 "at offset %u, but going on\n", (u32)len);
763 if (!lens[a] && a < lencnt) {
764 sprintf(rsn, "too few entries in fw file");
770 if (len != fw->size) {
771 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
776 ptr += MOXA_FW_HDRLEN;
779 strcpy(rsn, "read above");
781 ret = moxa_load_bios(brd, ptr, lens[lenp]);
785 /* we skip the tty section (lens[1]), since we don't need it */
786 ptr += lens[lenp] + lens[lenp + 1];
787 lenp += 2; /* comm */
789 if (hdr->model == 2) {
790 ret = moxa_load_320b(brd, ptr, lens[lenp]);
793 /* skip another tty */
794 ptr += lens[lenp] + lens[lenp + 1];
798 ret = moxa_load_code(brd, ptr, lens[lenp]);
804 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
808 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
810 const struct firmware *fw;
816 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
818 if (brd->ports == NULL) {
819 printk(KERN_ERR "cannot allocate memory for ports\n");
824 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
825 tty_port_init(&p->port);
826 p->type = PORT_16550A;
827 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
830 switch (brd->boardType) {
831 case MOXA_BOARD_C218_ISA:
832 case MOXA_BOARD_C218_PCI:
833 file = "c218tunx.cod";
835 case MOXA_BOARD_CP204J:
836 file = "cp204unx.cod";
839 file = "c320tunx.cod";
843 ret = request_firmware(&fw, file, dev);
845 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
846 "you've placed '%s' file into your firmware "
847 "loader directory (e.g. /lib/firmware)\n",
852 ret = moxa_load_fw(brd, fw);
854 release_firmware(fw);
859 spin_lock_bh(&moxa_lock);
861 if (!timer_pending(&moxaTimer))
862 mod_timer(&moxaTimer, jiffies + HZ / 50);
863 spin_unlock_bh(&moxa_lock);
872 static void moxa_board_deinit(struct moxa_board_conf *brd)
874 unsigned int a, opened;
876 mutex_lock(&moxa_openlock);
877 spin_lock_bh(&moxa_lock);
879 spin_unlock_bh(&moxa_lock);
881 /* pci hot-un-plug support */
882 for (a = 0; a < brd->numPorts; a++)
883 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
884 tty_hangup(brd->ports[a].port.tty);
887 for (a = 0; a < brd->numPorts; a++)
888 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
890 mutex_unlock(&moxa_openlock);
894 mutex_lock(&moxa_openlock);
897 iounmap(brd->basemem);
903 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
904 const struct pci_device_id *ent)
906 struct moxa_board_conf *board;
908 int board_type = ent->driver_data;
911 retval = pci_enable_device(pdev);
913 dev_err(&pdev->dev, "can't enable pci device\n");
917 for (i = 0; i < MAX_BOARDS; i++)
918 if (moxa_boards[i].basemem == NULL)
922 if (i >= MAX_BOARDS) {
923 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
924 "found. Board is ignored.\n", MAX_BOARDS);
928 board = &moxa_boards[i];
930 retval = pci_request_region(pdev, 2, "moxa-base");
932 dev_err(&pdev->dev, "can't request pci region 2\n");
936 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
937 if (board->basemem == NULL) {
938 dev_err(&pdev->dev, "can't remap io space 2\n");
942 board->boardType = board_type;
943 switch (board_type) {
944 case MOXA_BOARD_C218_ISA:
945 case MOXA_BOARD_C218_PCI:
949 case MOXA_BOARD_CP204J:
956 board->busType = MOXA_BUS_TYPE_PCI;
958 retval = moxa_init_board(board, &pdev->dev);
962 pci_set_drvdata(pdev, board);
964 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
965 moxa_brdname[board_type - 1], board->numPorts);
969 iounmap(board->basemem);
970 board->basemem = NULL;
972 pci_release_region(pdev, 2);
977 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
979 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
981 moxa_board_deinit(brd);
983 pci_release_region(pdev, 2);
986 static struct pci_driver moxa_pci_driver = {
988 .id_table = moxa_pcibrds,
989 .probe = moxa_pci_probe,
990 .remove = __devexit_p(moxa_pci_remove)
992 #endif /* CONFIG_PCI */
994 static int __init moxa_init(void)
996 unsigned int isabrds = 0;
999 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1001 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1005 moxaDriver->owner = THIS_MODULE;
1006 moxaDriver->name = "ttyMX";
1007 moxaDriver->major = ttymajor;
1008 moxaDriver->minor_start = 0;
1009 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1010 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1011 moxaDriver->init_termios = tty_std_termios;
1012 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1013 moxaDriver->init_termios.c_ispeed = 9600;
1014 moxaDriver->init_termios.c_ospeed = 9600;
1015 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1016 tty_set_operations(moxaDriver, &moxa_ops);
1018 if (tty_register_driver(moxaDriver)) {
1019 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1020 put_tty_driver(moxaDriver);
1024 /* Find the boards defined from module args. */
1027 struct moxa_board_conf *brd = moxa_boards;
1029 for (i = 0; i < MAX_BOARDS; i++) {
1032 if (type[i] == MOXA_BOARD_C218_ISA ||
1033 type[i] == MOXA_BOARD_C320_ISA) {
1034 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1035 isabrds + 1, moxa_brdname[type[i] - 1],
1037 brd->boardType = type[i];
1038 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1040 brd->busType = MOXA_BUS_TYPE_ISA;
1041 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
1042 if (!brd->basemem) {
1043 printk(KERN_ERR "MOXA: can't remap %lx\n",
1047 if (moxa_init_board(brd, NULL)) {
1048 iounmap(brd->basemem);
1049 brd->basemem = NULL;
1053 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1054 "ready (%u ports, firmware loaded)\n",
1055 baseaddr[i], brd->numPorts);
1065 retval = pci_register_driver(&moxa_pci_driver);
1067 printk(KERN_ERR "Can't register MOXA pci driver!\n");
1076 static void __exit moxa_exit(void)
1081 pci_unregister_driver(&moxa_pci_driver);
1084 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1085 if (moxa_boards[i].ready)
1086 moxa_board_deinit(&moxa_boards[i]);
1088 del_timer_sync(&moxaTimer);
1090 if (tty_unregister_driver(moxaDriver))
1091 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1093 put_tty_driver(moxaDriver);
1096 module_init(moxa_init);
1097 module_exit(moxa_exit);
1099 static void moxa_close_port(struct moxa_port *ch)
1102 MoxaPortFlushData(ch, 2);
1103 ch->port.flags &= ~ASYNC_NORMAL_ACTIVE;
1104 ch->port.tty->driver_data = NULL;
1105 ch->port.tty = NULL;
1108 static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1109 struct moxa_port *ch)
1116 prepare_to_wait(&ch->port.open_wait, &wait, TASK_INTERRUPTIBLE);
1117 if (tty_hung_up_p(filp)) {
1118 #ifdef SERIAL_DO_RESTART
1119 retval = -ERESTARTSYS;
1125 spin_lock_bh(&moxa_lock);
1127 spin_unlock_bh(&moxa_lock);
1131 if (signal_pending(current)) {
1132 retval = -ERESTARTSYS;
1137 finish_wait(&ch->port.open_wait, &wait);
1142 static int moxa_open(struct tty_struct *tty, struct file *filp)
1144 struct moxa_board_conf *brd;
1145 struct moxa_port *ch;
1150 if (port == MAX_PORTS) {
1151 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1153 if (mutex_lock_interruptible(&moxa_openlock))
1154 return -ERESTARTSYS;
1155 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1157 mutex_unlock(&moxa_openlock);
1161 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1163 tty->driver_data = ch;
1165 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
1166 ch->statusflags = 0;
1167 moxa_set_tty_param(tty, tty->termios);
1168 MoxaPortLineCtrl(ch, 1, 1);
1170 MoxaSetFifo(ch, ch->type == PORT_16550A);
1171 ch->port.flags |= ASYNC_INITIALIZED;
1173 mutex_unlock(&moxa_openlock);
1176 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1177 retval = moxa_block_till_ready(tty, filp, ch);
1178 mutex_lock(&moxa_openlock);
1180 if (ch->port.count) /* 0 means already hung up... */
1181 if (--ch->port.count == 0)
1182 moxa_close_port(ch);
1184 ch->port.flags |= ASYNC_NORMAL_ACTIVE;
1185 mutex_unlock(&moxa_openlock);
1190 static void moxa_close(struct tty_struct *tty, struct file *filp)
1192 struct moxa_port *ch;
1196 if (port == MAX_PORTS || tty_hung_up_p(filp))
1199 mutex_lock(&moxa_openlock);
1200 ch = tty->driver_data;
1203 if (tty->count == 1 && ch->port.count != 1) {
1204 printk(KERN_WARNING "moxa_close: bad serial port count; "
1205 "tty->count is 1, ch->port.count is %d\n", ch->port.count);
1208 if (--ch->port.count < 0) {
1209 printk(KERN_WARNING "moxa_close: bad serial port count, "
1210 "device=%s\n", tty->name);
1216 ch->cflag = tty->termios->c_cflag;
1217 if (ch->port.flags & ASYNC_INITIALIZED) {
1218 moxa_setup_empty_event(tty);
1219 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
1222 moxa_close_port(ch);
1224 mutex_unlock(&moxa_openlock);
1227 static int moxa_write(struct tty_struct *tty,
1228 const unsigned char *buf, int count)
1230 struct moxa_port *ch = tty->driver_data;
1236 spin_lock_bh(&moxa_lock);
1237 len = MoxaPortWriteData(ch, buf, count);
1238 spin_unlock_bh(&moxa_lock);
1240 ch->statusflags |= LOWWAIT;
1244 static int moxa_write_room(struct tty_struct *tty)
1246 struct moxa_port *ch;
1250 ch = tty->driver_data;
1253 return MoxaPortTxFree(ch);
1256 static void moxa_flush_buffer(struct tty_struct *tty)
1258 struct moxa_port *ch = tty->driver_data;
1262 MoxaPortFlushData(ch, 1);
1266 static int moxa_chars_in_buffer(struct tty_struct *tty)
1268 struct moxa_port *ch = tty->driver_data;
1272 * Sigh...I have to check if driver_data is NULL here, because
1273 * if an open() fails, the TTY subsystem eventually calls
1274 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1275 * routine. And since the open() failed, we return 0 here. TDJ
1280 chars = MoxaPortTxQueue(ch);
1283 * Make it possible to wakeup anything waiting for output
1284 * in tty_ioctl.c, etc.
1286 if (!(ch->statusflags & EMPTYWAIT))
1287 moxa_setup_empty_event(tty);
1293 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1295 struct moxa_port *ch;
1296 int flag = 0, dtr, rts;
1298 mutex_lock(&moxa_openlock);
1299 ch = tty->driver_data;
1301 mutex_unlock(&moxa_openlock);
1305 MoxaPortGetLineOut(ch, &dtr, &rts);
1310 dtr = MoxaPortLineStatus(ch);
1317 mutex_unlock(&moxa_openlock);
1321 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1322 unsigned int set, unsigned int clear)
1324 struct moxa_port *ch;
1329 mutex_lock(&moxa_openlock);
1330 ch = tty->driver_data;
1332 mutex_unlock(&moxa_openlock);
1336 MoxaPortGetLineOut(ch, &dtr, &rts);
1337 if (set & TIOCM_RTS)
1339 if (set & TIOCM_DTR)
1341 if (clear & TIOCM_RTS)
1343 if (clear & TIOCM_DTR)
1345 MoxaPortLineCtrl(ch, dtr, rts);
1346 mutex_unlock(&moxa_openlock);
1350 static void moxa_throttle(struct tty_struct *tty)
1352 struct moxa_port *ch = tty->driver_data;
1354 ch->statusflags |= THROTTLE;
1357 static void moxa_unthrottle(struct tty_struct *tty)
1359 struct moxa_port *ch = tty->driver_data;
1361 ch->statusflags &= ~THROTTLE;
1364 static void moxa_set_termios(struct tty_struct *tty,
1365 struct ktermios *old_termios)
1367 struct moxa_port *ch = tty->driver_data;
1371 moxa_set_tty_param(tty, old_termios);
1372 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1373 wake_up_interruptible(&ch->port.open_wait);
1376 static void moxa_stop(struct tty_struct *tty)
1378 struct moxa_port *ch = tty->driver_data;
1382 MoxaPortTxDisable(ch);
1383 ch->statusflags |= TXSTOPPED;
1387 static void moxa_start(struct tty_struct *tty)
1389 struct moxa_port *ch = tty->driver_data;
1394 if (!(ch->statusflags & TXSTOPPED))
1397 MoxaPortTxEnable(ch);
1398 ch->statusflags &= ~TXSTOPPED;
1401 static void moxa_hangup(struct tty_struct *tty)
1403 struct moxa_port *ch;
1405 mutex_lock(&moxa_openlock);
1406 ch = tty->driver_data;
1408 mutex_unlock(&moxa_openlock);
1412 moxa_close_port(ch);
1413 mutex_unlock(&moxa_openlock);
1415 wake_up_interruptible(&ch->port.open_wait);
1418 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1422 if (dcd != p->DCDState && p->port.tty && C_CLOCAL(p->port.tty)) {
1424 tty_hangup(p->port.tty);
1429 static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1432 struct tty_struct *tty = p->port.tty;
1433 void __iomem *ofsAddr;
1434 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
1438 if ((p->statusflags & EMPTYWAIT) &&
1439 MoxaPortTxQueue(p) == 0) {
1440 p->statusflags &= ~EMPTYWAIT;
1443 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1444 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1445 p->statusflags &= ~LOWWAIT;
1449 if (inited && !(p->statusflags & THROTTLE) &&
1450 MoxaPortRxQueue(p) > 0) { /* RX */
1451 MoxaPortReadData(p);
1452 tty_schedule_flip(tty);
1455 p->statusflags &= ~EMPTYWAIT;
1456 MoxaPortFlushData(p, 0); /* flush RX */
1459 if (!handle) /* nothing else to do */
1462 intr = readw(ip); /* port irq status */
1466 writew(0, ip); /* ACK port */
1467 ofsAddr = p->tableAddr;
1468 if (intr & IntrTx) /* disable tx intr */
1469 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1470 ofsAddr + HostStat);
1475 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1476 tty_insert_flip_char(tty, 0, TTY_BREAK);
1477 tty_schedule_flip(tty);
1480 if (intr & IntrLine)
1481 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1486 static void moxa_poll(unsigned long ignored)
1488 struct moxa_board_conf *brd;
1490 unsigned int card, port, served = 0;
1492 spin_lock(&moxa_lock);
1493 for (card = 0; card < MAX_BOARDS; card++) {
1494 brd = &moxa_boards[card];
1501 if (readb(brd->intPend) == 0xff)
1502 ip = brd->intTable + readb(brd->intNdx);
1504 for (port = 0; port < brd->numPorts; port++)
1505 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1508 writeb(0, brd->intPend); /* ACK */
1510 if (moxaLowWaterChk) {
1511 struct moxa_port *p = brd->ports;
1512 for (port = 0; port < brd->numPorts; port++, p++)
1513 if (p->lowChkFlag) {
1515 moxa_low_water_check(p->tableAddr);
1519 moxaLowWaterChk = 0;
1522 mod_timer(&moxaTimer, jiffies + HZ / 50);
1523 spin_unlock(&moxa_lock);
1526 /******************************************************************************/
1528 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1530 register struct ktermios *ts = tty->termios;
1531 struct moxa_port *ch = tty->driver_data;
1532 int rts, cts, txflow, rxflow, xany, baud;
1534 rts = cts = txflow = rxflow = xany = 0;
1535 if (ts->c_cflag & CRTSCTS)
1537 if (ts->c_iflag & IXON)
1539 if (ts->c_iflag & IXOFF)
1541 if (ts->c_iflag & IXANY)
1544 /* Clear the features we don't support */
1545 ts->c_cflag &= ~CMSPAR;
1546 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1547 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1549 baud = tty_termios_baud_rate(old_termios);
1550 /* Not put the baud rate into the termios data */
1551 tty_encode_baud_rate(tty, baud, baud);
1554 static void moxa_setup_empty_event(struct tty_struct *tty)
1556 struct moxa_port *ch = tty->driver_data;
1558 spin_lock_bh(&moxa_lock);
1559 ch->statusflags |= EMPTYWAIT;
1560 spin_unlock_bh(&moxa_lock);
1563 static void moxa_shut_down(struct moxa_port *ch)
1565 struct tty_struct *tp = ch->port.tty;
1567 if (!(ch->port.flags & ASYNC_INITIALIZED))
1570 MoxaPortDisable(ch);
1573 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1576 MoxaPortLineCtrl(ch, 0, 0);
1578 spin_lock_bh(&moxa_lock);
1579 ch->port.flags &= ~ASYNC_INITIALIZED;
1580 spin_unlock_bh(&moxa_lock);
1583 /*****************************************************************************
1584 * Driver level functions: *
1585 *****************************************************************************/
1587 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1589 void __iomem *ofsAddr;
1590 if (mode < 0 || mode > 2)
1592 ofsAddr = port->tableAddr;
1593 moxafunc(ofsAddr, FC_FlushQueue, mode);
1595 port->lowChkFlag = 0;
1596 moxa_low_water_check(ofsAddr);
1601 * Moxa Port Number Description:
1603 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1604 * the port number using in MOXA driver functions will be 0 to 31 for
1605 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1606 * to 127 for fourth. For example, if you setup three MOXA boards,
1607 * first board is C218, second board is C320-16 and third board is
1608 * C320-32. The port number of first board (C218 - 8 ports) is from
1609 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1610 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1611 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1612 * 127 will be invalid.
1615 * Moxa Functions Description:
1617 * Function 1: Driver initialization routine, this routine must be
1618 * called when initialized driver.
1620 * void MoxaDriverInit();
1623 * Function 2: Moxa driver private IOCTL command processing.
1625 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1627 * unsigned int cmd : IOCTL command
1628 * unsigned long arg : IOCTL argument
1629 * int port : port number (0 - 127)
1636 * Function 6: Enable this port to start Tx/Rx data.
1638 * void MoxaPortEnable(int port);
1639 * int port : port number (0 - 127)
1642 * Function 7: Disable this port
1644 * void MoxaPortDisable(int port);
1645 * int port : port number (0 - 127)
1648 * Function 10: Setting baud rate of this port.
1650 * speed_t MoxaPortSetBaud(int port, speed_t baud);
1651 * int port : port number (0 - 127)
1652 * long baud : baud rate (50 - 115200)
1654 * return: 0 : this port is invalid or baud < 50
1655 * 50 - 115200 : the real baud rate set to the port, if
1656 * the argument baud is large than maximun
1657 * available baud rate, the real setting
1658 * baud rate will be the maximun baud rate.
1661 * Function 12: Configure the port.
1663 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1664 * int port : port number (0 - 127)
1665 * struct ktermios * termio : termio structure pointer
1666 * speed_t baud : baud rate
1668 * return: -1 : this port is invalid or termio == NULL
1672 * Function 13: Get the DTR/RTS state of this port.
1674 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1675 * int port : port number (0 - 127)
1676 * int * dtrState : pointer to INT to receive the current DTR
1677 * state. (if NULL, this function will not
1678 * write to this address)
1679 * int * rtsState : pointer to INT to receive the current RTS
1680 * state. (if NULL, this function will not
1681 * write to this address)
1683 * return: -1 : this port is invalid
1687 * Function 14: Setting the DTR/RTS output state of this port.
1689 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1690 * int port : port number (0 - 127)
1691 * int dtrState : DTR output state (0: off, 1: on)
1692 * int rtsState : RTS output state (0: off, 1: on)
1695 * Function 15: Setting the flow control of this port.
1697 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1698 * int txFlow,int xany);
1699 * int port : port number (0 - 127)
1700 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1701 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1702 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1703 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1704 * int xany : S/W XANY flow control (0: no, 1: yes)
1707 * Function 16: Get ths line status of this port
1709 * int MoxaPortLineStatus(int port);
1710 * int port : port number (0 - 127)
1712 * return: Bit 0 - CTS state (0: off, 1: on)
1713 * Bit 1 - DSR state (0: off, 1: on)
1714 * Bit 2 - DCD state (0: off, 1: on)
1717 * Function 19: Flush the Rx/Tx buffer data of this port.
1719 * void MoxaPortFlushData(int port, int mode);
1720 * int port : port number (0 - 127)
1722 * 0 : flush the Rx buffer
1723 * 1 : flush the Tx buffer
1724 * 2 : flush the Rx and Tx buffer
1727 * Function 20: Write data.
1729 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1730 * int port : port number (0 - 127)
1731 * unsigned char * buffer : pointer to write data buffer.
1732 * int length : write data length
1734 * return: 0 - length : real write data length
1737 * Function 21: Read data.
1739 * int MoxaPortReadData(int port, struct tty_struct *tty);
1740 * int port : port number (0 - 127)
1741 * struct tty_struct *tty : tty for data
1743 * return: 0 - length : real read data length
1746 * Function 24: Get the Tx buffer current queued data bytes
1748 * int MoxaPortTxQueue(int port);
1749 * int port : port number (0 - 127)
1751 * return: .. : Tx buffer current queued data bytes
1754 * Function 25: Get the Tx buffer current free space
1756 * int MoxaPortTxFree(int port);
1757 * int port : port number (0 - 127)
1759 * return: .. : Tx buffer current free space
1762 * Function 26: Get the Rx buffer current queued data bytes
1764 * int MoxaPortRxQueue(int port);
1765 * int port : port number (0 - 127)
1767 * return: .. : Rx buffer current queued data bytes
1770 * Function 28: Disable port data transmission.
1772 * void MoxaPortTxDisable(int port);
1773 * int port : port number (0 - 127)
1776 * Function 29: Enable port data transmission.
1778 * void MoxaPortTxEnable(int port);
1779 * int port : port number (0 - 127)
1782 * Function 31: Get the received BREAK signal count and reset it.
1784 * int MoxaPortResetBrkCnt(int port);
1785 * int port : port number (0 - 127)
1787 * return: 0 - .. : BREAK signal count
1792 static void MoxaPortEnable(struct moxa_port *port)
1794 void __iomem *ofsAddr;
1797 ofsAddr = port->tableAddr;
1798 writew(lowwater, ofsAddr + Low_water);
1799 if (MOXA_IS_320(port->board))
1800 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1802 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1803 ofsAddr + HostStat);
1805 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1806 moxafunc(ofsAddr, FC_FlushQueue, 2);
1808 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1809 MoxaPortLineStatus(port);
1812 static void MoxaPortDisable(struct moxa_port *port)
1814 void __iomem *ofsAddr = port->tableAddr;
1816 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1817 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1818 writew(0, ofsAddr + HostStat);
1819 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1822 static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
1824 void __iomem *ofsAddr = port->tableAddr;
1825 unsigned int clock, val;
1828 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1835 moxafunc(ofsAddr, FC_SetBaud, val);
1840 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1843 void __iomem *ofsAddr;
1847 ofsAddr = port->tableAddr;
1848 cflag = termio->c_cflag; /* termio->c_cflag */
1850 mode = termio->c_cflag & CSIZE;
1853 else if (mode == CS6)
1855 else if (mode == CS7)
1857 else if (mode == CS8)
1860 if (termio->c_cflag & CSTOPB) {
1868 if (termio->c_cflag & PARENB) {
1869 if (termio->c_cflag & PARODD)
1876 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
1878 if (MOXA_IS_320(port->board) && baud >= 921600)
1881 baud = MoxaPortSetBaud(port, baud);
1883 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1884 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1885 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1886 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1887 moxa_wait_finish(ofsAddr);
1893 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1897 *dtrState = !!(port->lineCtrl & DTR_ON);
1899 *rtsState = !!(port->lineCtrl & RTS_ON);
1904 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1912 port->lineCtrl = mode;
1913 moxafunc(port->tableAddr, FC_LineControl, mode);
1916 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1917 int txflow, int rxflow, int txany)
1922 mode |= RTS_FlowCtl;
1924 mode |= CTS_FlowCtl;
1931 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
1934 static int MoxaPortLineStatus(struct moxa_port *port)
1936 void __iomem *ofsAddr;
1939 ofsAddr = port->tableAddr;
1940 if (MOXA_IS_320(port->board)) {
1941 moxafunc(ofsAddr, FC_LineStatus, 0);
1942 val = readw(ofsAddr + FuncArg);
1944 val = readw(ofsAddr + FlagStat) >> 4;
1949 spin_lock_bh(&moxa_lock);
1950 moxa_new_dcdstate(port, val & 8);
1951 spin_unlock_bh(&moxa_lock);
1956 static int MoxaPortWriteData(struct moxa_port *port,
1957 const unsigned char *buffer, int len)
1959 void __iomem *baseAddr, *ofsAddr, *ofs;
1960 unsigned int c, total;
1961 u16 head, tail, tx_mask, spage, epage;
1962 u16 pageno, pageofs, bufhead;
1964 ofsAddr = port->tableAddr;
1965 baseAddr = port->board->basemem;
1966 tx_mask = readw(ofsAddr + TX_mask);
1967 spage = readw(ofsAddr + Page_txb);
1968 epage = readw(ofsAddr + EndPage_txb);
1969 tail = readw(ofsAddr + TXwptr);
1970 head = readw(ofsAddr + TXrptr);
1971 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
1974 moxaLog.txcnt[port->port.tty->index] += c;
1976 if (spage == epage) {
1977 bufhead = readw(ofsAddr + Ofs_txb);
1978 writew(spage, baseAddr + Control_reg);
1981 len = head - tail - 1;
1983 len = tx_mask + 1 - tail;
1984 len = (c > len) ? len : c;
1985 ofs = baseAddr + DynPage_addr + bufhead + tail;
1986 memcpy_toio(ofs, buffer, len);
1988 tail = (tail + len) & tx_mask;
1992 pageno = spage + (tail >> 13);
1993 pageofs = tail & Page_mask;
1995 len = Page_size - pageofs;
1998 writeb(pageno, baseAddr + Control_reg);
1999 ofs = baseAddr + DynPage_addr + pageofs;
2000 memcpy_toio(ofs, buffer, len);
2002 if (++pageno == epage)
2007 tail = (tail + total) & tx_mask;
2009 writew(tail, ofsAddr + TXwptr);
2010 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2014 static int MoxaPortReadData(struct moxa_port *port)
2016 struct tty_struct *tty = port->port.tty;
2018 void __iomem *baseAddr, *ofsAddr, *ofs;
2019 unsigned int count, len, total;
2020 u16 tail, rx_mask, spage, epage;
2021 u16 pageno, pageofs, bufhead, head;
2023 ofsAddr = port->tableAddr;
2024 baseAddr = port->board->basemem;
2025 head = readw(ofsAddr + RXrptr);
2026 tail = readw(ofsAddr + RXwptr);
2027 rx_mask = readw(ofsAddr + RX_mask);
2028 spage = readw(ofsAddr + Page_rxb);
2029 epage = readw(ofsAddr + EndPage_rxb);
2030 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
2035 moxaLog.rxcnt[tty->index] += total;
2036 if (spage == epage) {
2037 bufhead = readw(ofsAddr + Ofs_rxb);
2038 writew(spage, baseAddr + Control_reg);
2040 ofs = baseAddr + DynPage_addr + bufhead + head;
2041 len = (tail >= head) ? (tail - head) :
2042 (rx_mask + 1 - head);
2043 len = tty_prepare_flip_string(tty, &dst,
2045 memcpy_fromio(dst, ofs, len);
2046 head = (head + len) & rx_mask;
2050 pageno = spage + (head >> 13);
2051 pageofs = head & Page_mask;
2053 writew(pageno, baseAddr + Control_reg);
2054 ofs = baseAddr + DynPage_addr + pageofs;
2055 len = tty_prepare_flip_string(tty, &dst,
2056 min(Page_size - pageofs, count));
2057 memcpy_fromio(dst, ofs, len);
2060 pageofs = (pageofs + len) & Page_mask;
2061 if (pageofs == 0 && ++pageno == epage)
2064 head = (head + total) & rx_mask;
2066 writew(head, ofsAddr + RXrptr);
2067 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2068 moxaLowWaterChk = 1;
2069 port->lowChkFlag = 1;
2075 static int MoxaPortTxQueue(struct moxa_port *port)
2077 void __iomem *ofsAddr = port->tableAddr;
2078 u16 rptr, wptr, mask;
2080 rptr = readw(ofsAddr + TXrptr);
2081 wptr = readw(ofsAddr + TXwptr);
2082 mask = readw(ofsAddr + TX_mask);
2083 return (wptr - rptr) & mask;
2086 static int MoxaPortTxFree(struct moxa_port *port)
2088 void __iomem *ofsAddr = port->tableAddr;
2089 u16 rptr, wptr, mask;
2091 rptr = readw(ofsAddr + TXrptr);
2092 wptr = readw(ofsAddr + TXwptr);
2093 mask = readw(ofsAddr + TX_mask);
2094 return mask - ((wptr - rptr) & mask);
2097 static int MoxaPortRxQueue(struct moxa_port *port)
2099 void __iomem *ofsAddr = port->tableAddr;
2100 u16 rptr, wptr, mask;
2102 rptr = readw(ofsAddr + RXrptr);
2103 wptr = readw(ofsAddr + RXwptr);
2104 mask = readw(ofsAddr + RX_mask);
2105 return (wptr - rptr) & mask;
2108 static void MoxaPortTxDisable(struct moxa_port *port)
2110 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2113 static void MoxaPortTxEnable(struct moxa_port *port)
2115 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2118 static int moxa_get_serial_info(struct moxa_port *info,
2119 struct serial_struct __user *retinfo)
2121 struct serial_struct tmp = {
2123 .line = info->port.tty->index,
2124 .flags = info->port.flags,
2125 .baud_base = 921600,
2126 .close_delay = info->port.close_delay
2128 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2132 static int moxa_set_serial_info(struct moxa_port *info,
2133 struct serial_struct __user *new_info)
2135 struct serial_struct new_serial;
2137 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2140 if (new_serial.irq != 0 || new_serial.port != 0 ||
2141 new_serial.custom_divisor != 0 ||
2142 new_serial.baud_base != 921600)
2145 if (!capable(CAP_SYS_ADMIN)) {
2146 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2147 (info->port.flags & ~ASYNC_USR_MASK)))
2150 info->port.close_delay = new_serial.close_delay * HZ / 100;
2152 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2153 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
2155 MoxaSetFifo(info, new_serial.type == PORT_16550A);
2157 info->type = new_serial.type;
2163 /*****************************************************************************
2164 * Static local functions: *
2165 *****************************************************************************/
2167 static void MoxaSetFifo(struct moxa_port *port, int enable)
2169 void __iomem *ofsAddr = port->tableAddr;
2172 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2173 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2175 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2176 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);