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/smp_lock.h>
38 #include <linux/string.h>
39 #include <linux/fcntl.h>
40 #include <linux/ptrace.h>
41 #include <linux/serial.h>
42 #include <linux/tty_driver.h>
43 #include <linux/delay.h>
44 #include <linux/pci.h>
45 #include <linux/init.h>
46 #include <linux/bitops.h>
48 #include <asm/system.h>
50 #include <asm/uaccess.h>
54 #define MOXA_VERSION "6.0k"
56 #define MOXA_FW_HDRLEN 32
60 #define MAX_BOARDS 4 /* Don't change this value */
61 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
62 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
64 #define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
65 (brd)->boardType == MOXA_BOARD_C320_PCI)
68 * Define the Moxa PCI vendor and device IDs.
70 #define MOXA_BUS_TYPE_ISA 0
71 #define MOXA_BUS_TYPE_PCI 1
74 MOXA_BOARD_C218_PCI = 1,
81 static char *moxa_brdname[] =
83 "C218 Turbo PCI series",
84 "C218 Turbo ISA series",
85 "C320 Turbo PCI series",
86 "C320 Turbo ISA series",
91 static struct pci_device_id moxa_pcibrds[] = {
92 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
93 .driver_data = MOXA_BOARD_C218_PCI },
94 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
95 .driver_data = MOXA_BOARD_C320_PCI },
96 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
97 .driver_data = MOXA_BOARD_CP204J },
100 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
101 #endif /* CONFIG_PCI */
105 static struct moxa_board_conf {
112 struct moxa_port *ports;
114 void __iomem *basemem;
115 void __iomem *intNdx;
116 void __iomem *intPend;
117 void __iomem *intTable;
118 } moxa_boards[MAX_BOARDS];
120 struct mxser_mstatus {
134 struct tty_port port;
135 struct moxa_board_conf *board;
136 void __iomem *tableAddr;
140 unsigned long statusflags;
149 int rxcnt[MAX_PORTS];
150 int txcnt[MAX_PORTS];
159 #define SERIAL_DO_RESTART
161 #define WAKEUP_CHARS 256
163 static int ttymajor = MOXAMAJOR;
164 static struct mon_str moxaLog;
165 static unsigned int moxaFuncTout = HZ / 2;
166 static unsigned int moxaLowWaterChk;
167 static DEFINE_MUTEX(moxa_openlock);
168 /* Variables for insmod */
170 static unsigned long baseaddr[MAX_BOARDS];
171 static unsigned int type[MAX_BOARDS];
172 static unsigned int numports[MAX_BOARDS];
175 MODULE_AUTHOR("William Chen");
176 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
177 MODULE_LICENSE("GPL");
179 module_param_array(type, uint, NULL, 0);
180 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
181 module_param_array(baseaddr, ulong, NULL, 0);
182 MODULE_PARM_DESC(baseaddr, "base address");
183 module_param_array(numports, uint, NULL, 0);
184 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
186 module_param(ttymajor, int, 0);
191 static int moxa_open(struct tty_struct *, struct file *);
192 static void moxa_close(struct tty_struct *, struct file *);
193 static int moxa_write(struct tty_struct *, const unsigned char *, int);
194 static int moxa_write_room(struct tty_struct *);
195 static void moxa_flush_buffer(struct tty_struct *);
196 static int moxa_chars_in_buffer(struct tty_struct *);
197 static void moxa_throttle(struct tty_struct *);
198 static void moxa_unthrottle(struct tty_struct *);
199 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
200 static void moxa_stop(struct tty_struct *);
201 static void moxa_start(struct tty_struct *);
202 static void moxa_hangup(struct tty_struct *);
203 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
204 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
205 unsigned int set, unsigned int clear);
206 static void moxa_poll(unsigned long);
207 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
208 static void moxa_setup_empty_event(struct tty_struct *);
209 static void moxa_shutdown(struct tty_port *);
210 static int moxa_carrier_raised(struct tty_port *);
211 static void moxa_dtr_rts(struct tty_port *, int);
213 * moxa board interface functions:
215 static void MoxaPortEnable(struct moxa_port *);
216 static void MoxaPortDisable(struct moxa_port *);
217 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
218 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
219 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
220 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
221 static int MoxaPortLineStatus(struct moxa_port *);
222 static void MoxaPortFlushData(struct moxa_port *, int);
223 static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
224 static int MoxaPortReadData(struct moxa_port *);
225 static int MoxaPortTxQueue(struct moxa_port *);
226 static int MoxaPortRxQueue(struct moxa_port *);
227 static int MoxaPortTxFree(struct moxa_port *);
228 static void MoxaPortTxDisable(struct moxa_port *);
229 static void MoxaPortTxEnable(struct moxa_port *);
230 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
231 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
232 static void MoxaSetFifo(struct moxa_port *port, int enable);
238 static DEFINE_SPINLOCK(moxafunc_lock);
240 static void moxa_wait_finish(void __iomem *ofsAddr)
242 unsigned long end = jiffies + moxaFuncTout;
244 while (readw(ofsAddr + FuncCode) != 0)
245 if (time_after(jiffies, end))
247 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
248 printk(KERN_WARNING "moxa function expired\n");
251 static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
254 spin_lock_irqsave(&moxafunc_lock, flags);
255 writew(arg, ofsAddr + FuncArg);
256 writew(cmd, ofsAddr + FuncCode);
257 moxa_wait_finish(ofsAddr);
258 spin_unlock_irqrestore(&moxafunc_lock, flags);
261 static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
265 spin_lock_irqsave(&moxafunc_lock, flags);
266 writew(arg, ofsAddr + FuncArg);
267 writew(cmd, ofsAddr + FuncCode);
268 moxa_wait_finish(ofsAddr);
269 ret = readw(ofsAddr + FuncArg);
270 spin_unlock_irqrestore(&moxafunc_lock, flags);
274 static void moxa_low_water_check(void __iomem *ofsAddr)
276 u16 rptr, wptr, mask, len;
278 if (readb(ofsAddr + FlagStat) & Xoff_state) {
279 rptr = readw(ofsAddr + RXrptr);
280 wptr = readw(ofsAddr + RXwptr);
281 mask = readw(ofsAddr + RX_mask);
282 len = (wptr - rptr) & mask;
283 if (len <= Low_water)
284 moxafunc(ofsAddr, FC_SendXon, 0);
292 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
293 unsigned int cmd, unsigned long arg)
295 struct moxa_port *ch = tty->driver_data;
296 void __user *argp = (void __user *)arg;
299 if (tty->index == MAX_PORTS) {
300 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
301 cmd != MOXA_GETMSTATUS)
307 case MOXA_GETDATACOUNT:
308 moxaLog.tick = jiffies;
309 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
312 case MOXA_FLUSH_QUEUE:
313 MoxaPortFlushData(ch, arg);
315 case MOXA_GET_IOQUEUE: {
316 struct moxaq_str __user *argm = argp;
317 struct moxaq_str tmp;
321 mutex_lock(&moxa_openlock);
322 for (i = 0; i < MAX_BOARDS; i++) {
323 p = moxa_boards[i].ports;
324 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
325 memset(&tmp, 0, sizeof(tmp));
326 if (moxa_boards[i].ready) {
327 tmp.inq = MoxaPortRxQueue(p);
328 tmp.outq = MoxaPortTxQueue(p);
330 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
331 mutex_unlock(&moxa_openlock);
336 mutex_unlock(&moxa_openlock);
338 } case MOXA_GET_OQUEUE:
339 status = MoxaPortTxQueue(ch);
340 ret = put_user(status, (unsigned long __user *)argp);
342 case MOXA_GET_IQUEUE:
343 status = MoxaPortRxQueue(ch);
344 ret = put_user(status, (unsigned long __user *)argp);
346 case MOXA_GETMSTATUS: {
347 struct mxser_mstatus __user *argm = argp;
348 struct mxser_mstatus tmp;
352 mutex_lock(&moxa_openlock);
353 for (i = 0; i < MAX_BOARDS; i++) {
354 p = moxa_boards[i].ports;
355 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
356 struct tty_struct *ttyp;
357 memset(&tmp, 0, sizeof(tmp));
358 if (!moxa_boards[i].ready)
361 status = MoxaPortLineStatus(p);
369 ttyp = tty_port_tty_get(&p->port);
370 if (!ttyp || !ttyp->termios)
371 tmp.cflag = p->cflag;
373 tmp.cflag = ttyp->termios->c_cflag;
376 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
377 mutex_unlock(&moxa_openlock);
382 mutex_unlock(&moxa_openlock);
386 mutex_lock(&ch->port.mutex);
387 ret = moxa_get_serial_info(ch, argp);
388 mutex_unlock(&ch->port.mutex);
391 mutex_lock(&ch->port.mutex);
392 ret = moxa_set_serial_info(ch, argp);
393 mutex_unlock(&ch->port.mutex);
401 static int moxa_break_ctl(struct tty_struct *tty, int state)
403 struct moxa_port *port = tty->driver_data;
405 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
410 static const struct tty_operations moxa_ops = {
414 .write_room = moxa_write_room,
415 .flush_buffer = moxa_flush_buffer,
416 .chars_in_buffer = moxa_chars_in_buffer,
418 .throttle = moxa_throttle,
419 .unthrottle = moxa_unthrottle,
420 .set_termios = moxa_set_termios,
423 .hangup = moxa_hangup,
424 .break_ctl = moxa_break_ctl,
425 .tiocmget = moxa_tiocmget,
426 .tiocmset = moxa_tiocmset,
429 static const struct tty_port_operations moxa_port_ops = {
430 .carrier_raised = moxa_carrier_raised,
431 .dtr_rts = moxa_dtr_rts,
432 .shutdown = moxa_shutdown,
435 static struct tty_driver *moxaDriver;
436 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
437 static DEFINE_SPINLOCK(moxa_lock);
443 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
445 switch (brd->boardType) {
446 case MOXA_BOARD_C218_ISA:
447 case MOXA_BOARD_C218_PCI:
451 case MOXA_BOARD_CP204J:
465 static int moxa_check_fw(const void *ptr)
467 const __le16 *lptr = ptr;
469 if (*lptr != cpu_to_le16(0x7980))
475 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
478 void __iomem *baseAddr = brd->basemem;
481 writeb(HW_reset, baseAddr + Control_reg); /* reset */
483 memset_io(baseAddr, 0, 4096);
484 memcpy_toio(baseAddr, buf, len); /* download BIOS */
485 writeb(0, baseAddr + Control_reg); /* restart */
489 switch (brd->boardType) {
490 case MOXA_BOARD_C218_ISA:
491 case MOXA_BOARD_C218_PCI:
492 tmp = readw(baseAddr + C218_key);
493 if (tmp != C218_KeyCode)
496 case MOXA_BOARD_CP204J:
497 tmp = readw(baseAddr + C218_key);
498 if (tmp != CP204J_KeyCode)
502 tmp = readw(baseAddr + C320_key);
503 if (tmp != C320_KeyCode)
505 tmp = readw(baseAddr + C320_status);
506 if (tmp != STS_init) {
507 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
508 "module not found\n");
516 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
520 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
523 void __iomem *baseAddr = brd->basemem;
526 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
530 writew(len - 7168 - 2, baseAddr + C320bapi_len);
531 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
532 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
533 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
534 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
539 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
542 void __iomem *baseAddr = brd->basemem;
543 const __le16 *uptr = ptr;
544 size_t wlen, len2, j;
545 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
546 unsigned int i, retry;
549 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
552 switch (brd->boardType) {
553 case MOXA_BOARD_CP204J:
554 case MOXA_BOARD_C218_ISA:
555 case MOXA_BOARD_C218_PCI:
557 loadbuf = C218_LoadBuf;
558 loadlen = C218DLoad_len;
559 checksum = C218check_sum;
560 checksum_ok = C218chksum_ok;
564 keycode = C320_KeyCode;
565 loadbuf = C320_LoadBuf;
566 loadlen = C320DLoad_len;
567 checksum = C320check_sum;
568 checksum_ok = C320chksum_ok;
574 for (i = 0; i < wlen; i++)
575 usum += le16_to_cpu(uptr[i]);
581 len2 = (wlen > 2048) ? 2048 : wlen;
583 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
586 writew(len2, baseAddr + loadlen);
587 writew(0, baseAddr + key);
588 for (i = 0; i < 100; i++) {
589 if (readw(baseAddr + key) == keycode)
593 if (readw(baseAddr + key) != keycode)
596 writew(0, baseAddr + loadlen);
597 writew(usum, baseAddr + checksum);
598 writew(0, baseAddr + key);
599 for (i = 0; i < 100; i++) {
600 if (readw(baseAddr + key) == keycode)
605 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
606 if (readb(baseAddr + checksum_ok) != 1)
609 writew(0, baseAddr + key);
610 for (i = 0; i < 600; i++) {
611 if (readw(baseAddr + Magic_no) == Magic_code)
615 if (readw(baseAddr + Magic_no) != Magic_code)
618 if (MOXA_IS_320(brd)) {
619 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
620 writew(0x3800, baseAddr + TMS320_PORT1);
621 writew(0x3900, baseAddr + TMS320_PORT2);
622 writew(28499, baseAddr + TMS320_CLOCK);
624 writew(0x3200, baseAddr + TMS320_PORT1);
625 writew(0x3400, baseAddr + TMS320_PORT2);
626 writew(19999, baseAddr + TMS320_CLOCK);
629 writew(1, baseAddr + Disable_IRQ);
630 writew(0, baseAddr + Magic_no);
631 for (i = 0; i < 500; i++) {
632 if (readw(baseAddr + Magic_no) == Magic_code)
636 if (readw(baseAddr + Magic_no) != Magic_code)
639 if (MOXA_IS_320(brd)) {
640 j = readw(baseAddr + Module_cnt);
643 brd->numPorts = j * 8;
644 writew(j, baseAddr + Module_no);
645 writew(0, baseAddr + Magic_no);
646 for (i = 0; i < 600; i++) {
647 if (readw(baseAddr + Magic_no) == Magic_code)
651 if (readw(baseAddr + Magic_no) != Magic_code)
654 brd->intNdx = baseAddr + IRQindex;
655 brd->intPend = baseAddr + IRQpending;
656 brd->intTable = baseAddr + IRQtable;
661 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
664 void __iomem *ofsAddr, *baseAddr = brd->basemem;
665 struct moxa_port *port;
669 printk(KERN_ERR "MOXA: bios length is not even\n");
673 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
677 switch (brd->boardType) {
678 case MOXA_BOARD_C218_ISA:
679 case MOXA_BOARD_C218_PCI:
680 case MOXA_BOARD_CP204J:
682 for (i = 0; i < brd->numPorts; i++, port++) {
685 port->tableAddr = baseAddr + Extern_table +
687 ofsAddr = port->tableAddr;
688 writew(C218rx_mask, ofsAddr + RX_mask);
689 writew(C218tx_mask, ofsAddr + TX_mask);
690 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
691 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
693 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
694 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
700 for (i = 0; i < brd->numPorts; i++, port++) {
703 port->tableAddr = baseAddr + Extern_table +
705 ofsAddr = port->tableAddr;
706 switch (brd->numPorts) {
708 writew(C320p8rx_mask, ofsAddr + RX_mask);
709 writew(C320p8tx_mask, ofsAddr + TX_mask);
710 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
711 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
712 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
713 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
717 writew(C320p16rx_mask, ofsAddr + RX_mask);
718 writew(C320p16tx_mask, ofsAddr + TX_mask);
719 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
720 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
721 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
722 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
726 writew(C320p24rx_mask, ofsAddr + RX_mask);
727 writew(C320p24tx_mask, ofsAddr + TX_mask);
728 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
729 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
730 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
731 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
734 writew(C320p32rx_mask, ofsAddr + RX_mask);
735 writew(C320p32tx_mask, ofsAddr + TX_mask);
736 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
737 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
738 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
739 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
740 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
749 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
751 const void *ptr = fw->data;
755 unsigned int a, lenp, lencnt;
758 __le32 magic; /* 0x34303430 */
760 u8 type; /* UNIX = 3 */
761 u8 model; /* C218T=1, C320T=2, CP204=3 */
766 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
768 if (fw->size < MOXA_FW_HDRLEN) {
769 strcpy(rsn, "too short (even header won't fit)");
772 if (hdr->magic != cpu_to_le32(0x30343034)) {
773 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
776 if (hdr->type != 3) {
777 sprintf(rsn, "not for linux, type is %u", hdr->type);
780 if (moxa_check_fw_model(brd, hdr->model)) {
781 sprintf(rsn, "not for this card, model is %u", hdr->model);
785 len = MOXA_FW_HDRLEN;
786 lencnt = hdr->model == 2 ? 5 : 3;
787 for (a = 0; a < ARRAY_SIZE(lens); a++) {
788 lens[a] = le16_to_cpu(hdr->len[a]);
789 if (lens[a] && len + lens[a] <= fw->size &&
790 moxa_check_fw(&fw->data[len]))
791 printk(KERN_WARNING "MOXA firmware: unexpected input "
792 "at offset %u, but going on\n", (u32)len);
793 if (!lens[a] && a < lencnt) {
794 sprintf(rsn, "too few entries in fw file");
800 if (len != fw->size) {
801 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
806 ptr += MOXA_FW_HDRLEN;
809 strcpy(rsn, "read above");
811 ret = moxa_load_bios(brd, ptr, lens[lenp]);
815 /* we skip the tty section (lens[1]), since we don't need it */
816 ptr += lens[lenp] + lens[lenp + 1];
817 lenp += 2; /* comm */
819 if (hdr->model == 2) {
820 ret = moxa_load_320b(brd, ptr, lens[lenp]);
823 /* skip another tty */
824 ptr += lens[lenp] + lens[lenp + 1];
828 ret = moxa_load_code(brd, ptr, lens[lenp]);
834 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
838 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
840 const struct firmware *fw;
846 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
848 if (brd->ports == NULL) {
849 printk(KERN_ERR "cannot allocate memory for ports\n");
854 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
855 tty_port_init(&p->port);
856 p->port.ops = &moxa_port_ops;
857 p->type = PORT_16550A;
858 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
861 switch (brd->boardType) {
862 case MOXA_BOARD_C218_ISA:
863 case MOXA_BOARD_C218_PCI:
864 file = "c218tunx.cod";
866 case MOXA_BOARD_CP204J:
867 file = "cp204unx.cod";
870 file = "c320tunx.cod";
874 ret = request_firmware(&fw, file, dev);
876 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
877 "you've placed '%s' file into your firmware "
878 "loader directory (e.g. /lib/firmware)\n",
883 ret = moxa_load_fw(brd, fw);
885 release_firmware(fw);
890 spin_lock_bh(&moxa_lock);
892 if (!timer_pending(&moxaTimer))
893 mod_timer(&moxaTimer, jiffies + HZ / 50);
894 spin_unlock_bh(&moxa_lock);
903 static void moxa_board_deinit(struct moxa_board_conf *brd)
905 unsigned int a, opened;
907 mutex_lock(&moxa_openlock);
908 spin_lock_bh(&moxa_lock);
910 spin_unlock_bh(&moxa_lock);
912 /* pci hot-un-plug support */
913 for (a = 0; a < brd->numPorts; a++)
914 if (brd->ports[a].port.flags & ASYNC_INITIALIZED) {
915 struct tty_struct *tty = tty_port_tty_get(
916 &brd->ports[a].port);
924 for (a = 0; a < brd->numPorts; a++)
925 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
927 mutex_unlock(&moxa_openlock);
931 mutex_lock(&moxa_openlock);
934 iounmap(brd->basemem);
940 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
941 const struct pci_device_id *ent)
943 struct moxa_board_conf *board;
945 int board_type = ent->driver_data;
948 retval = pci_enable_device(pdev);
950 dev_err(&pdev->dev, "can't enable pci device\n");
954 for (i = 0; i < MAX_BOARDS; i++)
955 if (moxa_boards[i].basemem == NULL)
959 if (i >= MAX_BOARDS) {
960 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
961 "found. Board is ignored.\n", MAX_BOARDS);
965 board = &moxa_boards[i];
967 retval = pci_request_region(pdev, 2, "moxa-base");
969 dev_err(&pdev->dev, "can't request pci region 2\n");
973 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
974 if (board->basemem == NULL) {
975 dev_err(&pdev->dev, "can't remap io space 2\n");
979 board->boardType = board_type;
980 switch (board_type) {
981 case MOXA_BOARD_C218_ISA:
982 case MOXA_BOARD_C218_PCI:
986 case MOXA_BOARD_CP204J:
993 board->busType = MOXA_BUS_TYPE_PCI;
995 retval = moxa_init_board(board, &pdev->dev);
999 pci_set_drvdata(pdev, board);
1001 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
1002 moxa_brdname[board_type - 1], board->numPorts);
1006 iounmap(board->basemem);
1007 board->basemem = NULL;
1009 pci_release_region(pdev, 2);
1014 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
1016 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
1018 moxa_board_deinit(brd);
1020 pci_release_region(pdev, 2);
1023 static struct pci_driver moxa_pci_driver = {
1025 .id_table = moxa_pcibrds,
1026 .probe = moxa_pci_probe,
1027 .remove = __devexit_p(moxa_pci_remove)
1029 #endif /* CONFIG_PCI */
1031 static int __init moxa_init(void)
1033 unsigned int isabrds = 0;
1036 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1038 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1042 moxaDriver->owner = THIS_MODULE;
1043 moxaDriver->name = "ttyMX";
1044 moxaDriver->major = ttymajor;
1045 moxaDriver->minor_start = 0;
1046 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1047 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1048 moxaDriver->init_termios = tty_std_termios;
1049 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1050 moxaDriver->init_termios.c_ispeed = 9600;
1051 moxaDriver->init_termios.c_ospeed = 9600;
1052 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1053 tty_set_operations(moxaDriver, &moxa_ops);
1055 if (tty_register_driver(moxaDriver)) {
1056 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1057 put_tty_driver(moxaDriver);
1061 /* Find the boards defined from module args. */
1064 struct moxa_board_conf *brd = moxa_boards;
1066 for (i = 0; i < MAX_BOARDS; i++) {
1069 if (type[i] == MOXA_BOARD_C218_ISA ||
1070 type[i] == MOXA_BOARD_C320_ISA) {
1071 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1072 isabrds + 1, moxa_brdname[type[i] - 1],
1074 brd->boardType = type[i];
1075 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1077 brd->busType = MOXA_BUS_TYPE_ISA;
1078 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
1079 if (!brd->basemem) {
1080 printk(KERN_ERR "MOXA: can't remap %lx\n",
1084 if (moxa_init_board(brd, NULL)) {
1085 iounmap(brd->basemem);
1086 brd->basemem = NULL;
1090 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1091 "ready (%u ports, firmware loaded)\n",
1092 baseaddr[i], brd->numPorts);
1102 retval = pci_register_driver(&moxa_pci_driver);
1104 printk(KERN_ERR "Can't register MOXA pci driver!\n");
1113 static void __exit moxa_exit(void)
1118 pci_unregister_driver(&moxa_pci_driver);
1121 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1122 if (moxa_boards[i].ready)
1123 moxa_board_deinit(&moxa_boards[i]);
1125 del_timer_sync(&moxaTimer);
1127 if (tty_unregister_driver(moxaDriver))
1128 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1130 put_tty_driver(moxaDriver);
1133 module_init(moxa_init);
1134 module_exit(moxa_exit);
1136 static void moxa_shutdown(struct tty_port *port)
1138 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1139 MoxaPortDisable(ch);
1140 MoxaPortFlushData(ch, 2);
1141 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1144 static int moxa_carrier_raised(struct tty_port *port)
1146 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1149 spin_lock_bh(&moxa_lock);
1151 spin_unlock_bh(&moxa_lock);
1155 static void moxa_dtr_rts(struct tty_port *port, int onoff)
1157 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1158 MoxaPortLineCtrl(ch, onoff, onoff);
1162 static int moxa_open(struct tty_struct *tty, struct file *filp)
1164 struct moxa_board_conf *brd;
1165 struct moxa_port *ch;
1170 if (port == MAX_PORTS) {
1171 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1173 if (mutex_lock_interruptible(&moxa_openlock))
1174 return -ERESTARTSYS;
1175 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1177 mutex_unlock(&moxa_openlock);
1181 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1182 mutex_unlock(&moxa_openlock);
1186 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1188 tty->driver_data = ch;
1189 tty_port_tty_set(&ch->port, tty);
1190 mutex_lock(&ch->port.mutex);
1191 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
1192 ch->statusflags = 0;
1193 moxa_set_tty_param(tty, tty->termios);
1194 MoxaPortLineCtrl(ch, 1, 1);
1196 MoxaSetFifo(ch, ch->type == PORT_16550A);
1197 ch->port.flags |= ASYNC_INITIALIZED;
1199 mutex_unlock(&ch->port.mutex);
1200 mutex_unlock(&moxa_openlock);
1202 retval = tty_port_block_til_ready(&ch->port, tty, filp);
1204 set_bit(ASYNCB_NORMAL_ACTIVE, &ch->port.flags);
1208 static void moxa_close(struct tty_struct *tty, struct file *filp)
1210 struct moxa_port *ch = tty->driver_data;
1211 ch->cflag = tty->termios->c_cflag;
1212 tty_port_close(&ch->port, tty, filp);
1215 static int moxa_write(struct tty_struct *tty,
1216 const unsigned char *buf, int count)
1218 struct moxa_port *ch = tty->driver_data;
1224 spin_lock_bh(&moxa_lock);
1225 len = MoxaPortWriteData(tty, buf, count);
1226 spin_unlock_bh(&moxa_lock);
1228 set_bit(LOWWAIT, &ch->statusflags);
1232 static int moxa_write_room(struct tty_struct *tty)
1234 struct moxa_port *ch;
1238 ch = tty->driver_data;
1241 return MoxaPortTxFree(ch);
1244 static void moxa_flush_buffer(struct tty_struct *tty)
1246 struct moxa_port *ch = tty->driver_data;
1250 MoxaPortFlushData(ch, 1);
1254 static int moxa_chars_in_buffer(struct tty_struct *tty)
1256 struct moxa_port *ch = tty->driver_data;
1260 chars = MoxaPortTxQueue(ch);
1263 * Make it possible to wakeup anything waiting for output
1264 * in tty_ioctl.c, etc.
1266 if (!test_bit(EMPTYWAIT, &ch->statusflags))
1267 moxa_setup_empty_event(tty);
1273 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1275 struct moxa_port *ch;
1276 int flag = 0, dtr, rts;
1278 mutex_lock(&moxa_openlock);
1279 ch = tty->driver_data;
1281 mutex_unlock(&moxa_openlock);
1285 MoxaPortGetLineOut(ch, &dtr, &rts);
1290 dtr = MoxaPortLineStatus(ch);
1297 mutex_unlock(&moxa_openlock);
1301 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1302 unsigned int set, unsigned int clear)
1304 struct moxa_port *ch;
1309 mutex_lock(&moxa_openlock);
1310 ch = tty->driver_data;
1312 mutex_unlock(&moxa_openlock);
1316 MoxaPortGetLineOut(ch, &dtr, &rts);
1317 if (set & TIOCM_RTS)
1319 if (set & TIOCM_DTR)
1321 if (clear & TIOCM_RTS)
1323 if (clear & TIOCM_DTR)
1325 MoxaPortLineCtrl(ch, dtr, rts);
1326 mutex_unlock(&moxa_openlock);
1330 static void moxa_throttle(struct tty_struct *tty)
1332 struct moxa_port *ch = tty->driver_data;
1334 set_bit(THROTTLE, &ch->statusflags);
1337 static void moxa_unthrottle(struct tty_struct *tty)
1339 struct moxa_port *ch = tty->driver_data;
1341 clear_bit(THROTTLE, &ch->statusflags);
1344 static void moxa_set_termios(struct tty_struct *tty,
1345 struct ktermios *old_termios)
1347 struct moxa_port *ch = tty->driver_data;
1351 moxa_set_tty_param(tty, old_termios);
1352 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1353 wake_up_interruptible(&ch->port.open_wait);
1356 static void moxa_stop(struct tty_struct *tty)
1358 struct moxa_port *ch = tty->driver_data;
1362 MoxaPortTxDisable(ch);
1363 set_bit(TXSTOPPED, &ch->statusflags);
1367 static void moxa_start(struct tty_struct *tty)
1369 struct moxa_port *ch = tty->driver_data;
1374 if (!(ch->statusflags & TXSTOPPED))
1377 MoxaPortTxEnable(ch);
1378 clear_bit(TXSTOPPED, &ch->statusflags);
1381 static void moxa_hangup(struct tty_struct *tty)
1383 struct moxa_port *ch = tty->driver_data;
1384 tty_port_hangup(&ch->port);
1387 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1389 struct tty_struct *tty;
1392 if (dcd != p->DCDState) {
1393 tty = tty_port_tty_get(&p->port);
1394 if (tty && C_CLOCAL(tty) && !dcd)
1401 static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1404 struct tty_struct *tty = tty_port_tty_get(&p->port);
1405 void __iomem *ofsAddr;
1406 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
1410 if (test_bit(EMPTYWAIT, &p->statusflags) &&
1411 MoxaPortTxQueue(p) == 0) {
1412 clear_bit(EMPTYWAIT, &p->statusflags);
1415 if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
1416 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1417 clear_bit(LOWWAIT, &p->statusflags);
1421 if (inited && !test_bit(THROTTLE, &p->statusflags) &&
1422 MoxaPortRxQueue(p) > 0) { /* RX */
1423 MoxaPortReadData(p);
1424 tty_schedule_flip(tty);
1427 clear_bit(EMPTYWAIT, &p->statusflags);
1428 MoxaPortFlushData(p, 0); /* flush RX */
1431 if (!handle) /* nothing else to do */
1434 intr = readw(ip); /* port irq status */
1438 writew(0, ip); /* ACK port */
1439 ofsAddr = p->tableAddr;
1440 if (intr & IntrTx) /* disable tx intr */
1441 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1442 ofsAddr + HostStat);
1447 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1448 tty_insert_flip_char(tty, 0, TTY_BREAK);
1449 tty_schedule_flip(tty);
1452 if (intr & IntrLine)
1453 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1460 static void moxa_poll(unsigned long ignored)
1462 struct moxa_board_conf *brd;
1464 unsigned int card, port, served = 0;
1466 spin_lock(&moxa_lock);
1467 for (card = 0; card < MAX_BOARDS; card++) {
1468 brd = &moxa_boards[card];
1475 if (readb(brd->intPend) == 0xff)
1476 ip = brd->intTable + readb(brd->intNdx);
1478 for (port = 0; port < brd->numPorts; port++)
1479 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1482 writeb(0, brd->intPend); /* ACK */
1484 if (moxaLowWaterChk) {
1485 struct moxa_port *p = brd->ports;
1486 for (port = 0; port < brd->numPorts; port++, p++)
1487 if (p->lowChkFlag) {
1489 moxa_low_water_check(p->tableAddr);
1493 moxaLowWaterChk = 0;
1496 mod_timer(&moxaTimer, jiffies + HZ / 50);
1497 spin_unlock(&moxa_lock);
1500 /******************************************************************************/
1502 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1504 register struct ktermios *ts = tty->termios;
1505 struct moxa_port *ch = tty->driver_data;
1506 int rts, cts, txflow, rxflow, xany, baud;
1508 rts = cts = txflow = rxflow = xany = 0;
1509 if (ts->c_cflag & CRTSCTS)
1511 if (ts->c_iflag & IXON)
1513 if (ts->c_iflag & IXOFF)
1515 if (ts->c_iflag & IXANY)
1518 /* Clear the features we don't support */
1519 ts->c_cflag &= ~CMSPAR;
1520 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1521 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1523 baud = tty_termios_baud_rate(old_termios);
1524 /* Not put the baud rate into the termios data */
1525 tty_encode_baud_rate(tty, baud, baud);
1528 static void moxa_setup_empty_event(struct tty_struct *tty)
1530 struct moxa_port *ch = tty->driver_data;
1531 set_bit(EMPTYWAIT, &ch->statusflags);
1534 /*****************************************************************************
1535 * Driver level functions: *
1536 *****************************************************************************/
1538 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1540 void __iomem *ofsAddr;
1541 if (mode < 0 || mode > 2)
1543 ofsAddr = port->tableAddr;
1544 moxafunc(ofsAddr, FC_FlushQueue, mode);
1546 port->lowChkFlag = 0;
1547 moxa_low_water_check(ofsAddr);
1552 * Moxa Port Number Description:
1554 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1555 * the port number using in MOXA driver functions will be 0 to 31 for
1556 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1557 * to 127 for fourth. For example, if you setup three MOXA boards,
1558 * first board is C218, second board is C320-16 and third board is
1559 * C320-32. The port number of first board (C218 - 8 ports) is from
1560 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1561 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1562 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1563 * 127 will be invalid.
1566 * Moxa Functions Description:
1568 * Function 1: Driver initialization routine, this routine must be
1569 * called when initialized driver.
1571 * void MoxaDriverInit();
1574 * Function 2: Moxa driver private IOCTL command processing.
1576 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1578 * unsigned int cmd : IOCTL command
1579 * unsigned long arg : IOCTL argument
1580 * int port : port number (0 - 127)
1587 * Function 6: Enable this port to start Tx/Rx data.
1589 * void MoxaPortEnable(int port);
1590 * int port : port number (0 - 127)
1593 * Function 7: Disable this port
1595 * void MoxaPortDisable(int port);
1596 * int port : port number (0 - 127)
1599 * Function 10: Setting baud rate of this port.
1601 * speed_t MoxaPortSetBaud(int port, speed_t baud);
1602 * int port : port number (0 - 127)
1603 * long baud : baud rate (50 - 115200)
1605 * return: 0 : this port is invalid or baud < 50
1606 * 50 - 115200 : the real baud rate set to the port, if
1607 * the argument baud is large than maximun
1608 * available baud rate, the real setting
1609 * baud rate will be the maximun baud rate.
1612 * Function 12: Configure the port.
1614 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1615 * int port : port number (0 - 127)
1616 * struct ktermios * termio : termio structure pointer
1617 * speed_t baud : baud rate
1619 * return: -1 : this port is invalid or termio == NULL
1623 * Function 13: Get the DTR/RTS state of this port.
1625 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1626 * int port : port number (0 - 127)
1627 * int * dtrState : pointer to INT to receive the current DTR
1628 * state. (if NULL, this function will not
1629 * write to this address)
1630 * int * rtsState : pointer to INT to receive the current RTS
1631 * state. (if NULL, this function will not
1632 * write to this address)
1634 * return: -1 : this port is invalid
1638 * Function 14: Setting the DTR/RTS output state of this port.
1640 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1641 * int port : port number (0 - 127)
1642 * int dtrState : DTR output state (0: off, 1: on)
1643 * int rtsState : RTS output state (0: off, 1: on)
1646 * Function 15: Setting the flow control of this port.
1648 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1649 * int txFlow,int xany);
1650 * int port : port number (0 - 127)
1651 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1652 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1653 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1654 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1655 * int xany : S/W XANY flow control (0: no, 1: yes)
1658 * Function 16: Get ths line status of this port
1660 * int MoxaPortLineStatus(int port);
1661 * int port : port number (0 - 127)
1663 * return: Bit 0 - CTS state (0: off, 1: on)
1664 * Bit 1 - DSR state (0: off, 1: on)
1665 * Bit 2 - DCD state (0: off, 1: on)
1668 * Function 19: Flush the Rx/Tx buffer data of this port.
1670 * void MoxaPortFlushData(int port, int mode);
1671 * int port : port number (0 - 127)
1673 * 0 : flush the Rx buffer
1674 * 1 : flush the Tx buffer
1675 * 2 : flush the Rx and Tx buffer
1678 * Function 20: Write data.
1680 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1681 * int port : port number (0 - 127)
1682 * unsigned char * buffer : pointer to write data buffer.
1683 * int length : write data length
1685 * return: 0 - length : real write data length
1688 * Function 21: Read data.
1690 * int MoxaPortReadData(int port, struct tty_struct *tty);
1691 * int port : port number (0 - 127)
1692 * struct tty_struct *tty : tty for data
1694 * return: 0 - length : real read data length
1697 * Function 24: Get the Tx buffer current queued data bytes
1699 * int MoxaPortTxQueue(int port);
1700 * int port : port number (0 - 127)
1702 * return: .. : Tx buffer current queued data bytes
1705 * Function 25: Get the Tx buffer current free space
1707 * int MoxaPortTxFree(int port);
1708 * int port : port number (0 - 127)
1710 * return: .. : Tx buffer current free space
1713 * Function 26: Get the Rx buffer current queued data bytes
1715 * int MoxaPortRxQueue(int port);
1716 * int port : port number (0 - 127)
1718 * return: .. : Rx buffer current queued data bytes
1721 * Function 28: Disable port data transmission.
1723 * void MoxaPortTxDisable(int port);
1724 * int port : port number (0 - 127)
1727 * Function 29: Enable port data transmission.
1729 * void MoxaPortTxEnable(int port);
1730 * int port : port number (0 - 127)
1733 * Function 31: Get the received BREAK signal count and reset it.
1735 * int MoxaPortResetBrkCnt(int port);
1736 * int port : port number (0 - 127)
1738 * return: 0 - .. : BREAK signal count
1743 static void MoxaPortEnable(struct moxa_port *port)
1745 void __iomem *ofsAddr;
1748 ofsAddr = port->tableAddr;
1749 writew(lowwater, ofsAddr + Low_water);
1750 if (MOXA_IS_320(port->board))
1751 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1753 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1754 ofsAddr + HostStat);
1756 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1757 moxafunc(ofsAddr, FC_FlushQueue, 2);
1759 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1760 MoxaPortLineStatus(port);
1763 static void MoxaPortDisable(struct moxa_port *port)
1765 void __iomem *ofsAddr = port->tableAddr;
1767 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1768 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1769 writew(0, ofsAddr + HostStat);
1770 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1773 static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
1775 void __iomem *ofsAddr = port->tableAddr;
1776 unsigned int clock, val;
1779 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1786 moxafunc(ofsAddr, FC_SetBaud, val);
1791 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1794 void __iomem *ofsAddr;
1798 ofsAddr = port->tableAddr;
1799 cflag = termio->c_cflag; /* termio->c_cflag */
1801 mode = termio->c_cflag & CSIZE;
1804 else if (mode == CS6)
1806 else if (mode == CS7)
1808 else if (mode == CS8)
1811 if (termio->c_cflag & CSTOPB) {
1819 if (termio->c_cflag & PARENB) {
1820 if (termio->c_cflag & PARODD)
1827 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
1829 if (MOXA_IS_320(port->board) && baud >= 921600)
1832 baud = MoxaPortSetBaud(port, baud);
1834 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1835 spin_lock_irq(&moxafunc_lock);
1836 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1837 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1838 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1839 moxa_wait_finish(ofsAddr);
1840 spin_unlock_irq(&moxafunc_lock);
1846 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1850 *dtrState = !!(port->lineCtrl & DTR_ON);
1852 *rtsState = !!(port->lineCtrl & RTS_ON);
1857 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1865 port->lineCtrl = mode;
1866 moxafunc(port->tableAddr, FC_LineControl, mode);
1869 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1870 int txflow, int rxflow, int txany)
1875 mode |= RTS_FlowCtl;
1877 mode |= CTS_FlowCtl;
1884 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
1887 static int MoxaPortLineStatus(struct moxa_port *port)
1889 void __iomem *ofsAddr;
1892 ofsAddr = port->tableAddr;
1893 if (MOXA_IS_320(port->board))
1894 val = moxafuncret(ofsAddr, FC_LineStatus, 0);
1896 val = readw(ofsAddr + FlagStat) >> 4;
1900 spin_lock_bh(&moxa_lock);
1901 moxa_new_dcdstate(port, val & 8);
1902 spin_unlock_bh(&moxa_lock);
1907 static int MoxaPortWriteData(struct tty_struct *tty,
1908 const unsigned char *buffer, int len)
1910 struct moxa_port *port = tty->driver_data;
1911 void __iomem *baseAddr, *ofsAddr, *ofs;
1912 unsigned int c, total;
1913 u16 head, tail, tx_mask, spage, epage;
1914 u16 pageno, pageofs, bufhead;
1916 ofsAddr = port->tableAddr;
1917 baseAddr = port->board->basemem;
1918 tx_mask = readw(ofsAddr + TX_mask);
1919 spage = readw(ofsAddr + Page_txb);
1920 epage = readw(ofsAddr + EndPage_txb);
1921 tail = readw(ofsAddr + TXwptr);
1922 head = readw(ofsAddr + TXrptr);
1923 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
1926 moxaLog.txcnt[port->port.tty->index] += c;
1928 if (spage == epage) {
1929 bufhead = readw(ofsAddr + Ofs_txb);
1930 writew(spage, baseAddr + Control_reg);
1933 len = head - tail - 1;
1935 len = tx_mask + 1 - tail;
1936 len = (c > len) ? len : c;
1937 ofs = baseAddr + DynPage_addr + bufhead + tail;
1938 memcpy_toio(ofs, buffer, len);
1940 tail = (tail + len) & tx_mask;
1944 pageno = spage + (tail >> 13);
1945 pageofs = tail & Page_mask;
1947 len = Page_size - pageofs;
1950 writeb(pageno, baseAddr + Control_reg);
1951 ofs = baseAddr + DynPage_addr + pageofs;
1952 memcpy_toio(ofs, buffer, len);
1954 if (++pageno == epage)
1959 tail = (tail + total) & tx_mask;
1961 writew(tail, ofsAddr + TXwptr);
1962 writeb(1, ofsAddr + CD180TXirq); /* start to send */
1966 static int MoxaPortReadData(struct moxa_port *port)
1968 struct tty_struct *tty = port->port.tty;
1970 void __iomem *baseAddr, *ofsAddr, *ofs;
1971 unsigned int count, len, total;
1972 u16 tail, rx_mask, spage, epage;
1973 u16 pageno, pageofs, bufhead, head;
1975 ofsAddr = port->tableAddr;
1976 baseAddr = port->board->basemem;
1977 head = readw(ofsAddr + RXrptr);
1978 tail = readw(ofsAddr + RXwptr);
1979 rx_mask = readw(ofsAddr + RX_mask);
1980 spage = readw(ofsAddr + Page_rxb);
1981 epage = readw(ofsAddr + EndPage_rxb);
1982 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
1987 moxaLog.rxcnt[tty->index] += total;
1988 if (spage == epage) {
1989 bufhead = readw(ofsAddr + Ofs_rxb);
1990 writew(spage, baseAddr + Control_reg);
1992 ofs = baseAddr + DynPage_addr + bufhead + head;
1993 len = (tail >= head) ? (tail - head) :
1994 (rx_mask + 1 - head);
1995 len = tty_prepare_flip_string(tty, &dst,
1997 memcpy_fromio(dst, ofs, len);
1998 head = (head + len) & rx_mask;
2002 pageno = spage + (head >> 13);
2003 pageofs = head & Page_mask;
2005 writew(pageno, baseAddr + Control_reg);
2006 ofs = baseAddr + DynPage_addr + pageofs;
2007 len = tty_prepare_flip_string(tty, &dst,
2008 min(Page_size - pageofs, count));
2009 memcpy_fromio(dst, ofs, len);
2012 pageofs = (pageofs + len) & Page_mask;
2013 if (pageofs == 0 && ++pageno == epage)
2016 head = (head + total) & rx_mask;
2018 writew(head, ofsAddr + RXrptr);
2019 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2020 moxaLowWaterChk = 1;
2021 port->lowChkFlag = 1;
2027 static int MoxaPortTxQueue(struct moxa_port *port)
2029 void __iomem *ofsAddr = port->tableAddr;
2030 u16 rptr, wptr, mask;
2032 rptr = readw(ofsAddr + TXrptr);
2033 wptr = readw(ofsAddr + TXwptr);
2034 mask = readw(ofsAddr + TX_mask);
2035 return (wptr - rptr) & mask;
2038 static int MoxaPortTxFree(struct moxa_port *port)
2040 void __iomem *ofsAddr = port->tableAddr;
2041 u16 rptr, wptr, mask;
2043 rptr = readw(ofsAddr + TXrptr);
2044 wptr = readw(ofsAddr + TXwptr);
2045 mask = readw(ofsAddr + TX_mask);
2046 return mask - ((wptr - rptr) & mask);
2049 static int MoxaPortRxQueue(struct moxa_port *port)
2051 void __iomem *ofsAddr = port->tableAddr;
2052 u16 rptr, wptr, mask;
2054 rptr = readw(ofsAddr + RXrptr);
2055 wptr = readw(ofsAddr + RXwptr);
2056 mask = readw(ofsAddr + RX_mask);
2057 return (wptr - rptr) & mask;
2060 static void MoxaPortTxDisable(struct moxa_port *port)
2062 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2065 static void MoxaPortTxEnable(struct moxa_port *port)
2067 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2070 static int moxa_get_serial_info(struct moxa_port *info,
2071 struct serial_struct __user *retinfo)
2073 struct serial_struct tmp = {
2075 .line = info->port.tty->index,
2076 .flags = info->port.flags,
2077 .baud_base = 921600,
2078 .close_delay = info->port.close_delay
2080 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2084 static int moxa_set_serial_info(struct moxa_port *info,
2085 struct serial_struct __user *new_info)
2087 struct serial_struct new_serial;
2089 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2092 if (new_serial.irq != 0 || new_serial.port != 0 ||
2093 new_serial.custom_divisor != 0 ||
2094 new_serial.baud_base != 921600)
2097 if (!capable(CAP_SYS_ADMIN)) {
2098 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2099 (info->port.flags & ~ASYNC_USR_MASK)))
2102 info->port.close_delay = new_serial.close_delay * HZ / 100;
2104 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2105 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
2107 MoxaSetFifo(info, new_serial.type == PORT_16550A);
2109 info->type = new_serial.type;
2115 /*****************************************************************************
2116 * Static local functions: *
2117 *****************************************************************************/
2119 static void MoxaSetFifo(struct moxa_port *port, int enable)
2121 void __iomem *ofsAddr = port->tableAddr;
2124 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2125 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2127 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2128 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);