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;
141 u8 DCDState; /* Protected by the port lock */
148 int rxcnt[MAX_PORTS];
149 int txcnt[MAX_PORTS];
157 #define SERIAL_DO_RESTART
159 #define WAKEUP_CHARS 256
161 static int ttymajor = MOXAMAJOR;
162 static struct mon_str moxaLog;
163 static unsigned int moxaFuncTout = HZ / 2;
164 static unsigned int moxaLowWaterChk;
165 static DEFINE_MUTEX(moxa_openlock);
166 /* Variables for insmod */
168 static unsigned long baseaddr[MAX_BOARDS];
169 static unsigned int type[MAX_BOARDS];
170 static unsigned int numports[MAX_BOARDS];
173 MODULE_AUTHOR("William Chen");
174 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
175 MODULE_LICENSE("GPL");
177 module_param_array(type, uint, NULL, 0);
178 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
179 module_param_array(baseaddr, ulong, NULL, 0);
180 MODULE_PARM_DESC(baseaddr, "base address");
181 module_param_array(numports, uint, NULL, 0);
182 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
184 module_param(ttymajor, int, 0);
189 static int moxa_open(struct tty_struct *, struct file *);
190 static void moxa_close(struct tty_struct *, struct file *);
191 static int moxa_write(struct tty_struct *, const unsigned char *, int);
192 static int moxa_write_room(struct tty_struct *);
193 static void moxa_flush_buffer(struct tty_struct *);
194 static int moxa_chars_in_buffer(struct tty_struct *);
195 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
196 static void moxa_stop(struct tty_struct *);
197 static void moxa_start(struct tty_struct *);
198 static void moxa_hangup(struct tty_struct *);
199 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
200 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
201 unsigned int set, unsigned int clear);
202 static void moxa_poll(unsigned long);
203 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
204 static void moxa_shutdown(struct tty_port *);
205 static int moxa_carrier_raised(struct tty_port *);
206 static void moxa_dtr_rts(struct tty_port *, int);
208 * moxa board interface functions:
210 static void MoxaPortEnable(struct moxa_port *);
211 static void MoxaPortDisable(struct moxa_port *);
212 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
213 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
214 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
215 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
216 static int MoxaPortLineStatus(struct moxa_port *);
217 static void MoxaPortFlushData(struct moxa_port *, int);
218 static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
219 static int MoxaPortReadData(struct moxa_port *);
220 static int MoxaPortTxQueue(struct moxa_port *);
221 static int MoxaPortRxQueue(struct moxa_port *);
222 static int MoxaPortTxFree(struct moxa_port *);
223 static void MoxaPortTxDisable(struct moxa_port *);
224 static void MoxaPortTxEnable(struct moxa_port *);
225 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
226 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
227 static void MoxaSetFifo(struct moxa_port *port, int enable);
233 static DEFINE_SPINLOCK(moxafunc_lock);
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)
249 spin_lock_irqsave(&moxafunc_lock, flags);
250 writew(arg, ofsAddr + FuncArg);
251 writew(cmd, ofsAddr + FuncCode);
252 moxa_wait_finish(ofsAddr);
253 spin_unlock_irqrestore(&moxafunc_lock, flags);
256 static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
260 spin_lock_irqsave(&moxafunc_lock, flags);
261 writew(arg, ofsAddr + FuncArg);
262 writew(cmd, ofsAddr + FuncCode);
263 moxa_wait_finish(ofsAddr);
264 ret = readw(ofsAddr + FuncArg);
265 spin_unlock_irqrestore(&moxafunc_lock, flags);
269 static void moxa_low_water_check(void __iomem *ofsAddr)
271 u16 rptr, wptr, mask, len;
273 if (readb(ofsAddr + FlagStat) & Xoff_state) {
274 rptr = readw(ofsAddr + RXrptr);
275 wptr = readw(ofsAddr + RXwptr);
276 mask = readw(ofsAddr + RX_mask);
277 len = (wptr - rptr) & mask;
278 if (len <= Low_water)
279 moxafunc(ofsAddr, FC_SendXon, 0);
287 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
288 unsigned int cmd, unsigned long arg)
290 struct moxa_port *ch = tty->driver_data;
291 void __user *argp = (void __user *)arg;
294 if (tty->index == MAX_PORTS) {
295 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
296 cmd != MOXA_GETMSTATUS)
302 case MOXA_GETDATACOUNT:
303 moxaLog.tick = jiffies;
304 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
307 case MOXA_FLUSH_QUEUE:
308 MoxaPortFlushData(ch, arg);
310 case MOXA_GET_IOQUEUE: {
311 struct moxaq_str __user *argm = argp;
312 struct moxaq_str tmp;
316 mutex_lock(&moxa_openlock);
317 for (i = 0; i < MAX_BOARDS; i++) {
318 p = moxa_boards[i].ports;
319 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
320 memset(&tmp, 0, sizeof(tmp));
321 if (moxa_boards[i].ready) {
322 tmp.inq = MoxaPortRxQueue(p);
323 tmp.outq = MoxaPortTxQueue(p);
325 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
326 mutex_unlock(&moxa_openlock);
331 mutex_unlock(&moxa_openlock);
333 } case MOXA_GET_OQUEUE:
334 status = MoxaPortTxQueue(ch);
335 ret = put_user(status, (unsigned long __user *)argp);
337 case MOXA_GET_IQUEUE:
338 status = MoxaPortRxQueue(ch);
339 ret = put_user(status, (unsigned long __user *)argp);
341 case MOXA_GETMSTATUS: {
342 struct mxser_mstatus __user *argm = argp;
343 struct mxser_mstatus tmp;
347 mutex_lock(&moxa_openlock);
348 for (i = 0; i < MAX_BOARDS; i++) {
349 p = moxa_boards[i].ports;
350 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
351 struct tty_struct *ttyp;
352 memset(&tmp, 0, sizeof(tmp));
353 if (!moxa_boards[i].ready)
356 status = MoxaPortLineStatus(p);
364 ttyp = tty_port_tty_get(&p->port);
365 if (!ttyp || !ttyp->termios)
366 tmp.cflag = p->cflag;
368 tmp.cflag = ttyp->termios->c_cflag;
371 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
372 mutex_unlock(&moxa_openlock);
377 mutex_unlock(&moxa_openlock);
381 mutex_lock(&ch->port.mutex);
382 ret = moxa_get_serial_info(ch, argp);
383 mutex_unlock(&ch->port.mutex);
386 mutex_lock(&ch->port.mutex);
387 ret = moxa_set_serial_info(ch, argp);
388 mutex_unlock(&ch->port.mutex);
396 static int moxa_break_ctl(struct tty_struct *tty, int state)
398 struct moxa_port *port = tty->driver_data;
400 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
405 static const struct tty_operations moxa_ops = {
409 .write_room = moxa_write_room,
410 .flush_buffer = moxa_flush_buffer,
411 .chars_in_buffer = moxa_chars_in_buffer,
413 .set_termios = moxa_set_termios,
416 .hangup = moxa_hangup,
417 .break_ctl = moxa_break_ctl,
418 .tiocmget = moxa_tiocmget,
419 .tiocmset = moxa_tiocmset,
422 static const struct tty_port_operations moxa_port_ops = {
423 .carrier_raised = moxa_carrier_raised,
424 .dtr_rts = moxa_dtr_rts,
425 .shutdown = moxa_shutdown,
428 static struct tty_driver *moxaDriver;
429 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
430 static DEFINE_SPINLOCK(moxa_lock);
436 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
438 switch (brd->boardType) {
439 case MOXA_BOARD_C218_ISA:
440 case MOXA_BOARD_C218_PCI:
444 case MOXA_BOARD_CP204J:
458 static int moxa_check_fw(const void *ptr)
460 const __le16 *lptr = ptr;
462 if (*lptr != cpu_to_le16(0x7980))
468 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
471 void __iomem *baseAddr = brd->basemem;
474 writeb(HW_reset, baseAddr + Control_reg); /* reset */
476 memset_io(baseAddr, 0, 4096);
477 memcpy_toio(baseAddr, buf, len); /* download BIOS */
478 writeb(0, baseAddr + Control_reg); /* restart */
482 switch (brd->boardType) {
483 case MOXA_BOARD_C218_ISA:
484 case MOXA_BOARD_C218_PCI:
485 tmp = readw(baseAddr + C218_key);
486 if (tmp != C218_KeyCode)
489 case MOXA_BOARD_CP204J:
490 tmp = readw(baseAddr + C218_key);
491 if (tmp != CP204J_KeyCode)
495 tmp = readw(baseAddr + C320_key);
496 if (tmp != C320_KeyCode)
498 tmp = readw(baseAddr + C320_status);
499 if (tmp != STS_init) {
500 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
501 "module not found\n");
509 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
513 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
516 void __iomem *baseAddr = brd->basemem;
519 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
523 writew(len - 7168 - 2, baseAddr + C320bapi_len);
524 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
525 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
526 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
527 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
532 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
535 void __iomem *baseAddr = brd->basemem;
536 const __le16 *uptr = ptr;
537 size_t wlen, len2, j;
538 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
539 unsigned int i, retry;
542 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
545 switch (brd->boardType) {
546 case MOXA_BOARD_CP204J:
547 case MOXA_BOARD_C218_ISA:
548 case MOXA_BOARD_C218_PCI:
550 loadbuf = C218_LoadBuf;
551 loadlen = C218DLoad_len;
552 checksum = C218check_sum;
553 checksum_ok = C218chksum_ok;
557 keycode = C320_KeyCode;
558 loadbuf = C320_LoadBuf;
559 loadlen = C320DLoad_len;
560 checksum = C320check_sum;
561 checksum_ok = C320chksum_ok;
567 for (i = 0; i < wlen; i++)
568 usum += le16_to_cpu(uptr[i]);
574 len2 = (wlen > 2048) ? 2048 : wlen;
576 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
579 writew(len2, baseAddr + loadlen);
580 writew(0, baseAddr + key);
581 for (i = 0; i < 100; i++) {
582 if (readw(baseAddr + key) == keycode)
586 if (readw(baseAddr + key) != keycode)
589 writew(0, baseAddr + loadlen);
590 writew(usum, baseAddr + checksum);
591 writew(0, baseAddr + key);
592 for (i = 0; i < 100; i++) {
593 if (readw(baseAddr + key) == keycode)
598 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
599 if (readb(baseAddr + checksum_ok) != 1)
602 writew(0, baseAddr + key);
603 for (i = 0; i < 600; i++) {
604 if (readw(baseAddr + Magic_no) == Magic_code)
608 if (readw(baseAddr + Magic_no) != Magic_code)
611 if (MOXA_IS_320(brd)) {
612 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
613 writew(0x3800, baseAddr + TMS320_PORT1);
614 writew(0x3900, baseAddr + TMS320_PORT2);
615 writew(28499, baseAddr + TMS320_CLOCK);
617 writew(0x3200, baseAddr + TMS320_PORT1);
618 writew(0x3400, baseAddr + TMS320_PORT2);
619 writew(19999, baseAddr + TMS320_CLOCK);
622 writew(1, baseAddr + Disable_IRQ);
623 writew(0, baseAddr + Magic_no);
624 for (i = 0; i < 500; i++) {
625 if (readw(baseAddr + Magic_no) == Magic_code)
629 if (readw(baseAddr + Magic_no) != Magic_code)
632 if (MOXA_IS_320(brd)) {
633 j = readw(baseAddr + Module_cnt);
636 brd->numPorts = j * 8;
637 writew(j, baseAddr + Module_no);
638 writew(0, baseAddr + Magic_no);
639 for (i = 0; i < 600; i++) {
640 if (readw(baseAddr + Magic_no) == Magic_code)
644 if (readw(baseAddr + Magic_no) != Magic_code)
647 brd->intNdx = baseAddr + IRQindex;
648 brd->intPend = baseAddr + IRQpending;
649 brd->intTable = baseAddr + IRQtable;
654 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
657 void __iomem *ofsAddr, *baseAddr = brd->basemem;
658 struct moxa_port *port;
662 printk(KERN_ERR "MOXA: bios length is not even\n");
666 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
670 switch (brd->boardType) {
671 case MOXA_BOARD_C218_ISA:
672 case MOXA_BOARD_C218_PCI:
673 case MOXA_BOARD_CP204J:
675 for (i = 0; i < brd->numPorts; i++, port++) {
678 port->tableAddr = baseAddr + Extern_table +
680 ofsAddr = port->tableAddr;
681 writew(C218rx_mask, ofsAddr + RX_mask);
682 writew(C218tx_mask, ofsAddr + TX_mask);
683 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
684 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
686 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
687 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
693 for (i = 0; i < brd->numPorts; i++, port++) {
696 port->tableAddr = baseAddr + Extern_table +
698 ofsAddr = port->tableAddr;
699 switch (brd->numPorts) {
701 writew(C320p8rx_mask, ofsAddr + RX_mask);
702 writew(C320p8tx_mask, ofsAddr + TX_mask);
703 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
704 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
705 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
706 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
710 writew(C320p16rx_mask, ofsAddr + RX_mask);
711 writew(C320p16tx_mask, ofsAddr + TX_mask);
712 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
713 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
714 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
715 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
719 writew(C320p24rx_mask, ofsAddr + RX_mask);
720 writew(C320p24tx_mask, ofsAddr + TX_mask);
721 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
722 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
723 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
724 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
727 writew(C320p32rx_mask, ofsAddr + RX_mask);
728 writew(C320p32tx_mask, ofsAddr + TX_mask);
729 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
730 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
731 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
732 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
733 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
742 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
744 const void *ptr = fw->data;
748 unsigned int a, lenp, lencnt;
751 __le32 magic; /* 0x34303430 */
753 u8 type; /* UNIX = 3 */
754 u8 model; /* C218T=1, C320T=2, CP204=3 */
759 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
761 if (fw->size < MOXA_FW_HDRLEN) {
762 strcpy(rsn, "too short (even header won't fit)");
765 if (hdr->magic != cpu_to_le32(0x30343034)) {
766 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
769 if (hdr->type != 3) {
770 sprintf(rsn, "not for linux, type is %u", hdr->type);
773 if (moxa_check_fw_model(brd, hdr->model)) {
774 sprintf(rsn, "not for this card, model is %u", hdr->model);
778 len = MOXA_FW_HDRLEN;
779 lencnt = hdr->model == 2 ? 5 : 3;
780 for (a = 0; a < ARRAY_SIZE(lens); a++) {
781 lens[a] = le16_to_cpu(hdr->len[a]);
782 if (lens[a] && len + lens[a] <= fw->size &&
783 moxa_check_fw(&fw->data[len]))
784 printk(KERN_WARNING "MOXA firmware: unexpected input "
785 "at offset %u, but going on\n", (u32)len);
786 if (!lens[a] && a < lencnt) {
787 sprintf(rsn, "too few entries in fw file");
793 if (len != fw->size) {
794 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
799 ptr += MOXA_FW_HDRLEN;
802 strcpy(rsn, "read above");
804 ret = moxa_load_bios(brd, ptr, lens[lenp]);
808 /* we skip the tty section (lens[1]), since we don't need it */
809 ptr += lens[lenp] + lens[lenp + 1];
810 lenp += 2; /* comm */
812 if (hdr->model == 2) {
813 ret = moxa_load_320b(brd, ptr, lens[lenp]);
816 /* skip another tty */
817 ptr += lens[lenp] + lens[lenp + 1];
821 ret = moxa_load_code(brd, ptr, lens[lenp]);
827 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
831 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
833 const struct firmware *fw;
839 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
841 if (brd->ports == NULL) {
842 printk(KERN_ERR "cannot allocate memory for ports\n");
847 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
848 tty_port_init(&p->port);
849 p->port.ops = &moxa_port_ops;
850 p->type = PORT_16550A;
851 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
854 switch (brd->boardType) {
855 case MOXA_BOARD_C218_ISA:
856 case MOXA_BOARD_C218_PCI:
857 file = "c218tunx.cod";
859 case MOXA_BOARD_CP204J:
860 file = "cp204unx.cod";
863 file = "c320tunx.cod";
867 ret = request_firmware(&fw, file, dev);
869 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
870 "you've placed '%s' file into your firmware "
871 "loader directory (e.g. /lib/firmware)\n",
876 ret = moxa_load_fw(brd, fw);
878 release_firmware(fw);
883 spin_lock_bh(&moxa_lock);
885 if (!timer_pending(&moxaTimer))
886 mod_timer(&moxaTimer, jiffies + HZ / 50);
887 spin_unlock_bh(&moxa_lock);
896 static void moxa_board_deinit(struct moxa_board_conf *brd)
898 unsigned int a, opened;
900 mutex_lock(&moxa_openlock);
901 spin_lock_bh(&moxa_lock);
903 spin_unlock_bh(&moxa_lock);
905 /* pci hot-un-plug support */
906 for (a = 0; a < brd->numPorts; a++)
907 if (brd->ports[a].port.flags & ASYNC_INITIALIZED) {
908 struct tty_struct *tty = tty_port_tty_get(
909 &brd->ports[a].port);
917 for (a = 0; a < brd->numPorts; a++)
918 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
920 mutex_unlock(&moxa_openlock);
924 mutex_lock(&moxa_openlock);
927 iounmap(brd->basemem);
933 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
934 const struct pci_device_id *ent)
936 struct moxa_board_conf *board;
938 int board_type = ent->driver_data;
941 retval = pci_enable_device(pdev);
943 dev_err(&pdev->dev, "can't enable pci device\n");
947 for (i = 0; i < MAX_BOARDS; i++)
948 if (moxa_boards[i].basemem == NULL)
952 if (i >= MAX_BOARDS) {
953 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
954 "found. Board is ignored.\n", MAX_BOARDS);
958 board = &moxa_boards[i];
960 retval = pci_request_region(pdev, 2, "moxa-base");
962 dev_err(&pdev->dev, "can't request pci region 2\n");
966 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
967 if (board->basemem == NULL) {
968 dev_err(&pdev->dev, "can't remap io space 2\n");
972 board->boardType = board_type;
973 switch (board_type) {
974 case MOXA_BOARD_C218_ISA:
975 case MOXA_BOARD_C218_PCI:
979 case MOXA_BOARD_CP204J:
986 board->busType = MOXA_BUS_TYPE_PCI;
988 retval = moxa_init_board(board, &pdev->dev);
992 pci_set_drvdata(pdev, board);
994 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
995 moxa_brdname[board_type - 1], board->numPorts);
999 iounmap(board->basemem);
1000 board->basemem = NULL;
1002 pci_release_region(pdev, 2);
1007 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
1009 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
1011 moxa_board_deinit(brd);
1013 pci_release_region(pdev, 2);
1016 static struct pci_driver moxa_pci_driver = {
1018 .id_table = moxa_pcibrds,
1019 .probe = moxa_pci_probe,
1020 .remove = __devexit_p(moxa_pci_remove)
1022 #endif /* CONFIG_PCI */
1024 static int __init moxa_init(void)
1026 unsigned int isabrds = 0;
1029 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1031 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1035 moxaDriver->owner = THIS_MODULE;
1036 moxaDriver->name = "ttyMX";
1037 moxaDriver->major = ttymajor;
1038 moxaDriver->minor_start = 0;
1039 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1040 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1041 moxaDriver->init_termios = tty_std_termios;
1042 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1043 moxaDriver->init_termios.c_ispeed = 9600;
1044 moxaDriver->init_termios.c_ospeed = 9600;
1045 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1046 tty_set_operations(moxaDriver, &moxa_ops);
1048 if (tty_register_driver(moxaDriver)) {
1049 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1050 put_tty_driver(moxaDriver);
1054 /* Find the boards defined from module args. */
1057 struct moxa_board_conf *brd = moxa_boards;
1059 for (i = 0; i < MAX_BOARDS; i++) {
1062 if (type[i] == MOXA_BOARD_C218_ISA ||
1063 type[i] == MOXA_BOARD_C320_ISA) {
1064 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1065 isabrds + 1, moxa_brdname[type[i] - 1],
1067 brd->boardType = type[i];
1068 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1070 brd->busType = MOXA_BUS_TYPE_ISA;
1071 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
1072 if (!brd->basemem) {
1073 printk(KERN_ERR "MOXA: can't remap %lx\n",
1077 if (moxa_init_board(brd, NULL)) {
1078 iounmap(brd->basemem);
1079 brd->basemem = NULL;
1083 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1084 "ready (%u ports, firmware loaded)\n",
1085 baseaddr[i], brd->numPorts);
1095 retval = pci_register_driver(&moxa_pci_driver);
1097 printk(KERN_ERR "Can't register MOXA pci driver!\n");
1106 static void __exit moxa_exit(void)
1111 pci_unregister_driver(&moxa_pci_driver);
1114 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1115 if (moxa_boards[i].ready)
1116 moxa_board_deinit(&moxa_boards[i]);
1118 del_timer_sync(&moxaTimer);
1120 if (tty_unregister_driver(moxaDriver))
1121 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1123 put_tty_driver(moxaDriver);
1126 module_init(moxa_init);
1127 module_exit(moxa_exit);
1129 static void moxa_shutdown(struct tty_port *port)
1131 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1132 MoxaPortDisable(ch);
1133 MoxaPortFlushData(ch, 2);
1134 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1137 static int moxa_carrier_raised(struct tty_port *port)
1139 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1142 spin_lock_irq(&port->lock);
1144 spin_unlock_irq(&port->lock);
1148 static void moxa_dtr_rts(struct tty_port *port, int onoff)
1150 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1151 MoxaPortLineCtrl(ch, onoff, onoff);
1155 static int moxa_open(struct tty_struct *tty, struct file *filp)
1157 struct moxa_board_conf *brd;
1158 struct moxa_port *ch;
1163 if (port == MAX_PORTS) {
1164 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1166 if (mutex_lock_interruptible(&moxa_openlock))
1167 return -ERESTARTSYS;
1168 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1170 mutex_unlock(&moxa_openlock);
1174 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1175 mutex_unlock(&moxa_openlock);
1179 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1181 tty->driver_data = ch;
1182 tty_port_tty_set(&ch->port, tty);
1183 mutex_lock(&ch->port.mutex);
1184 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
1185 ch->statusflags = 0;
1186 moxa_set_tty_param(tty, tty->termios);
1187 MoxaPortLineCtrl(ch, 1, 1);
1189 MoxaSetFifo(ch, ch->type == PORT_16550A);
1190 ch->port.flags |= ASYNC_INITIALIZED;
1192 mutex_unlock(&ch->port.mutex);
1193 mutex_unlock(&moxa_openlock);
1195 retval = tty_port_block_til_ready(&ch->port, tty, filp);
1197 set_bit(ASYNCB_NORMAL_ACTIVE, &ch->port.flags);
1201 static void moxa_close(struct tty_struct *tty, struct file *filp)
1203 struct moxa_port *ch = tty->driver_data;
1204 ch->cflag = tty->termios->c_cflag;
1205 tty_port_close(&ch->port, tty, filp);
1208 static int moxa_write(struct tty_struct *tty,
1209 const unsigned char *buf, int count)
1211 struct moxa_port *ch = tty->driver_data;
1217 spin_lock_bh(&moxa_lock);
1218 len = MoxaPortWriteData(tty, buf, count);
1219 spin_unlock_bh(&moxa_lock);
1221 set_bit(LOWWAIT, &ch->statusflags);
1225 static int moxa_write_room(struct tty_struct *tty)
1227 struct moxa_port *ch;
1231 ch = tty->driver_data;
1234 return MoxaPortTxFree(ch);
1237 static void moxa_flush_buffer(struct tty_struct *tty)
1239 struct moxa_port *ch = tty->driver_data;
1243 MoxaPortFlushData(ch, 1);
1247 static int moxa_chars_in_buffer(struct tty_struct *tty)
1249 struct moxa_port *ch = tty->driver_data;
1252 chars = MoxaPortTxQueue(ch);
1255 * Make it possible to wakeup anything waiting for output
1256 * in tty_ioctl.c, etc.
1258 set_bit(EMPTYWAIT, &ch->statusflags);
1262 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1264 struct moxa_port *ch = tty->driver_data;
1265 int flag = 0, dtr, rts;
1267 MoxaPortGetLineOut(ch, &dtr, &rts);
1272 dtr = MoxaPortLineStatus(ch);
1282 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1283 unsigned int set, unsigned int clear)
1285 struct moxa_port *ch;
1290 mutex_lock(&moxa_openlock);
1291 ch = tty->driver_data;
1293 mutex_unlock(&moxa_openlock);
1297 MoxaPortGetLineOut(ch, &dtr, &rts);
1298 if (set & TIOCM_RTS)
1300 if (set & TIOCM_DTR)
1302 if (clear & TIOCM_RTS)
1304 if (clear & TIOCM_DTR)
1306 MoxaPortLineCtrl(ch, dtr, rts);
1307 mutex_unlock(&moxa_openlock);
1311 static void moxa_set_termios(struct tty_struct *tty,
1312 struct ktermios *old_termios)
1314 struct moxa_port *ch = tty->driver_data;
1318 moxa_set_tty_param(tty, old_termios);
1319 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1320 wake_up_interruptible(&ch->port.open_wait);
1323 static void moxa_stop(struct tty_struct *tty)
1325 struct moxa_port *ch = tty->driver_data;
1329 MoxaPortTxDisable(ch);
1330 set_bit(TXSTOPPED, &ch->statusflags);
1334 static void moxa_start(struct tty_struct *tty)
1336 struct moxa_port *ch = tty->driver_data;
1341 if (!(ch->statusflags & TXSTOPPED))
1344 MoxaPortTxEnable(ch);
1345 clear_bit(TXSTOPPED, &ch->statusflags);
1348 static void moxa_hangup(struct tty_struct *tty)
1350 struct moxa_port *ch = tty->driver_data;
1351 tty_port_hangup(&ch->port);
1354 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1356 struct tty_struct *tty;
1357 unsigned long flags;
1360 spin_lock_irqsave(&p->port.lock, flags);
1361 if (dcd != p->DCDState) {
1363 spin_unlock_irqrestore(&p->port.lock, flags);
1364 tty = tty_port_tty_get(&p->port);
1365 if (tty && C_CLOCAL(tty) && !dcd)
1370 spin_unlock_irqrestore(&p->port.lock, flags);
1373 static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1376 struct tty_struct *tty = tty_port_tty_get(&p->port);
1377 void __iomem *ofsAddr;
1378 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
1382 if (test_bit(EMPTYWAIT, &p->statusflags) &&
1383 MoxaPortTxQueue(p) == 0) {
1384 clear_bit(EMPTYWAIT, &p->statusflags);
1387 if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
1388 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1389 clear_bit(LOWWAIT, &p->statusflags);
1393 if (inited && !test_bit(TTY_THROTTLED, &tty->flags) &&
1394 MoxaPortRxQueue(p) > 0) { /* RX */
1395 MoxaPortReadData(p);
1396 tty_schedule_flip(tty);
1399 clear_bit(EMPTYWAIT, &p->statusflags);
1400 MoxaPortFlushData(p, 0); /* flush RX */
1403 if (!handle) /* nothing else to do */
1406 intr = readw(ip); /* port irq status */
1410 writew(0, ip); /* ACK port */
1411 ofsAddr = p->tableAddr;
1412 if (intr & IntrTx) /* disable tx intr */
1413 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1414 ofsAddr + HostStat);
1419 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1420 tty_insert_flip_char(tty, 0, TTY_BREAK);
1421 tty_schedule_flip(tty);
1424 if (intr & IntrLine)
1425 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1432 static void moxa_poll(unsigned long ignored)
1434 struct moxa_board_conf *brd;
1436 unsigned int card, port, served = 0;
1438 spin_lock(&moxa_lock);
1439 for (card = 0; card < MAX_BOARDS; card++) {
1440 brd = &moxa_boards[card];
1447 if (readb(brd->intPend) == 0xff)
1448 ip = brd->intTable + readb(brd->intNdx);
1450 for (port = 0; port < brd->numPorts; port++)
1451 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1454 writeb(0, brd->intPend); /* ACK */
1456 if (moxaLowWaterChk) {
1457 struct moxa_port *p = brd->ports;
1458 for (port = 0; port < brd->numPorts; port++, p++)
1459 if (p->lowChkFlag) {
1461 moxa_low_water_check(p->tableAddr);
1465 moxaLowWaterChk = 0;
1468 mod_timer(&moxaTimer, jiffies + HZ / 50);
1469 spin_unlock(&moxa_lock);
1472 /******************************************************************************/
1474 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1476 register struct ktermios *ts = tty->termios;
1477 struct moxa_port *ch = tty->driver_data;
1478 int rts, cts, txflow, rxflow, xany, baud;
1480 rts = cts = txflow = rxflow = xany = 0;
1481 if (ts->c_cflag & CRTSCTS)
1483 if (ts->c_iflag & IXON)
1485 if (ts->c_iflag & IXOFF)
1487 if (ts->c_iflag & IXANY)
1490 /* Clear the features we don't support */
1491 ts->c_cflag &= ~CMSPAR;
1492 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1493 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1495 baud = tty_termios_baud_rate(old_termios);
1496 /* Not put the baud rate into the termios data */
1497 tty_encode_baud_rate(tty, baud, baud);
1500 /*****************************************************************************
1501 * Driver level functions: *
1502 *****************************************************************************/
1504 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1506 void __iomem *ofsAddr;
1507 if (mode < 0 || mode > 2)
1509 ofsAddr = port->tableAddr;
1510 moxafunc(ofsAddr, FC_FlushQueue, mode);
1512 port->lowChkFlag = 0;
1513 moxa_low_water_check(ofsAddr);
1518 * Moxa Port Number Description:
1520 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1521 * the port number using in MOXA driver functions will be 0 to 31 for
1522 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1523 * to 127 for fourth. For example, if you setup three MOXA boards,
1524 * first board is C218, second board is C320-16 and third board is
1525 * C320-32. The port number of first board (C218 - 8 ports) is from
1526 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1527 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1528 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1529 * 127 will be invalid.
1532 * Moxa Functions Description:
1534 * Function 1: Driver initialization routine, this routine must be
1535 * called when initialized driver.
1537 * void MoxaDriverInit();
1540 * Function 2: Moxa driver private IOCTL command processing.
1542 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1544 * unsigned int cmd : IOCTL command
1545 * unsigned long arg : IOCTL argument
1546 * int port : port number (0 - 127)
1553 * Function 6: Enable this port to start Tx/Rx data.
1555 * void MoxaPortEnable(int port);
1556 * int port : port number (0 - 127)
1559 * Function 7: Disable this port
1561 * void MoxaPortDisable(int port);
1562 * int port : port number (0 - 127)
1565 * Function 10: Setting baud rate of this port.
1567 * speed_t MoxaPortSetBaud(int port, speed_t baud);
1568 * int port : port number (0 - 127)
1569 * long baud : baud rate (50 - 115200)
1571 * return: 0 : this port is invalid or baud < 50
1572 * 50 - 115200 : the real baud rate set to the port, if
1573 * the argument baud is large than maximun
1574 * available baud rate, the real setting
1575 * baud rate will be the maximun baud rate.
1578 * Function 12: Configure the port.
1580 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1581 * int port : port number (0 - 127)
1582 * struct ktermios * termio : termio structure pointer
1583 * speed_t baud : baud rate
1585 * return: -1 : this port is invalid or termio == NULL
1589 * Function 13: Get the DTR/RTS state of this port.
1591 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1592 * int port : port number (0 - 127)
1593 * int * dtrState : pointer to INT to receive the current DTR
1594 * state. (if NULL, this function will not
1595 * write to this address)
1596 * int * rtsState : pointer to INT to receive the current RTS
1597 * state. (if NULL, this function will not
1598 * write to this address)
1600 * return: -1 : this port is invalid
1604 * Function 14: Setting the DTR/RTS output state of this port.
1606 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1607 * int port : port number (0 - 127)
1608 * int dtrState : DTR output state (0: off, 1: on)
1609 * int rtsState : RTS output state (0: off, 1: on)
1612 * Function 15: Setting the flow control of this port.
1614 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1615 * int txFlow,int xany);
1616 * int port : port number (0 - 127)
1617 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1618 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1619 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1620 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1621 * int xany : S/W XANY flow control (0: no, 1: yes)
1624 * Function 16: Get ths line status of this port
1626 * int MoxaPortLineStatus(int port);
1627 * int port : port number (0 - 127)
1629 * return: Bit 0 - CTS state (0: off, 1: on)
1630 * Bit 1 - DSR state (0: off, 1: on)
1631 * Bit 2 - DCD state (0: off, 1: on)
1634 * Function 19: Flush the Rx/Tx buffer data of this port.
1636 * void MoxaPortFlushData(int port, int mode);
1637 * int port : port number (0 - 127)
1639 * 0 : flush the Rx buffer
1640 * 1 : flush the Tx buffer
1641 * 2 : flush the Rx and Tx buffer
1644 * Function 20: Write data.
1646 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1647 * int port : port number (0 - 127)
1648 * unsigned char * buffer : pointer to write data buffer.
1649 * int length : write data length
1651 * return: 0 - length : real write data length
1654 * Function 21: Read data.
1656 * int MoxaPortReadData(int port, struct tty_struct *tty);
1657 * int port : port number (0 - 127)
1658 * struct tty_struct *tty : tty for data
1660 * return: 0 - length : real read data length
1663 * Function 24: Get the Tx buffer current queued data bytes
1665 * int MoxaPortTxQueue(int port);
1666 * int port : port number (0 - 127)
1668 * return: .. : Tx buffer current queued data bytes
1671 * Function 25: Get the Tx buffer current free space
1673 * int MoxaPortTxFree(int port);
1674 * int port : port number (0 - 127)
1676 * return: .. : Tx buffer current free space
1679 * Function 26: Get the Rx buffer current queued data bytes
1681 * int MoxaPortRxQueue(int port);
1682 * int port : port number (0 - 127)
1684 * return: .. : Rx buffer current queued data bytes
1687 * Function 28: Disable port data transmission.
1689 * void MoxaPortTxDisable(int port);
1690 * int port : port number (0 - 127)
1693 * Function 29: Enable port data transmission.
1695 * void MoxaPortTxEnable(int port);
1696 * int port : port number (0 - 127)
1699 * Function 31: Get the received BREAK signal count and reset it.
1701 * int MoxaPortResetBrkCnt(int port);
1702 * int port : port number (0 - 127)
1704 * return: 0 - .. : BREAK signal count
1709 static void MoxaPortEnable(struct moxa_port *port)
1711 void __iomem *ofsAddr;
1714 ofsAddr = port->tableAddr;
1715 writew(lowwater, ofsAddr + Low_water);
1716 if (MOXA_IS_320(port->board))
1717 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1719 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1720 ofsAddr + HostStat);
1722 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1723 moxafunc(ofsAddr, FC_FlushQueue, 2);
1725 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1726 MoxaPortLineStatus(port);
1729 static void MoxaPortDisable(struct moxa_port *port)
1731 void __iomem *ofsAddr = port->tableAddr;
1733 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1734 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1735 writew(0, ofsAddr + HostStat);
1736 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1739 static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
1741 void __iomem *ofsAddr = port->tableAddr;
1742 unsigned int clock, val;
1745 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1752 moxafunc(ofsAddr, FC_SetBaud, val);
1757 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1760 void __iomem *ofsAddr;
1764 ofsAddr = port->tableAddr;
1765 cflag = termio->c_cflag; /* termio->c_cflag */
1767 mode = termio->c_cflag & CSIZE;
1770 else if (mode == CS6)
1772 else if (mode == CS7)
1774 else if (mode == CS8)
1777 if (termio->c_cflag & CSTOPB) {
1785 if (termio->c_cflag & PARENB) {
1786 if (termio->c_cflag & PARODD)
1793 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
1795 if (MOXA_IS_320(port->board) && baud >= 921600)
1798 baud = MoxaPortSetBaud(port, baud);
1800 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1801 spin_lock_irq(&moxafunc_lock);
1802 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1803 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1804 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1805 moxa_wait_finish(ofsAddr);
1806 spin_unlock_irq(&moxafunc_lock);
1812 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1816 *dtrState = !!(port->lineCtrl & DTR_ON);
1818 *rtsState = !!(port->lineCtrl & RTS_ON);
1823 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1831 port->lineCtrl = mode;
1832 moxafunc(port->tableAddr, FC_LineControl, mode);
1835 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1836 int txflow, int rxflow, int txany)
1841 mode |= RTS_FlowCtl;
1843 mode |= CTS_FlowCtl;
1850 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
1853 static int MoxaPortLineStatus(struct moxa_port *port)
1855 void __iomem *ofsAddr;
1858 ofsAddr = port->tableAddr;
1859 if (MOXA_IS_320(port->board))
1860 val = moxafuncret(ofsAddr, FC_LineStatus, 0);
1862 val = readw(ofsAddr + FlagStat) >> 4;
1866 moxa_new_dcdstate(port, val & 8);
1871 static int MoxaPortWriteData(struct tty_struct *tty,
1872 const unsigned char *buffer, int len)
1874 struct moxa_port *port = tty->driver_data;
1875 void __iomem *baseAddr, *ofsAddr, *ofs;
1876 unsigned int c, total;
1877 u16 head, tail, tx_mask, spage, epage;
1878 u16 pageno, pageofs, bufhead;
1880 ofsAddr = port->tableAddr;
1881 baseAddr = port->board->basemem;
1882 tx_mask = readw(ofsAddr + TX_mask);
1883 spage = readw(ofsAddr + Page_txb);
1884 epage = readw(ofsAddr + EndPage_txb);
1885 tail = readw(ofsAddr + TXwptr);
1886 head = readw(ofsAddr + TXrptr);
1887 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
1890 moxaLog.txcnt[port->port.tty->index] += c;
1892 if (spage == epage) {
1893 bufhead = readw(ofsAddr + Ofs_txb);
1894 writew(spage, baseAddr + Control_reg);
1897 len = head - tail - 1;
1899 len = tx_mask + 1 - tail;
1900 len = (c > len) ? len : c;
1901 ofs = baseAddr + DynPage_addr + bufhead + tail;
1902 memcpy_toio(ofs, buffer, len);
1904 tail = (tail + len) & tx_mask;
1908 pageno = spage + (tail >> 13);
1909 pageofs = tail & Page_mask;
1911 len = Page_size - pageofs;
1914 writeb(pageno, baseAddr + Control_reg);
1915 ofs = baseAddr + DynPage_addr + pageofs;
1916 memcpy_toio(ofs, buffer, len);
1918 if (++pageno == epage)
1923 tail = (tail + total) & tx_mask;
1925 writew(tail, ofsAddr + TXwptr);
1926 writeb(1, ofsAddr + CD180TXirq); /* start to send */
1930 static int MoxaPortReadData(struct moxa_port *port)
1932 struct tty_struct *tty = port->port.tty;
1934 void __iomem *baseAddr, *ofsAddr, *ofs;
1935 unsigned int count, len, total;
1936 u16 tail, rx_mask, spage, epage;
1937 u16 pageno, pageofs, bufhead, head;
1939 ofsAddr = port->tableAddr;
1940 baseAddr = port->board->basemem;
1941 head = readw(ofsAddr + RXrptr);
1942 tail = readw(ofsAddr + RXwptr);
1943 rx_mask = readw(ofsAddr + RX_mask);
1944 spage = readw(ofsAddr + Page_rxb);
1945 epage = readw(ofsAddr + EndPage_rxb);
1946 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
1951 moxaLog.rxcnt[tty->index] += total;
1952 if (spage == epage) {
1953 bufhead = readw(ofsAddr + Ofs_rxb);
1954 writew(spage, baseAddr + Control_reg);
1956 ofs = baseAddr + DynPage_addr + bufhead + head;
1957 len = (tail >= head) ? (tail - head) :
1958 (rx_mask + 1 - head);
1959 len = tty_prepare_flip_string(tty, &dst,
1961 memcpy_fromio(dst, ofs, len);
1962 head = (head + len) & rx_mask;
1966 pageno = spage + (head >> 13);
1967 pageofs = head & Page_mask;
1969 writew(pageno, baseAddr + Control_reg);
1970 ofs = baseAddr + DynPage_addr + pageofs;
1971 len = tty_prepare_flip_string(tty, &dst,
1972 min(Page_size - pageofs, count));
1973 memcpy_fromio(dst, ofs, len);
1976 pageofs = (pageofs + len) & Page_mask;
1977 if (pageofs == 0 && ++pageno == epage)
1980 head = (head + total) & rx_mask;
1982 writew(head, ofsAddr + RXrptr);
1983 if (readb(ofsAddr + FlagStat) & Xoff_state) {
1984 moxaLowWaterChk = 1;
1985 port->lowChkFlag = 1;
1991 static int MoxaPortTxQueue(struct moxa_port *port)
1993 void __iomem *ofsAddr = port->tableAddr;
1994 u16 rptr, wptr, mask;
1996 rptr = readw(ofsAddr + TXrptr);
1997 wptr = readw(ofsAddr + TXwptr);
1998 mask = readw(ofsAddr + TX_mask);
1999 return (wptr - rptr) & mask;
2002 static int MoxaPortTxFree(struct moxa_port *port)
2004 void __iomem *ofsAddr = port->tableAddr;
2005 u16 rptr, wptr, mask;
2007 rptr = readw(ofsAddr + TXrptr);
2008 wptr = readw(ofsAddr + TXwptr);
2009 mask = readw(ofsAddr + TX_mask);
2010 return mask - ((wptr - rptr) & mask);
2013 static int MoxaPortRxQueue(struct moxa_port *port)
2015 void __iomem *ofsAddr = port->tableAddr;
2016 u16 rptr, wptr, mask;
2018 rptr = readw(ofsAddr + RXrptr);
2019 wptr = readw(ofsAddr + RXwptr);
2020 mask = readw(ofsAddr + RX_mask);
2021 return (wptr - rptr) & mask;
2024 static void MoxaPortTxDisable(struct moxa_port *port)
2026 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2029 static void MoxaPortTxEnable(struct moxa_port *port)
2031 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2034 static int moxa_get_serial_info(struct moxa_port *info,
2035 struct serial_struct __user *retinfo)
2037 struct serial_struct tmp = {
2039 .line = info->port.tty->index,
2040 .flags = info->port.flags,
2041 .baud_base = 921600,
2042 .close_delay = info->port.close_delay
2044 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2048 static int moxa_set_serial_info(struct moxa_port *info,
2049 struct serial_struct __user *new_info)
2051 struct serial_struct new_serial;
2053 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2056 if (new_serial.irq != 0 || new_serial.port != 0 ||
2057 new_serial.custom_divisor != 0 ||
2058 new_serial.baud_base != 921600)
2061 if (!capable(CAP_SYS_ADMIN)) {
2062 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2063 (info->port.flags & ~ASYNC_USR_MASK)))
2066 info->port.close_delay = new_serial.close_delay * HZ / 100;
2068 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2069 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
2071 MoxaSetFifo(info, new_serial.type == PORT_16550A);
2073 info->type = new_serial.type;
2079 /*****************************************************************************
2080 * Static local functions: *
2081 *****************************************************************************/
2083 static void MoxaSetFifo(struct moxa_port *port, int enable)
2085 void __iomem *ofsAddr = port->tableAddr;
2088 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2089 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2091 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2092 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);