1 /*****************************************************************************/
3 * moxa.c -- MOXA Intellio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
17 * MOXA Intellio Series Driver
23 #include <linux/module.h>
24 #include <linux/types.h>
26 #include <linux/ioport.h>
27 #include <linux/errno.h>
28 #include <linux/firmware.h>
29 #include <linux/signal.h>
30 #include <linux/sched.h>
31 #include <linux/timer.h>
32 #include <linux/interrupt.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
35 #include <linux/major.h>
36 #include <linux/string.h>
37 #include <linux/fcntl.h>
38 #include <linux/ptrace.h>
39 #include <linux/serial.h>
40 #include <linux/tty_driver.h>
41 #include <linux/delay.h>
42 #include <linux/pci.h>
43 #include <linux/init.h>
44 #include <linux/bitops.h>
45 #include <linux/completion.h>
47 #include <asm/system.h>
49 #include <asm/uaccess.h>
53 #define MOXA_VERSION "5.1k"
55 #define MOXA_FW_HDRLEN 32
58 #define MOXACUMAJOR 173
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)
65 * Define the Moxa PCI vendor and device IDs.
67 #define MOXA_BUS_TYPE_ISA 0
68 #define MOXA_BUS_TYPE_PCI 1
71 MOXA_BOARD_C218_PCI = 1,
78 static char *moxa_brdname[] =
80 "C218 Turbo PCI series",
81 "C218 Turbo ISA series",
82 "C320 Turbo PCI series",
83 "C320 Turbo ISA series",
88 static struct pci_device_id moxa_pcibrds[] = {
89 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
90 .driver_data = MOXA_BOARD_C218_PCI },
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
92 .driver_data = MOXA_BOARD_C320_PCI },
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
94 .driver_data = MOXA_BOARD_CP204J },
97 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
98 #endif /* CONFIG_PCI */
102 static struct moxa_board_conf {
109 struct moxa_port *ports;
111 void __iomem *basemem;
112 void __iomem *intNdx;
113 void __iomem *intPend;
114 void __iomem *intTable;
115 } moxa_boards[MAX_BOARDS];
117 struct mxser_mstatus {
131 struct moxa_board_conf *board;
132 struct tty_struct *tty;
133 void __iomem *tableAddr;
141 unsigned long statusflags;
142 wait_queue_head_t open_wait;
143 struct completion close_wait;
152 int rxcnt[MAX_PORTS];
153 int txcnt[MAX_PORTS];
157 #define TXSTOPPED 0x1
159 #define EMPTYWAIT 0x4
162 #define SERIAL_DO_RESTART
164 #define WAKEUP_CHARS 256
166 static int ttymajor = MOXAMAJOR;
167 static struct mon_str moxaLog;
168 static unsigned int moxaFuncTout = HZ / 2;
169 static unsigned int moxaLowWaterChk;
170 /* Variables for insmod */
172 static unsigned long baseaddr[MAX_BOARDS];
173 static unsigned int type[MAX_BOARDS];
174 static unsigned int numports[MAX_BOARDS];
177 MODULE_AUTHOR("William Chen");
178 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
179 MODULE_LICENSE("GPL");
181 module_param_array(type, uint, NULL, 0);
182 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
183 module_param_array(baseaddr, ulong, NULL, 0);
184 MODULE_PARM_DESC(baseaddr, "base address");
185 module_param_array(numports, uint, NULL, 0);
186 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
188 module_param(ttymajor, int, 0);
193 static int moxa_open(struct tty_struct *, struct file *);
194 static void moxa_close(struct tty_struct *, struct file *);
195 static int moxa_write(struct tty_struct *, const unsigned char *, int);
196 static int moxa_write_room(struct tty_struct *);
197 static void moxa_flush_buffer(struct tty_struct *);
198 static int moxa_chars_in_buffer(struct tty_struct *);
199 static void moxa_flush_chars(struct tty_struct *);
200 static void moxa_put_char(struct tty_struct *, unsigned char);
201 static void moxa_throttle(struct tty_struct *);
202 static void moxa_unthrottle(struct tty_struct *);
203 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
204 static void moxa_stop(struct tty_struct *);
205 static void moxa_start(struct tty_struct *);
206 static void moxa_hangup(struct tty_struct *);
207 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
208 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
209 unsigned int set, unsigned int clear);
210 static void moxa_poll(unsigned long);
211 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
212 static int moxa_block_till_ready(struct tty_struct *, struct file *,
214 static void moxa_setup_empty_event(struct tty_struct *);
215 static void moxa_shut_down(struct moxa_port *);
217 * moxa board interface functions:
219 static void MoxaPortEnable(struct moxa_port *);
220 static void MoxaPortDisable(struct moxa_port *);
221 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
222 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
223 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
224 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
225 static int MoxaPortLineStatus(struct moxa_port *);
226 static void MoxaPortFlushData(struct moxa_port *, int);
227 static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int);
228 static int MoxaPortReadData(struct moxa_port *);
229 static int MoxaPortTxQueue(struct moxa_port *);
230 static int MoxaPortRxQueue(struct moxa_port *);
231 static int MoxaPortTxFree(struct moxa_port *);
232 static void MoxaPortTxDisable(struct moxa_port *);
233 static void MoxaPortTxEnable(struct moxa_port *);
234 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
235 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
236 static void MoxaSetFifo(struct moxa_port *port, int enable);
242 static void moxa_wait_finish(void __iomem *ofsAddr)
244 unsigned long end = jiffies + moxaFuncTout;
246 while (readw(ofsAddr + FuncCode) != 0)
247 if (time_after(jiffies, end))
249 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
250 printk(KERN_WARNING "moxa function expired\n");
253 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
255 writew(arg, ofsAddr + FuncArg);
256 writew(cmd, ofsAddr + FuncCode);
257 moxa_wait_finish(ofsAddr);
260 static void moxa_low_water_check(void __iomem *ofsAddr)
262 u16 rptr, wptr, mask, len;
264 if (readb(ofsAddr + FlagStat) & Xoff_state) {
265 rptr = readw(ofsAddr + RXrptr);
266 wptr = readw(ofsAddr + RXwptr);
267 mask = readw(ofsAddr + RX_mask);
268 len = (wptr - rptr) & mask;
269 if (len <= Low_water)
270 moxafunc(ofsAddr, FC_SendXon, 0);
278 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
279 unsigned int cmd, unsigned long arg)
281 struct moxa_port *ch = tty->driver_data;
282 void __user *argp = (void __user *)arg;
285 if (tty->index == MAX_PORTS) {
286 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
287 cmd != MOXA_GETMSTATUS)
293 case MOXA_GETDATACOUNT:
294 moxaLog.tick = jiffies;
295 return copy_to_user(argp, &moxaLog, sizeof(moxaLog)) ?
297 case MOXA_FLUSH_QUEUE:
298 MoxaPortFlushData(ch, arg);
300 case MOXA_GET_IOQUEUE: {
301 struct moxaq_str __user *argm = argp;
302 struct moxaq_str tmp;
306 for (i = 0; i < MAX_BOARDS; i++) {
307 p = moxa_boards[i].ports;
308 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
309 memset(&tmp, 0, sizeof(tmp));
310 if (moxa_boards[i].ready) {
311 tmp.inq = MoxaPortRxQueue(p);
312 tmp.outq = MoxaPortTxQueue(p);
314 if (copy_to_user(argm, &tmp, sizeof(tmp)))
319 } case MOXA_GET_OQUEUE:
320 status = MoxaPortTxQueue(ch);
321 return put_user(status, (unsigned long __user *)argp);
322 case MOXA_GET_IQUEUE:
323 status = MoxaPortRxQueue(ch);
324 return put_user(status, (unsigned long __user *)argp);
325 case MOXA_GETMSTATUS: {
326 struct mxser_mstatus __user *argm = argp;
327 struct mxser_mstatus tmp;
331 for (i = 0; i < MAX_BOARDS; i++) {
332 p = moxa_boards[i].ports;
333 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
334 memset(&tmp, 0, sizeof(tmp));
335 if (!moxa_boards[i].ready)
338 status = MoxaPortLineStatus(p);
346 if (!p->tty || !p->tty->termios)
347 tmp.cflag = p->cflag;
349 tmp.cflag = p->tty->termios->c_cflag;
351 if (copy_to_user(argm, &tmp, sizeof(tmp)))
358 return moxa_get_serial_info(ch, argp);
360 return moxa_set_serial_info(ch, argp);
365 static void moxa_break_ctl(struct tty_struct *tty, int state)
367 struct moxa_port *port = tty->driver_data;
369 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
373 static const struct tty_operations moxa_ops = {
377 .write_room = moxa_write_room,
378 .flush_buffer = moxa_flush_buffer,
379 .chars_in_buffer = moxa_chars_in_buffer,
380 .flush_chars = moxa_flush_chars,
381 .put_char = moxa_put_char,
383 .throttle = moxa_throttle,
384 .unthrottle = moxa_unthrottle,
385 .set_termios = moxa_set_termios,
388 .hangup = moxa_hangup,
389 .break_ctl = moxa_break_ctl,
390 .tiocmget = moxa_tiocmget,
391 .tiocmset = moxa_tiocmset,
394 static struct tty_driver *moxaDriver;
395 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
396 static DEFINE_SPINLOCK(moxa_lock);
402 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
404 switch (brd->boardType) {
405 case MOXA_BOARD_C218_ISA:
406 case MOXA_BOARD_C218_PCI:
410 case MOXA_BOARD_CP204J:
424 static int moxa_check_fw(const void *ptr)
426 const __le16 *lptr = ptr;
428 if (*lptr != cpu_to_le16(0x7980))
434 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
437 void __iomem *baseAddr = brd->basemem;
440 writeb(HW_reset, baseAddr + Control_reg); /* reset */
442 memset_io(baseAddr, 0, 4096);
443 memcpy_toio(baseAddr, buf, len); /* download BIOS */
444 writeb(0, baseAddr + Control_reg); /* restart */
448 switch (brd->boardType) {
449 case MOXA_BOARD_C218_ISA:
450 case MOXA_BOARD_C218_PCI:
451 tmp = readw(baseAddr + C218_key);
452 if (tmp != C218_KeyCode)
455 case MOXA_BOARD_CP204J:
456 tmp = readw(baseAddr + C218_key);
457 if (tmp != CP204J_KeyCode)
461 tmp = readw(baseAddr + C320_key);
462 if (tmp != C320_KeyCode)
464 tmp = readw(baseAddr + C320_status);
465 if (tmp != STS_init) {
466 printk(KERN_ERR "moxa: bios upload failed -- CPU/Basic "
467 "module not found\n");
475 printk(KERN_ERR "moxa: bios upload failed -- board not found\n");
479 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
482 void __iomem *baseAddr = brd->basemem;
485 printk(KERN_ERR "moxa: invalid 320 bios -- too short\n");
489 writew(len - 7168 - 2, baseAddr + C320bapi_len);
490 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
491 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
492 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
493 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
498 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
501 void __iomem *baseAddr = brd->basemem;
502 const u16 *uptr = ptr;
503 size_t wlen, len2, j;
504 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
505 unsigned int i, retry, c320;
508 c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
509 brd->boardType == MOXA_BOARD_C320_ISA;
510 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
513 switch (brd->boardType) {
514 case MOXA_BOARD_CP204J:
515 case MOXA_BOARD_C218_ISA:
516 case MOXA_BOARD_C218_PCI:
518 loadbuf = C218_LoadBuf;
519 loadlen = C218DLoad_len;
520 checksum = C218check_sum;
521 checksum_ok = C218chksum_ok;
525 keycode = C320_KeyCode;
526 loadbuf = C320_LoadBuf;
527 loadlen = C320DLoad_len;
528 checksum = C320check_sum;
529 checksum_ok = C320chksum_ok;
535 for (i = 0; i < wlen; i++)
536 usum += le16_to_cpu(uptr[i]);
542 len2 = (wlen > 2048) ? 2048 : wlen;
544 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
547 writew(len2, baseAddr + loadlen);
548 writew(0, baseAddr + key);
549 for (i = 0; i < 100; i++) {
550 if (readw(baseAddr + key) == keycode)
554 if (readw(baseAddr + key) != keycode)
557 writew(0, baseAddr + loadlen);
558 writew(usum, baseAddr + checksum);
559 writew(0, baseAddr + key);
560 for (i = 0; i < 100; i++) {
561 if (readw(baseAddr + key) == keycode)
566 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
567 if (readb(baseAddr + checksum_ok) != 1)
570 writew(0, baseAddr + key);
571 for (i = 0; i < 600; i++) {
572 if (readw(baseAddr + Magic_no) == Magic_code)
576 if (readw(baseAddr + Magic_no) != Magic_code)
580 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
581 writew(0x3800, baseAddr + TMS320_PORT1);
582 writew(0x3900, baseAddr + TMS320_PORT2);
583 writew(28499, baseAddr + TMS320_CLOCK);
585 writew(0x3200, baseAddr + TMS320_PORT1);
586 writew(0x3400, baseAddr + TMS320_PORT2);
587 writew(19999, baseAddr + TMS320_CLOCK);
590 writew(1, baseAddr + Disable_IRQ);
591 writew(0, baseAddr + Magic_no);
592 for (i = 0; i < 500; i++) {
593 if (readw(baseAddr + Magic_no) == Magic_code)
597 if (readw(baseAddr + Magic_no) != Magic_code)
601 j = readw(baseAddr + Module_cnt);
604 brd->numPorts = j * 8;
605 writew(j, baseAddr + Module_no);
606 writew(0, baseAddr + Magic_no);
607 for (i = 0; i < 600; i++) {
608 if (readw(baseAddr + Magic_no) == Magic_code)
612 if (readw(baseAddr + Magic_no) != Magic_code)
615 brd->intNdx = baseAddr + IRQindex;
616 brd->intPend = baseAddr + IRQpending;
617 brd->intTable = baseAddr + IRQtable;
622 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
625 void __iomem *ofsAddr, *baseAddr = brd->basemem;
626 struct moxa_port *port;
630 printk(KERN_ERR "moxa: bios length is not even\n");
634 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
638 switch (brd->boardType) {
639 case MOXA_BOARD_C218_ISA:
640 case MOXA_BOARD_C218_PCI:
641 case MOXA_BOARD_CP204J:
643 for (i = 0; i < brd->numPorts; i++, port++) {
646 port->tableAddr = baseAddr + Extern_table +
648 ofsAddr = port->tableAddr;
649 writew(C218rx_mask, ofsAddr + RX_mask);
650 writew(C218tx_mask, ofsAddr + TX_mask);
651 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
652 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
654 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
655 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
661 for (i = 0; i < brd->numPorts; i++, port++) {
664 port->tableAddr = baseAddr + Extern_table +
666 ofsAddr = port->tableAddr;
667 switch (brd->numPorts) {
669 writew(C320p8rx_mask, ofsAddr + RX_mask);
670 writew(C320p8tx_mask, ofsAddr + TX_mask);
671 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
672 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
673 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
674 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
678 writew(C320p16rx_mask, ofsAddr + RX_mask);
679 writew(C320p16tx_mask, ofsAddr + TX_mask);
680 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
681 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
682 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
683 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
687 writew(C320p24rx_mask, ofsAddr + RX_mask);
688 writew(C320p24tx_mask, ofsAddr + TX_mask);
689 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
690 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
691 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
692 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
695 writew(C320p32rx_mask, ofsAddr + RX_mask);
696 writew(C320p32tx_mask, ofsAddr + TX_mask);
697 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
698 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
699 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
700 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
701 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
710 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
712 void *ptr = fw->data;
716 unsigned int a, lenp, lencnt;
719 __le32 magic; /* 0x34303430 */
721 u8 type; /* UNIX = 3 */
722 u8 model; /* C218T=1, C320T=2, CP204=3 */
727 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
729 if (fw->size < MOXA_FW_HDRLEN) {
730 strcpy(rsn, "too short (even header won't fit)");
733 if (hdr->magic != cpu_to_le32(0x30343034)) {
734 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
737 if (hdr->type != 3) {
738 sprintf(rsn, "not for linux, type is %u", hdr->type);
741 if (moxa_check_fw_model(brd, hdr->model)) {
742 sprintf(rsn, "not for this card, model is %u", hdr->model);
746 len = MOXA_FW_HDRLEN;
747 lencnt = hdr->model == 2 ? 5 : 3;
748 for (a = 0; a < ARRAY_SIZE(lens); a++) {
749 lens[a] = le16_to_cpu(hdr->len[a]);
750 if (lens[a] && len + lens[a] <= fw->size &&
751 moxa_check_fw(&fw->data[len]))
752 printk(KERN_WARNING "moxa firmware: unexpected input "
753 "at offset %u, but going on\n", (u32)len);
754 if (!lens[a] && a < lencnt) {
755 sprintf(rsn, "too few entries in fw file");
761 if (len != fw->size) {
762 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
767 ptr += MOXA_FW_HDRLEN;
770 strcpy(rsn, "read above");
772 ret = moxa_load_bios(brd, ptr, lens[lenp]);
776 /* we skip the tty section (lens[1]), since we don't need it */
777 ptr += lens[lenp] + lens[lenp + 1];
778 lenp += 2; /* comm */
780 if (hdr->model == 2) {
781 ret = moxa_load_320b(brd, ptr, lens[lenp]);
784 /* skip another tty */
785 ptr += lens[lenp] + lens[lenp + 1];
789 ret = moxa_load_code(brd, ptr, lens[lenp]);
795 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
799 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
801 const struct firmware *fw;
807 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
809 if (brd->ports == NULL) {
810 printk(KERN_ERR "cannot allocate memory for ports\n");
815 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
816 p->type = PORT_16550A;
817 p->close_delay = 5 * HZ / 10;
818 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
819 init_waitqueue_head(&p->open_wait);
820 init_completion(&p->close_wait);
823 switch (brd->boardType) {
824 case MOXA_BOARD_C218_ISA:
825 case MOXA_BOARD_C218_PCI:
826 file = "c218tunx.cod";
828 case MOXA_BOARD_CP204J:
829 file = "cp204unx.cod";
832 file = "c320tunx.cod";
836 ret = request_firmware(&fw, file, dev);
838 printk(KERN_ERR "request_firmware failed\n");
842 ret = moxa_load_fw(brd, fw);
844 release_firmware(fw);
851 if (!timer_pending(&moxaTimer))
852 mod_timer(&moxaTimer, jiffies + HZ / 50);
861 static void moxa_board_deinit(struct moxa_board_conf *brd)
863 spin_lock_bh(&moxa_lock);
865 spin_unlock_bh(&moxa_lock);
866 iounmap(brd->basemem);
872 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
873 const struct pci_device_id *ent)
875 struct moxa_board_conf *board;
877 int board_type = ent->driver_data;
880 retval = pci_enable_device(pdev);
882 dev_err(&pdev->dev, "can't enable pci device\n");
886 for (i = 0; i < MAX_BOARDS; i++)
887 if (moxa_boards[i].basemem == NULL)
891 if (i >= MAX_BOARDS) {
892 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
893 "found. Board is ignored.\n", MAX_BOARDS);
897 board = &moxa_boards[i];
899 retval = pci_request_region(pdev, 2, "moxa-base");
901 dev_err(&pdev->dev, "can't request pci region 2\n");
905 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
906 if (board->basemem == NULL) {
907 dev_err(&pdev->dev, "can't remap io space 2\n");
911 board->boardType = board_type;
912 switch (board_type) {
913 case MOXA_BOARD_C218_ISA:
914 case MOXA_BOARD_C218_PCI:
918 case MOXA_BOARD_CP204J:
925 board->busType = MOXA_BUS_TYPE_PCI;
927 retval = moxa_init_board(board, &pdev->dev);
931 pci_set_drvdata(pdev, board);
935 iounmap(board->basemem);
936 board->basemem = NULL;
938 pci_release_region(pdev, 2);
943 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
945 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
947 moxa_board_deinit(brd);
949 pci_release_region(pdev, 2);
952 static struct pci_driver moxa_pci_driver = {
954 .id_table = moxa_pcibrds,
955 .probe = moxa_pci_probe,
956 .remove = __devexit_p(moxa_pci_remove)
958 #endif /* CONFIG_PCI */
960 static int __init moxa_init(void)
962 unsigned int isabrds = 0;
965 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
967 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
971 moxaDriver->owner = THIS_MODULE;
972 moxaDriver->name = "ttyMX";
973 moxaDriver->major = ttymajor;
974 moxaDriver->minor_start = 0;
975 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
976 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
977 moxaDriver->init_termios = tty_std_termios;
978 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
979 moxaDriver->init_termios.c_ispeed = 9600;
980 moxaDriver->init_termios.c_ospeed = 9600;
981 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
982 tty_set_operations(moxaDriver, &moxa_ops);
984 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
986 if (tty_register_driver(moxaDriver)) {
987 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
988 put_tty_driver(moxaDriver);
992 /* Find the boards defined from module args. */
995 struct moxa_board_conf *brd = moxa_boards;
997 for (i = 0; i < MAX_BOARDS; i++) {
1000 if (type[i] == MOXA_BOARD_C218_ISA ||
1001 type[i] == MOXA_BOARD_C320_ISA) {
1002 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1003 isabrds + 1, moxa_brdname[type[i] - 1],
1005 brd->boardType = type[i];
1006 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1008 brd->busType = MOXA_BUS_TYPE_ISA;
1009 brd->basemem = ioremap(baseaddr[i], 0x4000);
1010 if (!brd->basemem) {
1011 printk(KERN_ERR "moxa: can't remap %lx\n",
1015 if (moxa_init_board(brd, NULL)) {
1016 iounmap(brd->basemem);
1017 brd->basemem = NULL;
1029 retval = pci_register_driver(&moxa_pci_driver);
1031 printk(KERN_ERR "Can't register moxa pci driver!\n");
1040 static void __exit moxa_exit(void)
1044 del_timer_sync(&moxaTimer);
1046 if (tty_unregister_driver(moxaDriver))
1047 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1049 put_tty_driver(moxaDriver);
1052 pci_unregister_driver(&moxa_pci_driver);
1055 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1056 if (moxa_boards[i].ready)
1057 moxa_board_deinit(&moxa_boards[i]);
1060 module_init(moxa_init);
1061 module_exit(moxa_exit);
1063 static int moxa_open(struct tty_struct *tty, struct file *filp)
1065 struct moxa_board_conf *brd;
1066 struct moxa_port *ch;
1071 if (port == MAX_PORTS) {
1072 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1074 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1078 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1080 tty->driver_data = ch;
1082 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
1083 ch->statusflags = 0;
1084 moxa_set_tty_param(tty, tty->termios);
1085 MoxaPortLineCtrl(ch, 1, 1);
1087 ch->asyncflags |= ASYNC_INITIALIZED;
1089 retval = moxa_block_till_ready(tty, filp, ch);
1091 moxa_unthrottle(tty);
1093 if (ch->type == PORT_16550A) {
1102 static void moxa_close(struct tty_struct *tty, struct file *filp)
1104 struct moxa_port *ch;
1108 if (port == MAX_PORTS) {
1111 if (tty->driver_data == NULL) {
1114 if (tty_hung_up_p(filp)) {
1117 ch = (struct moxa_port *) tty->driver_data;
1119 if ((tty->count == 1) && (ch->count != 1)) {
1120 printk(KERN_WARNING "moxa_close: bad serial port count; "
1121 "tty->count is 1, ch->count is %d\n", ch->count);
1124 if (--ch->count < 0) {
1125 printk(KERN_WARNING "moxa_close: bad serial port count, "
1126 "device=%s\n", tty->name);
1132 ch->asyncflags |= ASYNC_CLOSING;
1134 ch->cflag = tty->termios->c_cflag;
1135 if (ch->asyncflags & ASYNC_INITIALIZED) {
1136 moxa_setup_empty_event(tty);
1137 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
1140 MoxaPortFlushData(ch, 2);
1142 if (tty->driver->flush_buffer)
1143 tty->driver->flush_buffer(tty);
1144 tty_ldisc_flush(tty);
1148 if (ch->blocked_open) {
1149 if (ch->close_delay) {
1150 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
1152 wake_up_interruptible(&ch->open_wait);
1154 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1155 complete_all(&ch->close_wait);
1158 static int moxa_write(struct tty_struct *tty,
1159 const unsigned char *buf, int count)
1161 struct moxa_port *ch = tty->driver_data;
1167 spin_lock_bh(&moxa_lock);
1168 len = MoxaPortWriteData(ch, buf, count);
1169 spin_unlock_bh(&moxa_lock);
1171 /*********************************************
1172 if ( !(ch->statusflags & LOWWAIT) &&
1173 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1174 ************************************************/
1175 ch->statusflags |= LOWWAIT;
1179 static int moxa_write_room(struct tty_struct *tty)
1181 struct moxa_port *ch;
1185 ch = tty->driver_data;
1188 return MoxaPortTxFree(ch);
1191 static void moxa_flush_buffer(struct tty_struct *tty)
1193 struct moxa_port *ch = tty->driver_data;
1197 MoxaPortFlushData(ch, 1);
1201 static int moxa_chars_in_buffer(struct tty_struct *tty)
1203 struct moxa_port *ch = tty->driver_data;
1207 * Sigh...I have to check if driver_data is NULL here, because
1208 * if an open() fails, the TTY subsystem eventually calls
1209 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1210 * routine. And since the open() failed, we return 0 here. TDJ
1214 chars = MoxaPortTxQueue(ch);
1217 * Make it possible to wakeup anything waiting for output
1218 * in tty_ioctl.c, etc.
1220 if (!(ch->statusflags & EMPTYWAIT))
1221 moxa_setup_empty_event(tty);
1226 static void moxa_flush_chars(struct tty_struct *tty)
1229 * Don't think I need this, because this is called to empty the TX
1230 * buffer for the 16450, 16550, etc.
1234 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
1236 struct moxa_port *ch = tty->driver_data;
1240 spin_lock_bh(&moxa_lock);
1241 MoxaPortWriteData(ch, &c, 1);
1242 spin_unlock_bh(&moxa_lock);
1243 /************************************************
1244 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1245 *************************************************/
1246 ch->statusflags |= LOWWAIT;
1249 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1251 struct moxa_port *ch = tty->driver_data;
1252 int flag = 0, dtr, rts;
1257 MoxaPortGetLineOut(ch, &dtr, &rts);
1262 dtr = MoxaPortLineStatus(ch);
1272 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1273 unsigned int set, unsigned int clear)
1275 struct moxa_port *ch = tty->driver_data;
1283 MoxaPortGetLineOut(ch, &dtr, &rts);
1284 if (set & TIOCM_RTS)
1286 if (set & TIOCM_DTR)
1288 if (clear & TIOCM_RTS)
1290 if (clear & TIOCM_DTR)
1292 MoxaPortLineCtrl(ch, dtr, rts);
1296 static void moxa_throttle(struct tty_struct *tty)
1298 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1300 ch->statusflags |= THROTTLE;
1303 static void moxa_unthrottle(struct tty_struct *tty)
1305 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1307 ch->statusflags &= ~THROTTLE;
1310 static void moxa_set_termios(struct tty_struct *tty,
1311 struct ktermios *old_termios)
1313 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1317 moxa_set_tty_param(tty, old_termios);
1318 if (!(old_termios->c_cflag & CLOCAL) &&
1319 (tty->termios->c_cflag & CLOCAL))
1320 wake_up_interruptible(&ch->open_wait);
1323 static void moxa_stop(struct tty_struct *tty)
1325 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1329 MoxaPortTxDisable(ch);
1330 ch->statusflags |= TXSTOPPED;
1334 static void moxa_start(struct tty_struct *tty)
1336 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1341 if (!(ch->statusflags & TXSTOPPED))
1344 MoxaPortTxEnable(ch);
1345 ch->statusflags &= ~TXSTOPPED;
1348 static void moxa_hangup(struct tty_struct *tty)
1350 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1352 moxa_flush_buffer(tty);
1355 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1357 wake_up_interruptible(&ch->open_wait);
1360 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1364 if ((dcd != p->DCDState) && p->tty && C_CLOCAL(p->tty)) {
1367 p->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1369 wake_up_interruptible(&p->open_wait);
1374 static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1377 struct tty_struct *tty = p->tty;
1378 void __iomem *ofsAddr;
1379 unsigned int inited = p->asyncflags & ASYNC_INITIALIZED;
1383 if ((p->statusflags & EMPTYWAIT) &&
1384 MoxaPortTxQueue(p) == 0) {
1385 p->statusflags &= ~EMPTYWAIT;
1388 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1389 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1390 p->statusflags &= ~LOWWAIT;
1394 if (inited && !(p->statusflags & THROTTLE) &&
1395 MoxaPortRxQueue(p) > 0) { /* RX */
1396 MoxaPortReadData(p);
1397 tty_schedule_flip(tty);
1400 p->statusflags &= ~EMPTYWAIT;
1401 MoxaPortFlushData(p, 0); /* flush RX */
1404 if (!handle) /* nothing else to do */
1407 intr = readw(ip); /* port irq status */
1411 writew(0, ip); /* ACK port */
1412 ofsAddr = p->tableAddr;
1413 if (intr & IntrTx) /* disable tx intr */
1414 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1415 ofsAddr + HostStat);
1420 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1421 tty_insert_flip_char(tty, 0, TTY_BREAK);
1422 tty_schedule_flip(tty);
1425 if (intr & IntrLine)
1426 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1431 static void moxa_poll(unsigned long ignored)
1433 struct moxa_board_conf *brd;
1435 unsigned int card, port;
1437 spin_lock(&moxa_lock);
1438 for (card = 0; card < MAX_BOARDS; card++) {
1439 brd = &moxa_boards[card];
1444 if (readb(brd->intPend) == 0xff)
1445 ip = brd->intTable + readb(brd->intNdx);
1447 for (port = 0; port < brd->numPorts; port++)
1448 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1451 writeb(0, brd->intPend); /* ACK */
1453 if (moxaLowWaterChk) {
1454 struct moxa_port *p = brd->ports;
1455 for (port = 0; port < brd->numPorts; port++, p++)
1456 if (p->lowChkFlag) {
1458 moxa_low_water_check(p->tableAddr);
1462 moxaLowWaterChk = 0;
1463 spin_unlock(&moxa_lock);
1465 mod_timer(&moxaTimer, jiffies + HZ / 50);
1468 /******************************************************************************/
1470 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1472 register struct ktermios *ts;
1473 struct moxa_port *ch;
1474 int rts, cts, txflow, rxflow, xany, baud;
1476 ch = (struct moxa_port *) tty->driver_data;
1478 rts = cts = txflow = rxflow = xany = 0;
1479 if (ts->c_cflag & CRTSCTS)
1481 if (ts->c_iflag & IXON)
1483 if (ts->c_iflag & IXOFF)
1485 if (ts->c_iflag & IXANY)
1488 /* Clear the features we don't support */
1489 ts->c_cflag &= ~CMSPAR;
1490 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1491 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1493 baud = tty_termios_baud_rate(old_termios);
1494 /* Not put the baud rate into the termios data */
1495 tty_encode_baud_rate(tty, baud, baud);
1498 static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1499 struct moxa_port *ch)
1501 DECLARE_WAITQUEUE(wait,current);
1503 int do_clocal = C_CLOCAL(tty);
1506 * If the device is in the middle of being closed, then block
1507 * until it's done, and then try again.
1509 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1510 if (ch->asyncflags & ASYNC_CLOSING)
1511 wait_for_completion_interruptible(&ch->close_wait);
1512 #ifdef SERIAL_DO_RESTART
1513 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1516 return (-ERESTARTSYS);
1522 * If non-blocking mode is set, then make the check up front
1525 if (filp->f_flags & O_NONBLOCK) {
1526 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1530 * Block waiting for the carrier detect and the line to become free
1533 add_wait_queue(&ch->open_wait, &wait);
1534 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
1535 tty->index, ch->count);
1536 spin_lock_bh(&moxa_lock);
1537 if (!tty_hung_up_p(filp))
1540 spin_unlock_bh(&moxa_lock);
1543 set_current_state(TASK_INTERRUPTIBLE);
1544 if (tty_hung_up_p(filp) ||
1545 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1546 #ifdef SERIAL_DO_RESTART
1547 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1550 retval = -ERESTARTSYS;
1556 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1560 if (signal_pending(current)) {
1561 retval = -ERESTARTSYS;
1566 set_current_state(TASK_RUNNING);
1567 remove_wait_queue(&ch->open_wait, &wait);
1569 spin_lock_bh(&moxa_lock);
1570 if (!tty_hung_up_p(filp))
1573 spin_unlock_bh(&moxa_lock);
1574 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
1575 tty->index, ch->count);
1578 /* FIXME: review to see if we need to use set_bit on these */
1579 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1583 static void moxa_setup_empty_event(struct tty_struct *tty)
1585 struct moxa_port *ch = tty->driver_data;
1587 spin_lock_bh(&moxa_lock);
1588 ch->statusflags |= EMPTYWAIT;
1589 spin_unlock_bh(&moxa_lock);
1592 static void moxa_shut_down(struct moxa_port *ch)
1594 struct tty_struct *tp;
1596 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1601 MoxaPortDisable(ch);
1604 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1606 if (tp->termios->c_cflag & HUPCL)
1607 MoxaPortLineCtrl(ch, 0, 0);
1609 ch->asyncflags &= ~ASYNC_INITIALIZED;
1612 /*****************************************************************************
1613 * Driver level functions: *
1614 *****************************************************************************/
1616 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1618 void __iomem *ofsAddr;
1619 if ((mode < 0) || (mode > 2))
1621 ofsAddr = port->tableAddr;
1622 moxafunc(ofsAddr, FC_FlushQueue, mode);
1624 port->lowChkFlag = 0;
1625 moxa_low_water_check(ofsAddr);
1629 /*****************************************************************************
1630 * Port level functions: *
1631 * 2. MoxaPortEnable(int port); *
1632 * 3. MoxaPortDisable(int port); *
1633 * 4. MoxaPortGetMaxBaud(int port); *
1634 * 6. MoxaPortSetBaud(int port, long baud); *
1635 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1636 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1637 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1638 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1639 * 12. MoxaPortLineStatus(int port); *
1640 * 15. MoxaPortFlushData(int port, int mode); *
1641 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1642 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1643 * 20. MoxaPortTxQueue(int port); *
1644 * 21. MoxaPortTxFree(int port); *
1645 * 22. MoxaPortRxQueue(int port); *
1646 * 24. MoxaPortTxDisable(int port); *
1647 * 25. MoxaPortTxEnable(int port); *
1648 * 27. MoxaPortResetBrkCnt(int port); *
1649 *****************************************************************************/
1651 * Moxa Port Number Description:
1653 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1654 * the port number using in MOXA driver functions will be 0 to 31 for
1655 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1656 * to 127 for fourth. For example, if you setup three MOXA boards,
1657 * first board is C218, second board is C320-16 and third board is
1658 * C320-32. The port number of first board (C218 - 8 ports) is from
1659 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1660 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1661 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1662 * 127 will be invalid.
1665 * Moxa Functions Description:
1667 * Function 1: Driver initialization routine, this routine must be
1668 * called when initialized driver.
1670 * void MoxaDriverInit();
1673 * Function 2: Moxa driver private IOCTL command processing.
1675 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1677 * unsigned int cmd : IOCTL command
1678 * unsigned long arg : IOCTL argument
1679 * int port : port number (0 - 127)
1686 * Function 6: Enable this port to start Tx/Rx data.
1688 * void MoxaPortEnable(int port);
1689 * int port : port number (0 - 127)
1692 * Function 7: Disable this port
1694 * void MoxaPortDisable(int port);
1695 * int port : port number (0 - 127)
1698 * Function 8: Get the maximun available baud rate of this port.
1700 * long MoxaPortGetMaxBaud(int port);
1701 * int port : port number (0 - 127)
1703 * return: 0 : this port is invalid
1704 * 38400/57600/115200 bps
1707 * Function 10: Setting baud rate of this port.
1709 * long MoxaPortSetBaud(int port, long baud);
1710 * int port : port number (0 - 127)
1711 * long baud : baud rate (50 - 115200)
1713 * return: 0 : this port is invalid or baud < 50
1714 * 50 - 115200 : the real baud rate set to the port, if
1715 * the argument baud is large than maximun
1716 * available baud rate, the real setting
1717 * baud rate will be the maximun baud rate.
1720 * Function 12: Configure the port.
1722 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1723 * int port : port number (0 - 127)
1724 * struct ktermios * termio : termio structure pointer
1725 * speed_t baud : baud rate
1727 * return: -1 : this port is invalid or termio == NULL
1731 * Function 13: Get the DTR/RTS state of this port.
1733 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1734 * int port : port number (0 - 127)
1735 * int * dtrState : pointer to INT to receive the current DTR
1736 * state. (if NULL, this function will not
1737 * write to this address)
1738 * int * rtsState : pointer to INT to receive the current RTS
1739 * state. (if NULL, this function will not
1740 * write to this address)
1742 * return: -1 : this port is invalid
1746 * Function 14: Setting the DTR/RTS output state of this port.
1748 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1749 * int port : port number (0 - 127)
1750 * int dtrState : DTR output state (0: off, 1: on)
1751 * int rtsState : RTS output state (0: off, 1: on)
1754 * Function 15: Setting the flow control of this port.
1756 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1757 * int txFlow,int xany);
1758 * int port : port number (0 - 127)
1759 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1760 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1761 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1762 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1763 * int xany : S/W XANY flow control (0: no, 1: yes)
1766 * Function 16: Get ths line status of this port
1768 * int MoxaPortLineStatus(int port);
1769 * int port : port number (0 - 127)
1771 * return: Bit 0 - CTS state (0: off, 1: on)
1772 * Bit 1 - DSR state (0: off, 1: on)
1773 * Bit 2 - DCD state (0: off, 1: on)
1776 * Function 19: Flush the Rx/Tx buffer data of this port.
1778 * void MoxaPortFlushData(int port, int mode);
1779 * int port : port number (0 - 127)
1781 * 0 : flush the Rx buffer
1782 * 1 : flush the Tx buffer
1783 * 2 : flush the Rx and Tx buffer
1786 * Function 20: Write data.
1788 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1789 * int port : port number (0 - 127)
1790 * unsigned char * buffer : pointer to write data buffer.
1791 * int length : write data length
1793 * return: 0 - length : real write data length
1796 * Function 21: Read data.
1798 * int MoxaPortReadData(int port, struct tty_struct *tty);
1799 * int port : port number (0 - 127)
1800 * struct tty_struct *tty : tty for data
1802 * return: 0 - length : real read data length
1805 * Function 24: Get the Tx buffer current queued data bytes
1807 * int MoxaPortTxQueue(int port);
1808 * int port : port number (0 - 127)
1810 * return: .. : Tx buffer current queued data bytes
1813 * Function 25: Get the Tx buffer current free space
1815 * int MoxaPortTxFree(int port);
1816 * int port : port number (0 - 127)
1818 * return: .. : Tx buffer current free space
1821 * Function 26: Get the Rx buffer current queued data bytes
1823 * int MoxaPortRxQueue(int port);
1824 * int port : port number (0 - 127)
1826 * return: .. : Rx buffer current queued data bytes
1829 * Function 28: Disable port data transmission.
1831 * void MoxaPortTxDisable(int port);
1832 * int port : port number (0 - 127)
1835 * Function 29: Enable port data transmission.
1837 * void MoxaPortTxEnable(int port);
1838 * int port : port number (0 - 127)
1841 * Function 31: Get the received BREAK signal count and reset it.
1843 * int MoxaPortResetBrkCnt(int port);
1844 * int port : port number (0 - 127)
1846 * return: 0 - .. : BREAK signal count
1851 static void MoxaPortEnable(struct moxa_port *port)
1853 void __iomem *ofsAddr;
1854 short lowwater = 512;
1856 ofsAddr = port->tableAddr;
1857 writew(lowwater, ofsAddr + Low_water);
1858 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1859 port->board->boardType == MOXA_BOARD_C320_PCI) {
1860 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1862 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1865 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1866 moxafunc(ofsAddr, FC_FlushQueue, 2);
1868 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1869 MoxaPortLineStatus(port);
1872 static void MoxaPortDisable(struct moxa_port *port)
1874 void __iomem *ofsAddr = port->tableAddr;
1876 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1877 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1878 writew(0, ofsAddr + HostStat);
1879 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1882 static long MoxaPortGetMaxBaud(struct moxa_port *port)
1884 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1885 port->board->boardType == MOXA_BOARD_C320_PCI)
1892 static long MoxaPortSetBaud(struct moxa_port *port, long baud)
1894 void __iomem *ofsAddr;
1898 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
1900 ofsAddr = port->tableAddr;
1904 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1905 else if (max == 57600L)
1906 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1908 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1910 moxafunc(ofsAddr, FC_SetBaud, val);
1915 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1918 void __iomem *ofsAddr;
1922 ofsAddr = port->tableAddr;
1923 cflag = termio->c_cflag; /* termio->c_cflag */
1925 mode = termio->c_cflag & CSIZE;
1928 else if (mode == CS6)
1930 else if (mode == CS7)
1932 else if (mode == CS8)
1935 if (termio->c_cflag & CSTOPB) {
1943 if (termio->c_cflag & PARENB) {
1944 if (termio->c_cflag & PARODD)
1951 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
1953 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1954 port->board->boardType == MOXA_BOARD_C320_PCI) {
1955 if (baud >= 921600L)
1958 baud = MoxaPortSetBaud(port, baud);
1960 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1961 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1962 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1963 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1964 moxa_wait_finish(ofsAddr);
1970 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1975 *dtrState = !!(port->lineCtrl & DTR_ON);
1977 *rtsState = !!(port->lineCtrl & RTS_ON);
1982 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1990 port->lineCtrl = mode;
1991 moxafunc(port->tableAddr, FC_LineControl, mode);
1994 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1995 int txflow, int rxflow, int txany)
2000 mode |= RTS_FlowCtl;
2002 mode |= CTS_FlowCtl;
2009 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
2012 static int MoxaPortLineStatus(struct moxa_port *port)
2014 void __iomem *ofsAddr;
2017 ofsAddr = port->tableAddr;
2018 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2019 port->board->boardType == MOXA_BOARD_C320_PCI) {
2020 moxafunc(ofsAddr, FC_LineStatus, 0);
2021 val = readw(ofsAddr + FuncArg);
2023 val = readw(ofsAddr + FlagStat) >> 4;
2028 moxa_new_dcdstate(port, val & 8);
2033 static int MoxaPortWriteData(struct moxa_port *port,
2034 const unsigned char *buffer, int len)
2036 void __iomem *baseAddr, *ofsAddr, *ofs;
2037 unsigned int c, total;
2038 u16 head, tail, tx_mask, spage, epage;
2039 u16 pageno, pageofs, bufhead;
2041 ofsAddr = port->tableAddr;
2042 baseAddr = port->board->basemem;
2043 tx_mask = readw(ofsAddr + TX_mask);
2044 spage = readw(ofsAddr + Page_txb);
2045 epage = readw(ofsAddr + EndPage_txb);
2046 tail = readw(ofsAddr + TXwptr);
2047 head = readw(ofsAddr + TXrptr);
2048 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
2051 moxaLog.txcnt[port->tty->index] += c;
2053 if (spage == epage) {
2054 bufhead = readw(ofsAddr + Ofs_txb);
2055 writew(spage, baseAddr + Control_reg);
2058 len = head - tail - 1;
2060 len = tx_mask + 1 - tail;
2061 len = (c > len) ? len : c;
2062 ofs = baseAddr + DynPage_addr + bufhead + tail;
2063 memcpy_toio(ofs, buffer, len);
2065 tail = (tail + len) & tx_mask;
2069 pageno = spage + (tail >> 13);
2070 pageofs = tail & Page_mask;
2072 len = Page_size - pageofs;
2075 writeb(pageno, baseAddr + Control_reg);
2076 ofs = baseAddr + DynPage_addr + pageofs;
2077 memcpy_toio(ofs, buffer, len);
2079 if (++pageno == epage)
2084 tail = (tail + total) & tx_mask;
2086 writew(tail, ofsAddr + TXwptr);
2087 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2091 static int MoxaPortReadData(struct moxa_port *port)
2093 struct tty_struct *tty = port->tty;
2095 void __iomem *baseAddr, *ofsAddr, *ofs;
2096 unsigned int count, len, total;
2097 u16 tail, rx_mask, spage, epage;
2098 u16 pageno, pageofs, bufhead, head;
2100 ofsAddr = port->tableAddr;
2101 baseAddr = port->board->basemem;
2102 head = readw(ofsAddr + RXrptr);
2103 tail = readw(ofsAddr + RXwptr);
2104 rx_mask = readw(ofsAddr + RX_mask);
2105 spage = readw(ofsAddr + Page_rxb);
2106 epage = readw(ofsAddr + EndPage_rxb);
2107 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
2112 moxaLog.rxcnt[tty->index] += total;
2113 if (spage == epage) {
2114 bufhead = readw(ofsAddr + Ofs_rxb);
2115 writew(spage, baseAddr + Control_reg);
2117 ofs = baseAddr + DynPage_addr + bufhead + head;
2118 len = (tail >= head) ? (tail - head) :
2119 (rx_mask + 1 - head);
2120 len = tty_prepare_flip_string(tty, &dst,
2122 memcpy_fromio(dst, ofs, len);
2123 head = (head + len) & rx_mask;
2127 pageno = spage + (head >> 13);
2128 pageofs = head & Page_mask;
2130 writew(pageno, baseAddr + Control_reg);
2131 ofs = baseAddr + DynPage_addr + pageofs;
2132 len = tty_prepare_flip_string(tty, &dst,
2133 min(Page_size - pageofs, count));
2134 memcpy_fromio(dst, ofs, len);
2137 pageofs = (pageofs + len) & Page_mask;
2138 if (pageofs == 0 && ++pageno == epage)
2141 head = (head + total) & rx_mask;
2143 writew(head, ofsAddr + RXrptr);
2144 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2145 moxaLowWaterChk = 1;
2146 port->lowChkFlag = 1;
2152 static int MoxaPortTxQueue(struct moxa_port *port)
2154 void __iomem *ofsAddr = port->tableAddr;
2155 u16 rptr, wptr, mask;
2157 rptr = readw(ofsAddr + TXrptr);
2158 wptr = readw(ofsAddr + TXwptr);
2159 mask = readw(ofsAddr + TX_mask);
2160 return (wptr - rptr) & mask;
2163 static int MoxaPortTxFree(struct moxa_port *port)
2165 void __iomem *ofsAddr = port->tableAddr;
2166 u16 rptr, wptr, mask;
2168 rptr = readw(ofsAddr + TXrptr);
2169 wptr = readw(ofsAddr + TXwptr);
2170 mask = readw(ofsAddr + TX_mask);
2171 return mask - ((wptr - rptr) & mask);
2174 static int MoxaPortRxQueue(struct moxa_port *port)
2176 void __iomem *ofsAddr = port->tableAddr;
2177 u16 rptr, wptr, mask;
2179 rptr = readw(ofsAddr + RXrptr);
2180 wptr = readw(ofsAddr + RXwptr);
2181 mask = readw(ofsAddr + RX_mask);
2182 return (wptr - rptr) & mask;
2185 static void MoxaPortTxDisable(struct moxa_port *port)
2187 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2190 static void MoxaPortTxEnable(struct moxa_port *port)
2192 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2195 static int moxa_get_serial_info(struct moxa_port *info,
2196 struct serial_struct __user *retinfo)
2198 struct serial_struct tmp;
2200 memset(&tmp, 0, sizeof(tmp));
2201 tmp.type = info->type;
2202 tmp.line = info->tty->index;
2205 tmp.flags = info->asyncflags;
2206 tmp.baud_base = 921600;
2207 tmp.close_delay = info->close_delay;
2208 tmp.custom_divisor = 0;
2210 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2216 static int moxa_set_serial_info(struct moxa_port *info,
2217 struct serial_struct __user *new_info)
2219 struct serial_struct new_serial;
2221 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2224 if ((new_serial.irq != 0) ||
2225 (new_serial.port != 0) ||
2226 // (new_serial.type != info->type) ||
2227 (new_serial.custom_divisor != 0) ||
2228 (new_serial.baud_base != 921600))
2231 if (!capable(CAP_SYS_ADMIN)) {
2232 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2233 (info->asyncflags & ~ASYNC_USR_MASK)))
2236 info->close_delay = new_serial.close_delay * HZ / 100;
2239 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2240 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2242 if (new_serial.type == PORT_16550A) {
2243 MoxaSetFifo(info, 1);
2245 MoxaSetFifo(info, 0);
2248 info->type = new_serial.type;
2254 /*****************************************************************************
2255 * Static local functions: *
2256 *****************************************************************************/
2258 static void MoxaSetFifo(struct moxa_port *port, int enable)
2260 void __iomem *ofsAddr = port->tableAddr;
2263 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2264 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2266 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2267 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);