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>
46 #include <asm/system.h>
48 #include <asm/uaccess.h>
52 #define MOXA_VERSION "5.1k"
54 #define MOXA_FW_HDRLEN 32
58 #define MAX_BOARDS 4 /* Don't change this value */
59 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
60 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
63 * Define the Moxa PCI vendor and device IDs.
65 #define MOXA_BUS_TYPE_ISA 0
66 #define MOXA_BUS_TYPE_PCI 1
69 MOXA_BOARD_C218_PCI = 1,
76 static char *moxa_brdname[] =
78 "C218 Turbo PCI series",
79 "C218 Turbo ISA series",
80 "C320 Turbo PCI series",
81 "C320 Turbo ISA series",
86 static struct pci_device_id moxa_pcibrds[] = {
87 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
88 .driver_data = MOXA_BOARD_C218_PCI },
89 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
90 .driver_data = MOXA_BOARD_C320_PCI },
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
92 .driver_data = MOXA_BOARD_CP204J },
95 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
96 #endif /* CONFIG_PCI */
100 static struct moxa_board_conf {
107 struct moxa_port *ports;
109 void __iomem *basemem;
110 void __iomem *intNdx;
111 void __iomem *intPend;
112 void __iomem *intTable;
113 } moxa_boards[MAX_BOARDS];
115 struct mxser_mstatus {
129 struct moxa_board_conf *board;
130 struct tty_struct *tty;
131 void __iomem *tableAddr;
138 unsigned long statusflags;
139 wait_queue_head_t open_wait;
148 int rxcnt[MAX_PORTS];
149 int txcnt[MAX_PORTS];
153 #define TXSTOPPED 0x1
155 #define EMPTYWAIT 0x4
158 #define SERIAL_DO_RESTART
160 #define WAKEUP_CHARS 256
162 static int ttymajor = MOXAMAJOR;
163 static struct mon_str moxaLog;
164 static unsigned int moxaFuncTout = HZ / 2;
165 static unsigned int moxaLowWaterChk;
166 static DEFINE_MUTEX(moxa_openlock);
167 /* Variables for insmod */
169 static unsigned long baseaddr[MAX_BOARDS];
170 static unsigned int type[MAX_BOARDS];
171 static unsigned int numports[MAX_BOARDS];
174 MODULE_AUTHOR("William Chen");
175 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
176 MODULE_LICENSE("GPL");
178 module_param_array(type, uint, NULL, 0);
179 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
180 module_param_array(baseaddr, ulong, NULL, 0);
181 MODULE_PARM_DESC(baseaddr, "base address");
182 module_param_array(numports, uint, NULL, 0);
183 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
185 module_param(ttymajor, int, 0);
190 static int moxa_open(struct tty_struct *, struct file *);
191 static void moxa_close(struct tty_struct *, struct file *);
192 static int moxa_write(struct tty_struct *, const unsigned char *, int);
193 static int moxa_write_room(struct tty_struct *);
194 static void moxa_flush_buffer(struct tty_struct *);
195 static int moxa_chars_in_buffer(struct tty_struct *);
196 static void moxa_throttle(struct tty_struct *);
197 static void moxa_unthrottle(struct tty_struct *);
198 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
199 static void moxa_stop(struct tty_struct *);
200 static void moxa_start(struct tty_struct *);
201 static void moxa_hangup(struct tty_struct *);
202 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
203 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
204 unsigned int set, unsigned int clear);
205 static void moxa_poll(unsigned long);
206 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
207 static void moxa_setup_empty_event(struct tty_struct *);
208 static void moxa_shut_down(struct moxa_port *);
210 * moxa board interface functions:
212 static void MoxaPortEnable(struct moxa_port *);
213 static void MoxaPortDisable(struct moxa_port *);
214 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
215 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
216 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
217 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
218 static int MoxaPortLineStatus(struct moxa_port *);
219 static void MoxaPortFlushData(struct moxa_port *, int);
220 static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int);
221 static int MoxaPortReadData(struct moxa_port *);
222 static int MoxaPortTxQueue(struct moxa_port *);
223 static int MoxaPortRxQueue(struct moxa_port *);
224 static int MoxaPortTxFree(struct moxa_port *);
225 static void MoxaPortTxDisable(struct moxa_port *);
226 static void MoxaPortTxEnable(struct moxa_port *);
227 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
228 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
229 static void MoxaSetFifo(struct moxa_port *port, int enable);
235 static void moxa_wait_finish(void __iomem *ofsAddr)
237 unsigned long end = jiffies + moxaFuncTout;
239 while (readw(ofsAddr + FuncCode) != 0)
240 if (time_after(jiffies, end))
242 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
243 printk(KERN_WARNING "moxa function expired\n");
246 static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
248 writew(arg, ofsAddr + FuncArg);
249 writew(cmd, ofsAddr + FuncCode);
250 moxa_wait_finish(ofsAddr);
253 static void moxa_low_water_check(void __iomem *ofsAddr)
255 u16 rptr, wptr, mask, len;
257 if (readb(ofsAddr + FlagStat) & Xoff_state) {
258 rptr = readw(ofsAddr + RXrptr);
259 wptr = readw(ofsAddr + RXwptr);
260 mask = readw(ofsAddr + RX_mask);
261 len = (wptr - rptr) & mask;
262 if (len <= Low_water)
263 moxafunc(ofsAddr, FC_SendXon, 0);
271 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
272 unsigned int cmd, unsigned long arg)
274 struct moxa_port *ch = tty->driver_data;
275 void __user *argp = (void __user *)arg;
278 if (tty->index == MAX_PORTS) {
279 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
280 cmd != MOXA_GETMSTATUS)
286 case MOXA_GETDATACOUNT:
287 moxaLog.tick = jiffies;
288 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
291 case MOXA_FLUSH_QUEUE:
292 MoxaPortFlushData(ch, arg);
294 case MOXA_GET_IOQUEUE: {
295 struct moxaq_str __user *argm = argp;
296 struct moxaq_str tmp;
300 mutex_lock(&moxa_openlock);
301 for (i = 0; i < MAX_BOARDS; i++) {
302 p = moxa_boards[i].ports;
303 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
304 memset(&tmp, 0, sizeof(tmp));
305 if (moxa_boards[i].ready) {
306 tmp.inq = MoxaPortRxQueue(p);
307 tmp.outq = MoxaPortTxQueue(p);
309 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
310 mutex_unlock(&moxa_openlock);
315 mutex_unlock(&moxa_openlock);
317 } case MOXA_GET_OQUEUE:
318 status = MoxaPortTxQueue(ch);
319 ret = put_user(status, (unsigned long __user *)argp);
321 case MOXA_GET_IQUEUE:
322 status = MoxaPortRxQueue(ch);
323 ret = put_user(status, (unsigned long __user *)argp);
325 case MOXA_GETMSTATUS: {
326 struct mxser_mstatus __user *argm = argp;
327 struct mxser_mstatus tmp;
331 mutex_lock(&moxa_openlock);
332 for (i = 0; i < MAX_BOARDS; i++) {
333 p = moxa_boards[i].ports;
334 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
335 memset(&tmp, 0, sizeof(tmp));
336 if (!moxa_boards[i].ready)
339 status = MoxaPortLineStatus(p);
347 if (!p->tty || !p->tty->termios)
348 tmp.cflag = p->cflag;
350 tmp.cflag = p->tty->termios->c_cflag;
352 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
353 mutex_unlock(&moxa_openlock);
358 mutex_unlock(&moxa_openlock);
362 mutex_lock(&moxa_openlock);
363 ret = moxa_get_serial_info(ch, argp);
364 mutex_unlock(&moxa_openlock);
367 mutex_lock(&moxa_openlock);
368 ret = moxa_set_serial_info(ch, argp);
369 mutex_unlock(&moxa_openlock);
377 static void moxa_break_ctl(struct tty_struct *tty, int state)
379 struct moxa_port *port = tty->driver_data;
381 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
385 static const struct tty_operations moxa_ops = {
389 .write_room = moxa_write_room,
390 .flush_buffer = moxa_flush_buffer,
391 .chars_in_buffer = moxa_chars_in_buffer,
393 .throttle = moxa_throttle,
394 .unthrottle = moxa_unthrottle,
395 .set_termios = moxa_set_termios,
398 .hangup = moxa_hangup,
399 .break_ctl = moxa_break_ctl,
400 .tiocmget = moxa_tiocmget,
401 .tiocmset = moxa_tiocmset,
404 static struct tty_driver *moxaDriver;
405 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
406 static DEFINE_SPINLOCK(moxa_lock);
412 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
414 switch (brd->boardType) {
415 case MOXA_BOARD_C218_ISA:
416 case MOXA_BOARD_C218_PCI:
420 case MOXA_BOARD_CP204J:
434 static int moxa_check_fw(const void *ptr)
436 const __le16 *lptr = ptr;
438 if (*lptr != cpu_to_le16(0x7980))
444 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
447 void __iomem *baseAddr = brd->basemem;
450 writeb(HW_reset, baseAddr + Control_reg); /* reset */
452 memset_io(baseAddr, 0, 4096);
453 memcpy_toio(baseAddr, buf, len); /* download BIOS */
454 writeb(0, baseAddr + Control_reg); /* restart */
458 switch (brd->boardType) {
459 case MOXA_BOARD_C218_ISA:
460 case MOXA_BOARD_C218_PCI:
461 tmp = readw(baseAddr + C218_key);
462 if (tmp != C218_KeyCode)
465 case MOXA_BOARD_CP204J:
466 tmp = readw(baseAddr + C218_key);
467 if (tmp != CP204J_KeyCode)
471 tmp = readw(baseAddr + C320_key);
472 if (tmp != C320_KeyCode)
474 tmp = readw(baseAddr + C320_status);
475 if (tmp != STS_init) {
476 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
477 "module not found\n");
485 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
489 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
492 void __iomem *baseAddr = brd->basemem;
495 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
499 writew(len - 7168 - 2, baseAddr + C320bapi_len);
500 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
501 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
502 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
503 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
508 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
511 void __iomem *baseAddr = brd->basemem;
512 const u16 *uptr = ptr;
513 size_t wlen, len2, j;
514 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
515 unsigned int i, retry, c320;
518 c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
519 brd->boardType == MOXA_BOARD_C320_ISA;
520 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
523 switch (brd->boardType) {
524 case MOXA_BOARD_CP204J:
525 case MOXA_BOARD_C218_ISA:
526 case MOXA_BOARD_C218_PCI:
528 loadbuf = C218_LoadBuf;
529 loadlen = C218DLoad_len;
530 checksum = C218check_sum;
531 checksum_ok = C218chksum_ok;
535 keycode = C320_KeyCode;
536 loadbuf = C320_LoadBuf;
537 loadlen = C320DLoad_len;
538 checksum = C320check_sum;
539 checksum_ok = C320chksum_ok;
545 for (i = 0; i < wlen; i++)
546 usum += le16_to_cpu(uptr[i]);
552 len2 = (wlen > 2048) ? 2048 : wlen;
554 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
557 writew(len2, baseAddr + loadlen);
558 writew(0, baseAddr + key);
559 for (i = 0; i < 100; i++) {
560 if (readw(baseAddr + key) == keycode)
564 if (readw(baseAddr + key) != keycode)
567 writew(0, baseAddr + loadlen);
568 writew(usum, baseAddr + checksum);
569 writew(0, baseAddr + key);
570 for (i = 0; i < 100; i++) {
571 if (readw(baseAddr + key) == keycode)
576 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
577 if (readb(baseAddr + checksum_ok) != 1)
580 writew(0, baseAddr + key);
581 for (i = 0; i < 600; i++) {
582 if (readw(baseAddr + Magic_no) == Magic_code)
586 if (readw(baseAddr + Magic_no) != Magic_code)
590 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
591 writew(0x3800, baseAddr + TMS320_PORT1);
592 writew(0x3900, baseAddr + TMS320_PORT2);
593 writew(28499, baseAddr + TMS320_CLOCK);
595 writew(0x3200, baseAddr + TMS320_PORT1);
596 writew(0x3400, baseAddr + TMS320_PORT2);
597 writew(19999, baseAddr + TMS320_CLOCK);
600 writew(1, baseAddr + Disable_IRQ);
601 writew(0, baseAddr + Magic_no);
602 for (i = 0; i < 500; i++) {
603 if (readw(baseAddr + Magic_no) == Magic_code)
607 if (readw(baseAddr + Magic_no) != Magic_code)
611 j = readw(baseAddr + Module_cnt);
614 brd->numPorts = j * 8;
615 writew(j, baseAddr + Module_no);
616 writew(0, baseAddr + Magic_no);
617 for (i = 0; i < 600; i++) {
618 if (readw(baseAddr + Magic_no) == Magic_code)
622 if (readw(baseAddr + Magic_no) != Magic_code)
625 brd->intNdx = baseAddr + IRQindex;
626 brd->intPend = baseAddr + IRQpending;
627 brd->intTable = baseAddr + IRQtable;
632 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
635 void __iomem *ofsAddr, *baseAddr = brd->basemem;
636 struct moxa_port *port;
640 printk(KERN_ERR "MOXA: bios length is not even\n");
644 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
648 switch (brd->boardType) {
649 case MOXA_BOARD_C218_ISA:
650 case MOXA_BOARD_C218_PCI:
651 case MOXA_BOARD_CP204J:
653 for (i = 0; i < brd->numPorts; i++, port++) {
656 port->tableAddr = baseAddr + Extern_table +
658 ofsAddr = port->tableAddr;
659 writew(C218rx_mask, ofsAddr + RX_mask);
660 writew(C218tx_mask, ofsAddr + TX_mask);
661 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
662 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
664 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
665 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
671 for (i = 0; i < brd->numPorts; i++, port++) {
674 port->tableAddr = baseAddr + Extern_table +
676 ofsAddr = port->tableAddr;
677 switch (brd->numPorts) {
679 writew(C320p8rx_mask, ofsAddr + RX_mask);
680 writew(C320p8tx_mask, ofsAddr + TX_mask);
681 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
682 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
683 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
684 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
688 writew(C320p16rx_mask, ofsAddr + RX_mask);
689 writew(C320p16tx_mask, ofsAddr + TX_mask);
690 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
691 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
692 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
693 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
697 writew(C320p24rx_mask, ofsAddr + RX_mask);
698 writew(C320p24tx_mask, ofsAddr + TX_mask);
699 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
700 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
701 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
702 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
705 writew(C320p32rx_mask, ofsAddr + RX_mask);
706 writew(C320p32tx_mask, ofsAddr + TX_mask);
707 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
708 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
709 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
710 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
711 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
720 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
722 void *ptr = fw->data;
726 unsigned int a, lenp, lencnt;
729 __le32 magic; /* 0x34303430 */
731 u8 type; /* UNIX = 3 */
732 u8 model; /* C218T=1, C320T=2, CP204=3 */
737 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
739 if (fw->size < MOXA_FW_HDRLEN) {
740 strcpy(rsn, "too short (even header won't fit)");
743 if (hdr->magic != cpu_to_le32(0x30343034)) {
744 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
747 if (hdr->type != 3) {
748 sprintf(rsn, "not for linux, type is %u", hdr->type);
751 if (moxa_check_fw_model(brd, hdr->model)) {
752 sprintf(rsn, "not for this card, model is %u", hdr->model);
756 len = MOXA_FW_HDRLEN;
757 lencnt = hdr->model == 2 ? 5 : 3;
758 for (a = 0; a < ARRAY_SIZE(lens); a++) {
759 lens[a] = le16_to_cpu(hdr->len[a]);
760 if (lens[a] && len + lens[a] <= fw->size &&
761 moxa_check_fw(&fw->data[len]))
762 printk(KERN_WARNING "MOXA firmware: unexpected input "
763 "at offset %u, but going on\n", (u32)len);
764 if (!lens[a] && a < lencnt) {
765 sprintf(rsn, "too few entries in fw file");
771 if (len != fw->size) {
772 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
777 ptr += MOXA_FW_HDRLEN;
780 strcpy(rsn, "read above");
782 ret = moxa_load_bios(brd, ptr, lens[lenp]);
786 /* we skip the tty section (lens[1]), since we don't need it */
787 ptr += lens[lenp] + lens[lenp + 1];
788 lenp += 2; /* comm */
790 if (hdr->model == 2) {
791 ret = moxa_load_320b(brd, ptr, lens[lenp]);
794 /* skip another tty */
795 ptr += lens[lenp] + lens[lenp + 1];
799 ret = moxa_load_code(brd, ptr, lens[lenp]);
805 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
809 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
811 const struct firmware *fw;
817 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
819 if (brd->ports == NULL) {
820 printk(KERN_ERR "cannot allocate memory for ports\n");
825 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
826 p->type = PORT_16550A;
827 p->close_delay = 5 * HZ / 10;
828 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
829 init_waitqueue_head(&p->open_wait);
832 switch (brd->boardType) {
833 case MOXA_BOARD_C218_ISA:
834 case MOXA_BOARD_C218_PCI:
835 file = "c218tunx.cod";
837 case MOXA_BOARD_CP204J:
838 file = "cp204unx.cod";
841 file = "c320tunx.cod";
845 ret = request_firmware(&fw, file, dev);
847 printk(KERN_ERR "request_firmware failed\n");
851 ret = moxa_load_fw(brd, fw);
853 release_firmware(fw);
858 spin_lock_bh(&moxa_lock);
860 if (!timer_pending(&moxaTimer))
861 mod_timer(&moxaTimer, jiffies + HZ / 50);
862 spin_unlock_bh(&moxa_lock);
871 static void moxa_board_deinit(struct moxa_board_conf *brd)
873 unsigned int a, opened;
875 mutex_lock(&moxa_openlock);
876 spin_lock_bh(&moxa_lock);
878 spin_unlock_bh(&moxa_lock);
880 /* pci hot-un-plug support */
881 for (a = 0; a < brd->numPorts; a++)
882 if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
883 tty_hangup(brd->ports[a].tty);
886 for (a = 0; a < brd->numPorts; a++)
887 if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
889 mutex_unlock(&moxa_openlock);
893 mutex_lock(&moxa_openlock);
896 iounmap(brd->basemem);
902 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
903 const struct pci_device_id *ent)
905 struct moxa_board_conf *board;
907 int board_type = ent->driver_data;
910 retval = pci_enable_device(pdev);
912 dev_err(&pdev->dev, "can't enable pci device\n");
916 for (i = 0; i < MAX_BOARDS; i++)
917 if (moxa_boards[i].basemem == NULL)
921 if (i >= MAX_BOARDS) {
922 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
923 "found. Board is ignored.\n", MAX_BOARDS);
927 board = &moxa_boards[i];
929 retval = pci_request_region(pdev, 2, "moxa-base");
931 dev_err(&pdev->dev, "can't request pci region 2\n");
935 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
936 if (board->basemem == NULL) {
937 dev_err(&pdev->dev, "can't remap io space 2\n");
941 board->boardType = board_type;
942 switch (board_type) {
943 case MOXA_BOARD_C218_ISA:
944 case MOXA_BOARD_C218_PCI:
948 case MOXA_BOARD_CP204J:
955 board->busType = MOXA_BUS_TYPE_PCI;
957 retval = moxa_init_board(board, &pdev->dev);
961 pci_set_drvdata(pdev, board);
965 iounmap(board->basemem);
966 board->basemem = NULL;
968 pci_release_region(pdev, 2);
973 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
975 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
977 moxa_board_deinit(brd);
979 pci_release_region(pdev, 2);
982 static struct pci_driver moxa_pci_driver = {
984 .id_table = moxa_pcibrds,
985 .probe = moxa_pci_probe,
986 .remove = __devexit_p(moxa_pci_remove)
988 #endif /* CONFIG_PCI */
990 static int __init moxa_init(void)
992 unsigned int isabrds = 0;
995 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
997 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1001 moxaDriver->owner = THIS_MODULE;
1002 moxaDriver->name = "ttyMX";
1003 moxaDriver->major = ttymajor;
1004 moxaDriver->minor_start = 0;
1005 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1006 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1007 moxaDriver->init_termios = tty_std_termios;
1008 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1009 moxaDriver->init_termios.c_ispeed = 9600;
1010 moxaDriver->init_termios.c_ospeed = 9600;
1011 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1012 tty_set_operations(moxaDriver, &moxa_ops);
1014 if (tty_register_driver(moxaDriver)) {
1015 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1016 put_tty_driver(moxaDriver);
1020 /* Find the boards defined from module args. */
1023 struct moxa_board_conf *brd = moxa_boards;
1025 for (i = 0; i < MAX_BOARDS; i++) {
1028 if (type[i] == MOXA_BOARD_C218_ISA ||
1029 type[i] == MOXA_BOARD_C320_ISA) {
1030 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1031 isabrds + 1, moxa_brdname[type[i] - 1],
1033 brd->boardType = type[i];
1034 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1036 brd->busType = MOXA_BUS_TYPE_ISA;
1037 brd->basemem = ioremap(baseaddr[i], 0x4000);
1038 if (!brd->basemem) {
1039 printk(KERN_ERR "MOXA: can't remap %lx\n",
1043 if (moxa_init_board(brd, NULL)) {
1044 iounmap(brd->basemem);
1045 brd->basemem = NULL;
1057 retval = pci_register_driver(&moxa_pci_driver);
1059 printk(KERN_ERR "Can't register MOXA pci driver!\n");
1068 static void __exit moxa_exit(void)
1073 pci_unregister_driver(&moxa_pci_driver);
1076 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1077 if (moxa_boards[i].ready)
1078 moxa_board_deinit(&moxa_boards[i]);
1080 del_timer_sync(&moxaTimer);
1082 if (tty_unregister_driver(moxaDriver))
1083 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1085 put_tty_driver(moxaDriver);
1088 module_init(moxa_init);
1089 module_exit(moxa_exit);
1091 static void moxa_close_port(struct moxa_port *ch)
1094 MoxaPortFlushData(ch, 2);
1095 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1096 ch->tty->driver_data = NULL;
1100 static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1101 struct moxa_port *ch)
1108 prepare_to_wait(&ch->open_wait, &wait, TASK_INTERRUPTIBLE);
1109 if (tty_hung_up_p(filp)) {
1110 #ifdef SERIAL_DO_RESTART
1111 retval = -ERESTARTSYS;
1117 spin_lock_bh(&moxa_lock);
1119 spin_unlock_bh(&moxa_lock);
1123 if (signal_pending(current)) {
1124 retval = -ERESTARTSYS;
1129 finish_wait(&ch->open_wait, &wait);
1134 static int moxa_open(struct tty_struct *tty, struct file *filp)
1136 struct moxa_board_conf *brd;
1137 struct moxa_port *ch;
1142 if (port == MAX_PORTS) {
1143 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1145 if (mutex_lock_interruptible(&moxa_openlock))
1146 return -ERESTARTSYS;
1147 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1149 mutex_unlock(&moxa_openlock);
1153 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1155 tty->driver_data = ch;
1157 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
1158 ch->statusflags = 0;
1159 moxa_set_tty_param(tty, tty->termios);
1160 MoxaPortLineCtrl(ch, 1, 1);
1162 MoxaSetFifo(ch, ch->type == PORT_16550A);
1163 ch->asyncflags |= ASYNC_INITIALIZED;
1165 mutex_unlock(&moxa_openlock);
1168 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1169 retval = moxa_block_till_ready(tty, filp, ch);
1170 mutex_lock(&moxa_openlock);
1172 if (ch->count) /* 0 means already hung up... */
1173 if (--ch->count == 0)
1174 moxa_close_port(ch);
1176 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1177 mutex_unlock(&moxa_openlock);
1182 static void moxa_close(struct tty_struct *tty, struct file *filp)
1184 struct moxa_port *ch;
1188 if (port == MAX_PORTS || tty_hung_up_p(filp))
1191 mutex_lock(&moxa_openlock);
1192 ch = tty->driver_data;
1195 if (tty->count == 1 && ch->count != 1) {
1196 printk(KERN_WARNING "moxa_close: bad serial port count; "
1197 "tty->count is 1, ch->count is %d\n", ch->count);
1200 if (--ch->count < 0) {
1201 printk(KERN_WARNING "moxa_close: bad serial port count, "
1202 "device=%s\n", tty->name);
1208 ch->cflag = tty->termios->c_cflag;
1209 if (ch->asyncflags & ASYNC_INITIALIZED) {
1210 moxa_setup_empty_event(tty);
1211 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
1214 moxa_close_port(ch);
1216 mutex_unlock(&moxa_openlock);
1219 static int moxa_write(struct tty_struct *tty,
1220 const unsigned char *buf, int count)
1222 struct moxa_port *ch = tty->driver_data;
1228 spin_lock_bh(&moxa_lock);
1229 len = MoxaPortWriteData(ch, buf, count);
1230 spin_unlock_bh(&moxa_lock);
1232 ch->statusflags |= LOWWAIT;
1236 static int moxa_write_room(struct tty_struct *tty)
1238 struct moxa_port *ch;
1242 ch = tty->driver_data;
1245 return MoxaPortTxFree(ch);
1248 static void moxa_flush_buffer(struct tty_struct *tty)
1250 struct moxa_port *ch = tty->driver_data;
1254 MoxaPortFlushData(ch, 1);
1258 static int moxa_chars_in_buffer(struct tty_struct *tty)
1260 struct moxa_port *ch = tty->driver_data;
1264 * Sigh...I have to check if driver_data is NULL here, because
1265 * if an open() fails, the TTY subsystem eventually calls
1266 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1267 * routine. And since the open() failed, we return 0 here. TDJ
1271 chars = MoxaPortTxQueue(ch);
1274 * Make it possible to wakeup anything waiting for output
1275 * in tty_ioctl.c, etc.
1277 if (!(ch->statusflags & EMPTYWAIT))
1278 moxa_setup_empty_event(tty);
1283 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1285 struct moxa_port *ch;
1286 int flag = 0, dtr, rts;
1288 mutex_lock(&moxa_openlock);
1289 ch = tty->driver_data;
1291 mutex_unlock(&moxa_openlock);
1295 MoxaPortGetLineOut(ch, &dtr, &rts);
1300 dtr = MoxaPortLineStatus(ch);
1307 mutex_unlock(&moxa_openlock);
1311 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1312 unsigned int set, unsigned int clear)
1314 struct moxa_port *ch;
1319 mutex_lock(&moxa_openlock);
1320 ch = tty->driver_data;
1322 mutex_unlock(&moxa_openlock);
1326 MoxaPortGetLineOut(ch, &dtr, &rts);
1327 if (set & TIOCM_RTS)
1329 if (set & TIOCM_DTR)
1331 if (clear & TIOCM_RTS)
1333 if (clear & TIOCM_DTR)
1335 MoxaPortLineCtrl(ch, dtr, rts);
1336 mutex_unlock(&moxa_openlock);
1340 static void moxa_throttle(struct tty_struct *tty)
1342 struct moxa_port *ch = tty->driver_data;
1344 ch->statusflags |= THROTTLE;
1347 static void moxa_unthrottle(struct tty_struct *tty)
1349 struct moxa_port *ch = tty->driver_data;
1351 ch->statusflags &= ~THROTTLE;
1354 static void moxa_set_termios(struct tty_struct *tty,
1355 struct ktermios *old_termios)
1357 struct moxa_port *ch = tty->driver_data;
1361 moxa_set_tty_param(tty, old_termios);
1362 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1363 wake_up_interruptible(&ch->open_wait);
1366 static void moxa_stop(struct tty_struct *tty)
1368 struct moxa_port *ch = tty->driver_data;
1372 MoxaPortTxDisable(ch);
1373 ch->statusflags |= TXSTOPPED;
1377 static void moxa_start(struct tty_struct *tty)
1379 struct moxa_port *ch = tty->driver_data;
1384 if (!(ch->statusflags & TXSTOPPED))
1387 MoxaPortTxEnable(ch);
1388 ch->statusflags &= ~TXSTOPPED;
1391 static void moxa_hangup(struct tty_struct *tty)
1393 struct moxa_port *ch;
1395 mutex_lock(&moxa_openlock);
1396 ch = tty->driver_data;
1398 mutex_unlock(&moxa_openlock);
1402 moxa_close_port(ch);
1403 mutex_unlock(&moxa_openlock);
1405 wake_up_interruptible(&ch->open_wait);
1408 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1412 if (dcd != p->DCDState && p->tty && C_CLOCAL(p->tty)) {
1419 static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1422 struct tty_struct *tty = p->tty;
1423 void __iomem *ofsAddr;
1424 unsigned int inited = p->asyncflags & ASYNC_INITIALIZED;
1428 if ((p->statusflags & EMPTYWAIT) &&
1429 MoxaPortTxQueue(p) == 0) {
1430 p->statusflags &= ~EMPTYWAIT;
1433 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1434 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1435 p->statusflags &= ~LOWWAIT;
1439 if (inited && !(p->statusflags & THROTTLE) &&
1440 MoxaPortRxQueue(p) > 0) { /* RX */
1441 MoxaPortReadData(p);
1442 tty_schedule_flip(tty);
1445 p->statusflags &= ~EMPTYWAIT;
1446 MoxaPortFlushData(p, 0); /* flush RX */
1449 if (!handle) /* nothing else to do */
1452 intr = readw(ip); /* port irq status */
1456 writew(0, ip); /* ACK port */
1457 ofsAddr = p->tableAddr;
1458 if (intr & IntrTx) /* disable tx intr */
1459 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1460 ofsAddr + HostStat);
1465 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1466 tty_insert_flip_char(tty, 0, TTY_BREAK);
1467 tty_schedule_flip(tty);
1470 if (intr & IntrLine)
1471 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1476 static void moxa_poll(unsigned long ignored)
1478 struct moxa_board_conf *brd;
1480 unsigned int card, port, served = 0;
1482 spin_lock(&moxa_lock);
1483 for (card = 0; card < MAX_BOARDS; card++) {
1484 brd = &moxa_boards[card];
1491 if (readb(brd->intPend) == 0xff)
1492 ip = brd->intTable + readb(brd->intNdx);
1494 for (port = 0; port < brd->numPorts; port++)
1495 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1498 writeb(0, brd->intPend); /* ACK */
1500 if (moxaLowWaterChk) {
1501 struct moxa_port *p = brd->ports;
1502 for (port = 0; port < brd->numPorts; port++, p++)
1503 if (p->lowChkFlag) {
1505 moxa_low_water_check(p->tableAddr);
1509 moxaLowWaterChk = 0;
1512 mod_timer(&moxaTimer, jiffies + HZ / 50);
1513 spin_unlock(&moxa_lock);
1516 /******************************************************************************/
1518 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1520 register struct ktermios *ts = tty->termios;
1521 struct moxa_port *ch = tty->driver_data;
1522 int rts, cts, txflow, rxflow, xany, baud;
1524 rts = cts = txflow = rxflow = xany = 0;
1525 if (ts->c_cflag & CRTSCTS)
1527 if (ts->c_iflag & IXON)
1529 if (ts->c_iflag & IXOFF)
1531 if (ts->c_iflag & IXANY)
1534 /* Clear the features we don't support */
1535 ts->c_cflag &= ~CMSPAR;
1536 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1537 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1539 baud = tty_termios_baud_rate(old_termios);
1540 /* Not put the baud rate into the termios data */
1541 tty_encode_baud_rate(tty, baud, baud);
1544 static void moxa_setup_empty_event(struct tty_struct *tty)
1546 struct moxa_port *ch = tty->driver_data;
1548 spin_lock_bh(&moxa_lock);
1549 ch->statusflags |= EMPTYWAIT;
1550 spin_unlock_bh(&moxa_lock);
1553 static void moxa_shut_down(struct moxa_port *ch)
1555 struct tty_struct *tp = ch->tty;
1557 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1560 MoxaPortDisable(ch);
1563 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1566 MoxaPortLineCtrl(ch, 0, 0);
1568 spin_lock_bh(&moxa_lock);
1569 ch->asyncflags &= ~ASYNC_INITIALIZED;
1570 spin_unlock_bh(&moxa_lock);
1573 /*****************************************************************************
1574 * Driver level functions: *
1575 *****************************************************************************/
1577 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1579 void __iomem *ofsAddr;
1580 if (mode < 0 || mode > 2)
1582 ofsAddr = port->tableAddr;
1583 moxafunc(ofsAddr, FC_FlushQueue, mode);
1585 port->lowChkFlag = 0;
1586 moxa_low_water_check(ofsAddr);
1591 * Moxa Port Number Description:
1593 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1594 * the port number using in MOXA driver functions will be 0 to 31 for
1595 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1596 * to 127 for fourth. For example, if you setup three MOXA boards,
1597 * first board is C218, second board is C320-16 and third board is
1598 * C320-32. The port number of first board (C218 - 8 ports) is from
1599 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1600 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1601 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1602 * 127 will be invalid.
1605 * Moxa Functions Description:
1607 * Function 1: Driver initialization routine, this routine must be
1608 * called when initialized driver.
1610 * void MoxaDriverInit();
1613 * Function 2: Moxa driver private IOCTL command processing.
1615 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1617 * unsigned int cmd : IOCTL command
1618 * unsigned long arg : IOCTL argument
1619 * int port : port number (0 - 127)
1626 * Function 6: Enable this port to start Tx/Rx data.
1628 * void MoxaPortEnable(int port);
1629 * int port : port number (0 - 127)
1632 * Function 7: Disable this port
1634 * void MoxaPortDisable(int port);
1635 * int port : port number (0 - 127)
1638 * Function 8: Get the maximun available baud rate of this port.
1640 * long MoxaPortGetMaxBaud(int port);
1641 * int port : port number (0 - 127)
1643 * return: 0 : this port is invalid
1644 * 38400/57600/115200 bps
1647 * Function 10: Setting baud rate of this port.
1649 * long MoxaPortSetBaud(int port, long baud);
1650 * int port : port number (0 - 127)
1651 * long baud : baud rate (50 - 115200)
1653 * return: 0 : this port is invalid or baud < 50
1654 * 50 - 115200 : the real baud rate set to the port, if
1655 * the argument baud is large than maximun
1656 * available baud rate, the real setting
1657 * baud rate will be the maximun baud rate.
1660 * Function 12: Configure the port.
1662 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1663 * int port : port number (0 - 127)
1664 * struct ktermios * termio : termio structure pointer
1665 * speed_t baud : baud rate
1667 * return: -1 : this port is invalid or termio == NULL
1671 * Function 13: Get the DTR/RTS state of this port.
1673 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1674 * int port : port number (0 - 127)
1675 * int * dtrState : pointer to INT to receive the current DTR
1676 * state. (if NULL, this function will not
1677 * write to this address)
1678 * int * rtsState : pointer to INT to receive the current RTS
1679 * state. (if NULL, this function will not
1680 * write to this address)
1682 * return: -1 : this port is invalid
1686 * Function 14: Setting the DTR/RTS output state of this port.
1688 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1689 * int port : port number (0 - 127)
1690 * int dtrState : DTR output state (0: off, 1: on)
1691 * int rtsState : RTS output state (0: off, 1: on)
1694 * Function 15: Setting the flow control of this port.
1696 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1697 * int txFlow,int xany);
1698 * int port : port number (0 - 127)
1699 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1700 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1701 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1702 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1703 * int xany : S/W XANY flow control (0: no, 1: yes)
1706 * Function 16: Get ths line status of this port
1708 * int MoxaPortLineStatus(int port);
1709 * int port : port number (0 - 127)
1711 * return: Bit 0 - CTS state (0: off, 1: on)
1712 * Bit 1 - DSR state (0: off, 1: on)
1713 * Bit 2 - DCD state (0: off, 1: on)
1716 * Function 19: Flush the Rx/Tx buffer data of this port.
1718 * void MoxaPortFlushData(int port, int mode);
1719 * int port : port number (0 - 127)
1721 * 0 : flush the Rx buffer
1722 * 1 : flush the Tx buffer
1723 * 2 : flush the Rx and Tx buffer
1726 * Function 20: Write data.
1728 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1729 * int port : port number (0 - 127)
1730 * unsigned char * buffer : pointer to write data buffer.
1731 * int length : write data length
1733 * return: 0 - length : real write data length
1736 * Function 21: Read data.
1738 * int MoxaPortReadData(int port, struct tty_struct *tty);
1739 * int port : port number (0 - 127)
1740 * struct tty_struct *tty : tty for data
1742 * return: 0 - length : real read data length
1745 * Function 24: Get the Tx buffer current queued data bytes
1747 * int MoxaPortTxQueue(int port);
1748 * int port : port number (0 - 127)
1750 * return: .. : Tx buffer current queued data bytes
1753 * Function 25: Get the Tx buffer current free space
1755 * int MoxaPortTxFree(int port);
1756 * int port : port number (0 - 127)
1758 * return: .. : Tx buffer current free space
1761 * Function 26: Get the Rx buffer current queued data bytes
1763 * int MoxaPortRxQueue(int port);
1764 * int port : port number (0 - 127)
1766 * return: .. : Rx buffer current queued data bytes
1769 * Function 28: Disable port data transmission.
1771 * void MoxaPortTxDisable(int port);
1772 * int port : port number (0 - 127)
1775 * Function 29: Enable port data transmission.
1777 * void MoxaPortTxEnable(int port);
1778 * int port : port number (0 - 127)
1781 * Function 31: Get the received BREAK signal count and reset it.
1783 * int MoxaPortResetBrkCnt(int port);
1784 * int port : port number (0 - 127)
1786 * return: 0 - .. : BREAK signal count
1791 static void MoxaPortEnable(struct moxa_port *port)
1793 void __iomem *ofsAddr;
1796 ofsAddr = port->tableAddr;
1797 writew(lowwater, ofsAddr + Low_water);
1798 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1799 port->board->boardType == MOXA_BOARD_C320_PCI)
1800 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1802 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1803 ofsAddr + HostStat);
1805 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1806 moxafunc(ofsAddr, FC_FlushQueue, 2);
1808 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1809 MoxaPortLineStatus(port);
1812 static void MoxaPortDisable(struct moxa_port *port)
1814 void __iomem *ofsAddr = port->tableAddr;
1816 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1817 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1818 writew(0, ofsAddr + HostStat);
1819 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1822 static long MoxaPortGetMaxBaud(struct moxa_port *port)
1824 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1825 port->board->boardType == MOXA_BOARD_C320_PCI)
1832 static long MoxaPortSetBaud(struct moxa_port *port, long baud)
1834 void __iomem *ofsAddr;
1838 if (baud < 50L || (max = MoxaPortGetMaxBaud(port)) == 0)
1840 ofsAddr = port->tableAddr;
1844 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1845 else if (max == 57600L)
1846 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1848 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1850 moxafunc(ofsAddr, FC_SetBaud, val);
1855 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1858 void __iomem *ofsAddr;
1862 ofsAddr = port->tableAddr;
1863 cflag = termio->c_cflag; /* termio->c_cflag */
1865 mode = termio->c_cflag & CSIZE;
1868 else if (mode == CS6)
1870 else if (mode == CS7)
1872 else if (mode == CS8)
1875 if (termio->c_cflag & CSTOPB) {
1883 if (termio->c_cflag & PARENB) {
1884 if (termio->c_cflag & PARODD)
1891 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
1893 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1894 port->board->boardType == MOXA_BOARD_C320_PCI) {
1895 if (baud >= 921600L)
1898 baud = MoxaPortSetBaud(port, baud);
1900 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1901 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1902 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1903 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1904 moxa_wait_finish(ofsAddr);
1910 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1914 *dtrState = !!(port->lineCtrl & DTR_ON);
1916 *rtsState = !!(port->lineCtrl & RTS_ON);
1921 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1929 port->lineCtrl = mode;
1930 moxafunc(port->tableAddr, FC_LineControl, mode);
1933 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1934 int txflow, int rxflow, int txany)
1939 mode |= RTS_FlowCtl;
1941 mode |= CTS_FlowCtl;
1948 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
1951 static int MoxaPortLineStatus(struct moxa_port *port)
1953 void __iomem *ofsAddr;
1956 ofsAddr = port->tableAddr;
1957 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1958 port->board->boardType == MOXA_BOARD_C320_PCI) {
1959 moxafunc(ofsAddr, FC_LineStatus, 0);
1960 val = readw(ofsAddr + FuncArg);
1962 val = readw(ofsAddr + FlagStat) >> 4;
1967 spin_lock_bh(&moxa_lock);
1968 moxa_new_dcdstate(port, val & 8);
1969 spin_unlock_bh(&moxa_lock);
1974 static int MoxaPortWriteData(struct moxa_port *port,
1975 const unsigned char *buffer, int len)
1977 void __iomem *baseAddr, *ofsAddr, *ofs;
1978 unsigned int c, total;
1979 u16 head, tail, tx_mask, spage, epage;
1980 u16 pageno, pageofs, bufhead;
1982 ofsAddr = port->tableAddr;
1983 baseAddr = port->board->basemem;
1984 tx_mask = readw(ofsAddr + TX_mask);
1985 spage = readw(ofsAddr + Page_txb);
1986 epage = readw(ofsAddr + EndPage_txb);
1987 tail = readw(ofsAddr + TXwptr);
1988 head = readw(ofsAddr + TXrptr);
1989 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
1992 moxaLog.txcnt[port->tty->index] += c;
1994 if (spage == epage) {
1995 bufhead = readw(ofsAddr + Ofs_txb);
1996 writew(spage, baseAddr + Control_reg);
1999 len = head - tail - 1;
2001 len = tx_mask + 1 - tail;
2002 len = (c > len) ? len : c;
2003 ofs = baseAddr + DynPage_addr + bufhead + tail;
2004 memcpy_toio(ofs, buffer, len);
2006 tail = (tail + len) & tx_mask;
2010 pageno = spage + (tail >> 13);
2011 pageofs = tail & Page_mask;
2013 len = Page_size - pageofs;
2016 writeb(pageno, baseAddr + Control_reg);
2017 ofs = baseAddr + DynPage_addr + pageofs;
2018 memcpy_toio(ofs, buffer, len);
2020 if (++pageno == epage)
2025 tail = (tail + total) & tx_mask;
2027 writew(tail, ofsAddr + TXwptr);
2028 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2032 static int MoxaPortReadData(struct moxa_port *port)
2034 struct tty_struct *tty = port->tty;
2036 void __iomem *baseAddr, *ofsAddr, *ofs;
2037 unsigned int count, len, total;
2038 u16 tail, rx_mask, spage, epage;
2039 u16 pageno, pageofs, bufhead, head;
2041 ofsAddr = port->tableAddr;
2042 baseAddr = port->board->basemem;
2043 head = readw(ofsAddr + RXrptr);
2044 tail = readw(ofsAddr + RXwptr);
2045 rx_mask = readw(ofsAddr + RX_mask);
2046 spage = readw(ofsAddr + Page_rxb);
2047 epage = readw(ofsAddr + EndPage_rxb);
2048 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
2053 moxaLog.rxcnt[tty->index] += total;
2054 if (spage == epage) {
2055 bufhead = readw(ofsAddr + Ofs_rxb);
2056 writew(spage, baseAddr + Control_reg);
2058 ofs = baseAddr + DynPage_addr + bufhead + head;
2059 len = (tail >= head) ? (tail - head) :
2060 (rx_mask + 1 - head);
2061 len = tty_prepare_flip_string(tty, &dst,
2063 memcpy_fromio(dst, ofs, len);
2064 head = (head + len) & rx_mask;
2068 pageno = spage + (head >> 13);
2069 pageofs = head & Page_mask;
2071 writew(pageno, baseAddr + Control_reg);
2072 ofs = baseAddr + DynPage_addr + pageofs;
2073 len = tty_prepare_flip_string(tty, &dst,
2074 min(Page_size - pageofs, count));
2075 memcpy_fromio(dst, ofs, len);
2078 pageofs = (pageofs + len) & Page_mask;
2079 if (pageofs == 0 && ++pageno == epage)
2082 head = (head + total) & rx_mask;
2084 writew(head, ofsAddr + RXrptr);
2085 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2086 moxaLowWaterChk = 1;
2087 port->lowChkFlag = 1;
2093 static int MoxaPortTxQueue(struct moxa_port *port)
2095 void __iomem *ofsAddr = port->tableAddr;
2096 u16 rptr, wptr, mask;
2098 rptr = readw(ofsAddr + TXrptr);
2099 wptr = readw(ofsAddr + TXwptr);
2100 mask = readw(ofsAddr + TX_mask);
2101 return (wptr - rptr) & mask;
2104 static int MoxaPortTxFree(struct moxa_port *port)
2106 void __iomem *ofsAddr = port->tableAddr;
2107 u16 rptr, wptr, mask;
2109 rptr = readw(ofsAddr + TXrptr);
2110 wptr = readw(ofsAddr + TXwptr);
2111 mask = readw(ofsAddr + TX_mask);
2112 return mask - ((wptr - rptr) & mask);
2115 static int MoxaPortRxQueue(struct moxa_port *port)
2117 void __iomem *ofsAddr = port->tableAddr;
2118 u16 rptr, wptr, mask;
2120 rptr = readw(ofsAddr + RXrptr);
2121 wptr = readw(ofsAddr + RXwptr);
2122 mask = readw(ofsAddr + RX_mask);
2123 return (wptr - rptr) & mask;
2126 static void MoxaPortTxDisable(struct moxa_port *port)
2128 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2131 static void MoxaPortTxEnable(struct moxa_port *port)
2133 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2136 static int moxa_get_serial_info(struct moxa_port *info,
2137 struct serial_struct __user *retinfo)
2139 struct serial_struct tmp = {
2141 .line = info->tty->index,
2142 .flags = info->asyncflags,
2143 .baud_base = 921600,
2144 .close_delay = info->close_delay
2146 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2150 static int moxa_set_serial_info(struct moxa_port *info,
2151 struct serial_struct __user *new_info)
2153 struct serial_struct new_serial;
2155 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2158 if (new_serial.irq != 0 || new_serial.port != 0 ||
2159 new_serial.custom_divisor != 0 ||
2160 new_serial.baud_base != 921600)
2163 if (!capable(CAP_SYS_ADMIN)) {
2164 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2165 (info->asyncflags & ~ASYNC_USR_MASK)))
2168 info->close_delay = new_serial.close_delay * HZ / 100;
2170 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2171 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2173 MoxaSetFifo(info, new_serial.type == PORT_16550A);
2175 info->type = new_serial.type;
2181 /*****************************************************************************
2182 * Static local functions: *
2183 *****************************************************************************/
2185 static void MoxaSetFifo(struct moxa_port *port, int enable)
2187 void __iomem *ofsAddr = port->tableAddr;
2190 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2191 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2193 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2194 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);