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
57 #define MOXACUMAJOR 173
59 #define MAX_BOARDS 4 /* Don't change this value */
60 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
61 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
64 * Define the Moxa PCI vendor and device IDs.
66 #define MOXA_BUS_TYPE_ISA 0
67 #define MOXA_BUS_TYPE_PCI 1
70 MOXA_BOARD_C218_PCI = 1,
77 static char *moxa_brdname[] =
79 "C218 Turbo PCI series",
80 "C218 Turbo ISA series",
81 "C320 Turbo PCI series",
82 "C320 Turbo ISA series",
87 static struct pci_device_id moxa_pcibrds[] = {
88 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
89 .driver_data = MOXA_BOARD_C218_PCI },
90 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
91 .driver_data = MOXA_BOARD_C320_PCI },
92 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
93 .driver_data = MOXA_BOARD_CP204J },
96 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
97 #endif /* CONFIG_PCI */
101 static struct moxa_board_conf {
108 struct moxa_port *ports;
110 void __iomem *basemem;
111 void __iomem *intNdx;
112 void __iomem *intPend;
113 void __iomem *intTable;
114 } moxa_boards[MAX_BOARDS];
116 struct mxser_mstatus {
130 struct moxa_board_conf *board;
131 struct tty_struct *tty;
132 void __iomem *tableAddr;
139 unsigned long statusflags;
140 wait_queue_head_t open_wait;
149 int rxcnt[MAX_PORTS];
150 int txcnt[MAX_PORTS];
154 #define TXSTOPPED 0x1
156 #define EMPTYWAIT 0x4
159 #define SERIAL_DO_RESTART
161 #define WAKEUP_CHARS 256
163 static int ttymajor = MOXAMAJOR;
164 static struct mon_str moxaLog;
165 static unsigned int moxaFuncTout = HZ / 2;
166 static unsigned int moxaLowWaterChk;
167 static DEFINE_MUTEX(moxa_openlock);
168 /* Variables for insmod */
170 static unsigned long baseaddr[MAX_BOARDS];
171 static unsigned int type[MAX_BOARDS];
172 static unsigned int numports[MAX_BOARDS];
175 MODULE_AUTHOR("William Chen");
176 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
177 MODULE_LICENSE("GPL");
179 module_param_array(type, uint, NULL, 0);
180 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
181 module_param_array(baseaddr, ulong, NULL, 0);
182 MODULE_PARM_DESC(baseaddr, "base address");
183 module_param_array(numports, uint, NULL, 0);
184 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
186 module_param(ttymajor, int, 0);
191 static int moxa_open(struct tty_struct *, struct file *);
192 static void moxa_close(struct tty_struct *, struct file *);
193 static int moxa_write(struct tty_struct *, const unsigned char *, int);
194 static int moxa_write_room(struct tty_struct *);
195 static void moxa_flush_buffer(struct tty_struct *);
196 static int moxa_chars_in_buffer(struct tty_struct *);
197 static void moxa_flush_chars(struct tty_struct *);
198 static void moxa_put_char(struct tty_struct *, unsigned char);
199 static void moxa_throttle(struct tty_struct *);
200 static void moxa_unthrottle(struct tty_struct *);
201 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
202 static void moxa_stop(struct tty_struct *);
203 static void moxa_start(struct tty_struct *);
204 static void moxa_hangup(struct tty_struct *);
205 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
206 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
207 unsigned int set, unsigned int clear);
208 static void moxa_poll(unsigned long);
209 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
210 static void moxa_setup_empty_event(struct tty_struct *);
211 static void moxa_shut_down(struct moxa_port *);
213 * moxa board interface functions:
215 static void MoxaPortEnable(struct moxa_port *);
216 static void MoxaPortDisable(struct moxa_port *);
217 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
218 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
219 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
220 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
221 static int MoxaPortLineStatus(struct moxa_port *);
222 static void MoxaPortFlushData(struct moxa_port *, int);
223 static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int);
224 static int MoxaPortReadData(struct moxa_port *);
225 static int MoxaPortTxQueue(struct moxa_port *);
226 static int MoxaPortRxQueue(struct moxa_port *);
227 static int MoxaPortTxFree(struct moxa_port *);
228 static void MoxaPortTxDisable(struct moxa_port *);
229 static void MoxaPortTxEnable(struct moxa_port *);
230 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
231 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
232 static void MoxaSetFifo(struct moxa_port *port, int enable);
238 static void moxa_wait_finish(void __iomem *ofsAddr)
240 unsigned long end = jiffies + moxaFuncTout;
242 while (readw(ofsAddr + FuncCode) != 0)
243 if (time_after(jiffies, end))
245 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
246 printk(KERN_WARNING "moxa function expired\n");
249 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
251 writew(arg, ofsAddr + FuncArg);
252 writew(cmd, ofsAddr + FuncCode);
253 moxa_wait_finish(ofsAddr);
256 static void moxa_low_water_check(void __iomem *ofsAddr)
258 u16 rptr, wptr, mask, len;
260 if (readb(ofsAddr + FlagStat) & Xoff_state) {
261 rptr = readw(ofsAddr + RXrptr);
262 wptr = readw(ofsAddr + RXwptr);
263 mask = readw(ofsAddr + RX_mask);
264 len = (wptr - rptr) & mask;
265 if (len <= Low_water)
266 moxafunc(ofsAddr, FC_SendXon, 0);
274 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
275 unsigned int cmd, unsigned long arg)
277 struct moxa_port *ch = tty->driver_data;
278 void __user *argp = (void __user *)arg;
281 if (tty->index == MAX_PORTS) {
282 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
283 cmd != MOXA_GETMSTATUS)
289 case MOXA_GETDATACOUNT:
290 moxaLog.tick = jiffies;
291 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
294 case MOXA_FLUSH_QUEUE:
295 MoxaPortFlushData(ch, arg);
297 case MOXA_GET_IOQUEUE: {
298 struct moxaq_str __user *argm = argp;
299 struct moxaq_str tmp;
303 mutex_lock(&moxa_openlock);
304 for (i = 0; i < MAX_BOARDS; i++) {
305 p = moxa_boards[i].ports;
306 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
307 memset(&tmp, 0, sizeof(tmp));
308 if (moxa_boards[i].ready) {
309 tmp.inq = MoxaPortRxQueue(p);
310 tmp.outq = MoxaPortTxQueue(p);
312 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
313 mutex_unlock(&moxa_openlock);
318 mutex_unlock(&moxa_openlock);
320 } case MOXA_GET_OQUEUE:
321 status = MoxaPortTxQueue(ch);
322 ret = put_user(status, (unsigned long __user *)argp);
324 case MOXA_GET_IQUEUE:
325 status = MoxaPortRxQueue(ch);
326 ret = put_user(status, (unsigned long __user *)argp);
328 case MOXA_GETMSTATUS: {
329 struct mxser_mstatus __user *argm = argp;
330 struct mxser_mstatus tmp;
334 mutex_lock(&moxa_openlock);
335 for (i = 0; i < MAX_BOARDS; i++) {
336 p = moxa_boards[i].ports;
337 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
338 memset(&tmp, 0, sizeof(tmp));
339 if (!moxa_boards[i].ready)
342 status = MoxaPortLineStatus(p);
350 if (!p->tty || !p->tty->termios)
351 tmp.cflag = p->cflag;
353 tmp.cflag = p->tty->termios->c_cflag;
355 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
356 mutex_unlock(&moxa_openlock);
361 mutex_unlock(&moxa_openlock);
365 mutex_lock(&moxa_openlock);
366 ret = moxa_get_serial_info(ch, argp);
367 mutex_unlock(&moxa_openlock);
370 mutex_lock(&moxa_openlock);
371 ret = moxa_set_serial_info(ch, argp);
372 mutex_unlock(&moxa_openlock);
380 static void moxa_break_ctl(struct tty_struct *tty, int state)
382 struct moxa_port *port = tty->driver_data;
384 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
388 static const struct tty_operations moxa_ops = {
392 .write_room = moxa_write_room,
393 .flush_buffer = moxa_flush_buffer,
394 .chars_in_buffer = moxa_chars_in_buffer,
395 .flush_chars = moxa_flush_chars,
396 .put_char = moxa_put_char,
398 .throttle = moxa_throttle,
399 .unthrottle = moxa_unthrottle,
400 .set_termios = moxa_set_termios,
403 .hangup = moxa_hangup,
404 .break_ctl = moxa_break_ctl,
405 .tiocmget = moxa_tiocmget,
406 .tiocmset = moxa_tiocmset,
409 static struct tty_driver *moxaDriver;
410 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
411 static DEFINE_SPINLOCK(moxa_lock);
417 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
419 switch (brd->boardType) {
420 case MOXA_BOARD_C218_ISA:
421 case MOXA_BOARD_C218_PCI:
425 case MOXA_BOARD_CP204J:
439 static int moxa_check_fw(const void *ptr)
441 const __le16 *lptr = ptr;
443 if (*lptr != cpu_to_le16(0x7980))
449 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
452 void __iomem *baseAddr = brd->basemem;
455 writeb(HW_reset, baseAddr + Control_reg); /* reset */
457 memset_io(baseAddr, 0, 4096);
458 memcpy_toio(baseAddr, buf, len); /* download BIOS */
459 writeb(0, baseAddr + Control_reg); /* restart */
463 switch (brd->boardType) {
464 case MOXA_BOARD_C218_ISA:
465 case MOXA_BOARD_C218_PCI:
466 tmp = readw(baseAddr + C218_key);
467 if (tmp != C218_KeyCode)
470 case MOXA_BOARD_CP204J:
471 tmp = readw(baseAddr + C218_key);
472 if (tmp != CP204J_KeyCode)
476 tmp = readw(baseAddr + C320_key);
477 if (tmp != C320_KeyCode)
479 tmp = readw(baseAddr + C320_status);
480 if (tmp != STS_init) {
481 printk(KERN_ERR "moxa: bios upload failed -- CPU/Basic "
482 "module not found\n");
490 printk(KERN_ERR "moxa: bios upload failed -- board not found\n");
494 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
497 void __iomem *baseAddr = brd->basemem;
500 printk(KERN_ERR "moxa: invalid 320 bios -- too short\n");
504 writew(len - 7168 - 2, baseAddr + C320bapi_len);
505 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
506 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
507 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
508 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
513 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
516 void __iomem *baseAddr = brd->basemem;
517 const u16 *uptr = ptr;
518 size_t wlen, len2, j;
519 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
520 unsigned int i, retry, c320;
523 c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
524 brd->boardType == MOXA_BOARD_C320_ISA;
525 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
528 switch (brd->boardType) {
529 case MOXA_BOARD_CP204J:
530 case MOXA_BOARD_C218_ISA:
531 case MOXA_BOARD_C218_PCI:
533 loadbuf = C218_LoadBuf;
534 loadlen = C218DLoad_len;
535 checksum = C218check_sum;
536 checksum_ok = C218chksum_ok;
540 keycode = C320_KeyCode;
541 loadbuf = C320_LoadBuf;
542 loadlen = C320DLoad_len;
543 checksum = C320check_sum;
544 checksum_ok = C320chksum_ok;
550 for (i = 0; i < wlen; i++)
551 usum += le16_to_cpu(uptr[i]);
557 len2 = (wlen > 2048) ? 2048 : wlen;
559 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
562 writew(len2, baseAddr + loadlen);
563 writew(0, baseAddr + key);
564 for (i = 0; i < 100; i++) {
565 if (readw(baseAddr + key) == keycode)
569 if (readw(baseAddr + key) != keycode)
572 writew(0, baseAddr + loadlen);
573 writew(usum, baseAddr + checksum);
574 writew(0, baseAddr + key);
575 for (i = 0; i < 100; i++) {
576 if (readw(baseAddr + key) == keycode)
581 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
582 if (readb(baseAddr + checksum_ok) != 1)
585 writew(0, baseAddr + key);
586 for (i = 0; i < 600; i++) {
587 if (readw(baseAddr + Magic_no) == Magic_code)
591 if (readw(baseAddr + Magic_no) != Magic_code)
595 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
596 writew(0x3800, baseAddr + TMS320_PORT1);
597 writew(0x3900, baseAddr + TMS320_PORT2);
598 writew(28499, baseAddr + TMS320_CLOCK);
600 writew(0x3200, baseAddr + TMS320_PORT1);
601 writew(0x3400, baseAddr + TMS320_PORT2);
602 writew(19999, baseAddr + TMS320_CLOCK);
605 writew(1, baseAddr + Disable_IRQ);
606 writew(0, baseAddr + Magic_no);
607 for (i = 0; i < 500; i++) {
608 if (readw(baseAddr + Magic_no) == Magic_code)
612 if (readw(baseAddr + Magic_no) != Magic_code)
616 j = readw(baseAddr + Module_cnt);
619 brd->numPorts = j * 8;
620 writew(j, baseAddr + Module_no);
621 writew(0, baseAddr + Magic_no);
622 for (i = 0; i < 600; i++) {
623 if (readw(baseAddr + Magic_no) == Magic_code)
627 if (readw(baseAddr + Magic_no) != Magic_code)
630 brd->intNdx = baseAddr + IRQindex;
631 brd->intPend = baseAddr + IRQpending;
632 brd->intTable = baseAddr + IRQtable;
637 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
640 void __iomem *ofsAddr, *baseAddr = brd->basemem;
641 struct moxa_port *port;
645 printk(KERN_ERR "moxa: bios length is not even\n");
649 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
653 switch (brd->boardType) {
654 case MOXA_BOARD_C218_ISA:
655 case MOXA_BOARD_C218_PCI:
656 case MOXA_BOARD_CP204J:
658 for (i = 0; i < brd->numPorts; i++, port++) {
661 port->tableAddr = baseAddr + Extern_table +
663 ofsAddr = port->tableAddr;
664 writew(C218rx_mask, ofsAddr + RX_mask);
665 writew(C218tx_mask, ofsAddr + TX_mask);
666 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
667 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
669 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
670 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
676 for (i = 0; i < brd->numPorts; i++, port++) {
679 port->tableAddr = baseAddr + Extern_table +
681 ofsAddr = port->tableAddr;
682 switch (brd->numPorts) {
684 writew(C320p8rx_mask, ofsAddr + RX_mask);
685 writew(C320p8tx_mask, ofsAddr + TX_mask);
686 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
687 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
688 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
689 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
693 writew(C320p16rx_mask, ofsAddr + RX_mask);
694 writew(C320p16tx_mask, ofsAddr + TX_mask);
695 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
696 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
697 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
698 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
702 writew(C320p24rx_mask, ofsAddr + RX_mask);
703 writew(C320p24tx_mask, ofsAddr + TX_mask);
704 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
705 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
706 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
707 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
710 writew(C320p32rx_mask, ofsAddr + RX_mask);
711 writew(C320p32tx_mask, ofsAddr + TX_mask);
712 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
713 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
714 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
715 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
716 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
725 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
727 void *ptr = fw->data;
731 unsigned int a, lenp, lencnt;
734 __le32 magic; /* 0x34303430 */
736 u8 type; /* UNIX = 3 */
737 u8 model; /* C218T=1, C320T=2, CP204=3 */
742 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
744 if (fw->size < MOXA_FW_HDRLEN) {
745 strcpy(rsn, "too short (even header won't fit)");
748 if (hdr->magic != cpu_to_le32(0x30343034)) {
749 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
752 if (hdr->type != 3) {
753 sprintf(rsn, "not for linux, type is %u", hdr->type);
756 if (moxa_check_fw_model(brd, hdr->model)) {
757 sprintf(rsn, "not for this card, model is %u", hdr->model);
761 len = MOXA_FW_HDRLEN;
762 lencnt = hdr->model == 2 ? 5 : 3;
763 for (a = 0; a < ARRAY_SIZE(lens); a++) {
764 lens[a] = le16_to_cpu(hdr->len[a]);
765 if (lens[a] && len + lens[a] <= fw->size &&
766 moxa_check_fw(&fw->data[len]))
767 printk(KERN_WARNING "moxa firmware: unexpected input "
768 "at offset %u, but going on\n", (u32)len);
769 if (!lens[a] && a < lencnt) {
770 sprintf(rsn, "too few entries in fw file");
776 if (len != fw->size) {
777 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
782 ptr += MOXA_FW_HDRLEN;
785 strcpy(rsn, "read above");
787 ret = moxa_load_bios(brd, ptr, lens[lenp]);
791 /* we skip the tty section (lens[1]), since we don't need it */
792 ptr += lens[lenp] + lens[lenp + 1];
793 lenp += 2; /* comm */
795 if (hdr->model == 2) {
796 ret = moxa_load_320b(brd, ptr, lens[lenp]);
799 /* skip another tty */
800 ptr += lens[lenp] + lens[lenp + 1];
804 ret = moxa_load_code(brd, ptr, lens[lenp]);
810 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
814 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
816 const struct firmware *fw;
822 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
824 if (brd->ports == NULL) {
825 printk(KERN_ERR "cannot allocate memory for ports\n");
830 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
831 p->type = PORT_16550A;
832 p->close_delay = 5 * HZ / 10;
833 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
834 init_waitqueue_head(&p->open_wait);
837 switch (brd->boardType) {
838 case MOXA_BOARD_C218_ISA:
839 case MOXA_BOARD_C218_PCI:
840 file = "c218tunx.cod";
842 case MOXA_BOARD_CP204J:
843 file = "cp204unx.cod";
846 file = "c320tunx.cod";
850 ret = request_firmware(&fw, file, dev);
852 printk(KERN_ERR "request_firmware failed\n");
856 ret = moxa_load_fw(brd, fw);
858 release_firmware(fw);
863 spin_lock_bh(&moxa_lock);
865 if (!timer_pending(&moxaTimer))
866 mod_timer(&moxaTimer, jiffies + HZ / 50);
867 spin_unlock_bh(&moxa_lock);
876 static void moxa_board_deinit(struct moxa_board_conf *brd)
878 unsigned int a, opened;
880 mutex_lock(&moxa_openlock);
881 spin_lock_bh(&moxa_lock);
883 spin_unlock_bh(&moxa_lock);
885 /* pci hot-un-plug support */
886 for (a = 0; a < brd->numPorts; a++)
887 if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
888 tty_hangup(brd->ports[a].tty);
891 for (a = 0; a < brd->numPorts; a++)
892 if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
894 mutex_unlock(&moxa_openlock);
898 mutex_lock(&moxa_openlock);
901 iounmap(brd->basemem);
907 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
908 const struct pci_device_id *ent)
910 struct moxa_board_conf *board;
912 int board_type = ent->driver_data;
915 retval = pci_enable_device(pdev);
917 dev_err(&pdev->dev, "can't enable pci device\n");
921 for (i = 0; i < MAX_BOARDS; i++)
922 if (moxa_boards[i].basemem == NULL)
926 if (i >= MAX_BOARDS) {
927 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
928 "found. Board is ignored.\n", MAX_BOARDS);
932 board = &moxa_boards[i];
934 retval = pci_request_region(pdev, 2, "moxa-base");
936 dev_err(&pdev->dev, "can't request pci region 2\n");
940 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
941 if (board->basemem == NULL) {
942 dev_err(&pdev->dev, "can't remap io space 2\n");
946 board->boardType = board_type;
947 switch (board_type) {
948 case MOXA_BOARD_C218_ISA:
949 case MOXA_BOARD_C218_PCI:
953 case MOXA_BOARD_CP204J:
960 board->busType = MOXA_BUS_TYPE_PCI;
962 retval = moxa_init_board(board, &pdev->dev);
966 pci_set_drvdata(pdev, board);
970 iounmap(board->basemem);
971 board->basemem = NULL;
973 pci_release_region(pdev, 2);
978 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
980 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
982 moxa_board_deinit(brd);
984 pci_release_region(pdev, 2);
987 static struct pci_driver moxa_pci_driver = {
989 .id_table = moxa_pcibrds,
990 .probe = moxa_pci_probe,
991 .remove = __devexit_p(moxa_pci_remove)
993 #endif /* CONFIG_PCI */
995 static int __init moxa_init(void)
997 unsigned int isabrds = 0;
1000 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1002 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1006 moxaDriver->owner = THIS_MODULE;
1007 moxaDriver->name = "ttyMX";
1008 moxaDriver->major = ttymajor;
1009 moxaDriver->minor_start = 0;
1010 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1011 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1012 moxaDriver->init_termios = tty_std_termios;
1013 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1014 moxaDriver->init_termios.c_ispeed = 9600;
1015 moxaDriver->init_termios.c_ospeed = 9600;
1016 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1017 tty_set_operations(moxaDriver, &moxa_ops);
1019 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
1021 if (tty_register_driver(moxaDriver)) {
1022 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
1023 put_tty_driver(moxaDriver);
1027 /* Find the boards defined from module args. */
1030 struct moxa_board_conf *brd = moxa_boards;
1032 for (i = 0; i < MAX_BOARDS; i++) {
1035 if (type[i] == MOXA_BOARD_C218_ISA ||
1036 type[i] == MOXA_BOARD_C320_ISA) {
1037 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1038 isabrds + 1, moxa_brdname[type[i] - 1],
1040 brd->boardType = type[i];
1041 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1043 brd->busType = MOXA_BUS_TYPE_ISA;
1044 brd->basemem = ioremap(baseaddr[i], 0x4000);
1045 if (!brd->basemem) {
1046 printk(KERN_ERR "moxa: can't remap %lx\n",
1050 if (moxa_init_board(brd, NULL)) {
1051 iounmap(brd->basemem);
1052 brd->basemem = NULL;
1064 retval = pci_register_driver(&moxa_pci_driver);
1066 printk(KERN_ERR "Can't register moxa pci driver!\n");
1075 static void __exit moxa_exit(void)
1080 pci_unregister_driver(&moxa_pci_driver);
1083 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1084 if (moxa_boards[i].ready)
1085 moxa_board_deinit(&moxa_boards[i]);
1087 del_timer_sync(&moxaTimer);
1089 if (tty_unregister_driver(moxaDriver))
1090 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1092 put_tty_driver(moxaDriver);
1095 module_init(moxa_init);
1096 module_exit(moxa_exit);
1098 static void moxa_close_port(struct moxa_port *ch)
1101 MoxaPortFlushData(ch, 2);
1102 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1103 ch->tty->driver_data = NULL;
1107 static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1108 struct moxa_port *ch)
1115 prepare_to_wait(&ch->open_wait, &wait, TASK_INTERRUPTIBLE);
1116 if (tty_hung_up_p(filp)) {
1117 #ifdef SERIAL_DO_RESTART
1118 retval = -ERESTARTSYS;
1124 spin_lock_bh(&moxa_lock);
1126 spin_unlock_bh(&moxa_lock);
1130 if (signal_pending(current)) {
1131 retval = -ERESTARTSYS;
1136 finish_wait(&ch->open_wait, &wait);
1141 static int moxa_open(struct tty_struct *tty, struct file *filp)
1143 struct moxa_board_conf *brd;
1144 struct moxa_port *ch;
1149 if (port == MAX_PORTS) {
1150 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1152 if (mutex_lock_interruptible(&moxa_openlock))
1153 return -ERESTARTSYS;
1154 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1156 mutex_unlock(&moxa_openlock);
1160 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1162 tty->driver_data = ch;
1164 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
1165 ch->statusflags = 0;
1166 moxa_set_tty_param(tty, tty->termios);
1167 MoxaPortLineCtrl(ch, 1, 1);
1169 MoxaSetFifo(ch, ch->type == PORT_16550A);
1170 ch->asyncflags |= ASYNC_INITIALIZED;
1172 mutex_unlock(&moxa_openlock);
1175 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1176 retval = moxa_block_till_ready(tty, filp, ch);
1177 mutex_lock(&moxa_openlock);
1179 if (ch->count) /* 0 means already hung up... */
1180 if (--ch->count == 0)
1181 moxa_close_port(ch);
1183 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1184 mutex_unlock(&moxa_openlock);
1189 static void moxa_close(struct tty_struct *tty, struct file *filp)
1191 struct moxa_port *ch;
1195 if (port == MAX_PORTS || tty_hung_up_p(filp))
1198 mutex_lock(&moxa_openlock);
1199 ch = tty->driver_data;
1202 if (tty->count == 1 && ch->count != 1) {
1203 printk(KERN_WARNING "moxa_close: bad serial port count; "
1204 "tty->count is 1, ch->count is %d\n", ch->count);
1207 if (--ch->count < 0) {
1208 printk(KERN_WARNING "moxa_close: bad serial port count, "
1209 "device=%s\n", tty->name);
1215 ch->cflag = tty->termios->c_cflag;
1216 if (ch->asyncflags & ASYNC_INITIALIZED) {
1217 moxa_setup_empty_event(tty);
1218 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
1221 moxa_close_port(ch);
1223 mutex_unlock(&moxa_openlock);
1226 static int moxa_write(struct tty_struct *tty,
1227 const unsigned char *buf, int count)
1229 struct moxa_port *ch = tty->driver_data;
1235 spin_lock_bh(&moxa_lock);
1236 len = MoxaPortWriteData(ch, buf, count);
1237 spin_unlock_bh(&moxa_lock);
1239 /*********************************************
1240 if ( !(ch->statusflags & LOWWAIT) &&
1241 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1242 ************************************************/
1243 ch->statusflags |= LOWWAIT;
1247 static int moxa_write_room(struct tty_struct *tty)
1249 struct moxa_port *ch;
1253 ch = tty->driver_data;
1256 return MoxaPortTxFree(ch);
1259 static void moxa_flush_buffer(struct tty_struct *tty)
1261 struct moxa_port *ch = tty->driver_data;
1265 MoxaPortFlushData(ch, 1);
1269 static int moxa_chars_in_buffer(struct tty_struct *tty)
1271 struct moxa_port *ch = tty->driver_data;
1275 * Sigh...I have to check if driver_data is NULL here, because
1276 * if an open() fails, the TTY subsystem eventually calls
1277 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1278 * routine. And since the open() failed, we return 0 here. TDJ
1282 chars = MoxaPortTxQueue(ch);
1285 * Make it possible to wakeup anything waiting for output
1286 * in tty_ioctl.c, etc.
1288 if (!(ch->statusflags & EMPTYWAIT))
1289 moxa_setup_empty_event(tty);
1294 static void moxa_flush_chars(struct tty_struct *tty)
1297 * Don't think I need this, because this is called to empty the TX
1298 * buffer for the 16450, 16550, etc.
1302 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
1304 struct moxa_port *ch = tty->driver_data;
1308 spin_lock_bh(&moxa_lock);
1309 MoxaPortWriteData(ch, &c, 1);
1310 spin_unlock_bh(&moxa_lock);
1311 /************************************************
1312 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1313 *************************************************/
1314 ch->statusflags |= LOWWAIT;
1317 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1319 struct moxa_port *ch;
1320 int flag = 0, dtr, rts;
1322 mutex_lock(&moxa_openlock);
1323 ch = tty->driver_data;
1325 mutex_unlock(&moxa_openlock);
1329 MoxaPortGetLineOut(ch, &dtr, &rts);
1334 dtr = MoxaPortLineStatus(ch);
1341 mutex_unlock(&moxa_openlock);
1345 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1346 unsigned int set, unsigned int clear)
1348 struct moxa_port *ch;
1353 mutex_lock(&moxa_openlock);
1354 ch = tty->driver_data;
1356 mutex_unlock(&moxa_openlock);
1360 MoxaPortGetLineOut(ch, &dtr, &rts);
1361 if (set & TIOCM_RTS)
1363 if (set & TIOCM_DTR)
1365 if (clear & TIOCM_RTS)
1367 if (clear & TIOCM_DTR)
1369 MoxaPortLineCtrl(ch, dtr, rts);
1370 mutex_unlock(&moxa_openlock);
1374 static void moxa_throttle(struct tty_struct *tty)
1376 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1378 ch->statusflags |= THROTTLE;
1381 static void moxa_unthrottle(struct tty_struct *tty)
1383 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1385 ch->statusflags &= ~THROTTLE;
1388 static void moxa_set_termios(struct tty_struct *tty,
1389 struct ktermios *old_termios)
1391 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1395 moxa_set_tty_param(tty, old_termios);
1396 if (!(old_termios->c_cflag & CLOCAL) &&
1397 (tty->termios->c_cflag & CLOCAL))
1398 wake_up_interruptible(&ch->open_wait);
1401 static void moxa_stop(struct tty_struct *tty)
1403 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1407 MoxaPortTxDisable(ch);
1408 ch->statusflags |= TXSTOPPED;
1412 static void moxa_start(struct tty_struct *tty)
1414 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1419 if (!(ch->statusflags & TXSTOPPED))
1422 MoxaPortTxEnable(ch);
1423 ch->statusflags &= ~TXSTOPPED;
1426 static void moxa_hangup(struct tty_struct *tty)
1428 struct moxa_port *ch;
1430 mutex_lock(&moxa_openlock);
1431 ch = tty->driver_data;
1433 mutex_unlock(&moxa_openlock);
1437 moxa_close_port(ch);
1438 mutex_unlock(&moxa_openlock);
1440 wake_up_interruptible(&ch->open_wait);
1443 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1447 if ((dcd != p->DCDState) && p->tty && C_CLOCAL(p->tty)) {
1454 static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1457 struct tty_struct *tty = p->tty;
1458 void __iomem *ofsAddr;
1459 unsigned int inited = p->asyncflags & ASYNC_INITIALIZED;
1463 if ((p->statusflags & EMPTYWAIT) &&
1464 MoxaPortTxQueue(p) == 0) {
1465 p->statusflags &= ~EMPTYWAIT;
1468 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1469 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1470 p->statusflags &= ~LOWWAIT;
1474 if (inited && !(p->statusflags & THROTTLE) &&
1475 MoxaPortRxQueue(p) > 0) { /* RX */
1476 MoxaPortReadData(p);
1477 tty_schedule_flip(tty);
1480 p->statusflags &= ~EMPTYWAIT;
1481 MoxaPortFlushData(p, 0); /* flush RX */
1484 if (!handle) /* nothing else to do */
1487 intr = readw(ip); /* port irq status */
1491 writew(0, ip); /* ACK port */
1492 ofsAddr = p->tableAddr;
1493 if (intr & IntrTx) /* disable tx intr */
1494 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1495 ofsAddr + HostStat);
1500 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1501 tty_insert_flip_char(tty, 0, TTY_BREAK);
1502 tty_schedule_flip(tty);
1505 if (intr & IntrLine)
1506 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1511 static void moxa_poll(unsigned long ignored)
1513 struct moxa_board_conf *brd;
1515 unsigned int card, port, served = 0;
1517 spin_lock(&moxa_lock);
1518 for (card = 0; card < MAX_BOARDS; card++) {
1519 brd = &moxa_boards[card];
1526 if (readb(brd->intPend) == 0xff)
1527 ip = brd->intTable + readb(brd->intNdx);
1529 for (port = 0; port < brd->numPorts; port++)
1530 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1533 writeb(0, brd->intPend); /* ACK */
1535 if (moxaLowWaterChk) {
1536 struct moxa_port *p = brd->ports;
1537 for (port = 0; port < brd->numPorts; port++, p++)
1538 if (p->lowChkFlag) {
1540 moxa_low_water_check(p->tableAddr);
1544 moxaLowWaterChk = 0;
1547 mod_timer(&moxaTimer, jiffies + HZ / 50);
1548 spin_unlock(&moxa_lock);
1551 /******************************************************************************/
1553 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1555 register struct ktermios *ts;
1556 struct moxa_port *ch;
1557 int rts, cts, txflow, rxflow, xany, baud;
1559 ch = (struct moxa_port *) tty->driver_data;
1561 rts = cts = txflow = rxflow = xany = 0;
1562 if (ts->c_cflag & CRTSCTS)
1564 if (ts->c_iflag & IXON)
1566 if (ts->c_iflag & IXOFF)
1568 if (ts->c_iflag & IXANY)
1571 /* Clear the features we don't support */
1572 ts->c_cflag &= ~CMSPAR;
1573 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1574 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1576 baud = tty_termios_baud_rate(old_termios);
1577 /* Not put the baud rate into the termios data */
1578 tty_encode_baud_rate(tty, baud, baud);
1581 static void moxa_setup_empty_event(struct tty_struct *tty)
1583 struct moxa_port *ch = tty->driver_data;
1585 spin_lock_bh(&moxa_lock);
1586 ch->statusflags |= EMPTYWAIT;
1587 spin_unlock_bh(&moxa_lock);
1590 static void moxa_shut_down(struct moxa_port *ch)
1592 struct tty_struct *tp = ch->tty;
1594 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1597 MoxaPortDisable(ch);
1600 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1603 MoxaPortLineCtrl(ch, 0, 0);
1605 spin_lock_bh(&moxa_lock);
1606 ch->asyncflags &= ~ASYNC_INITIALIZED;
1607 spin_unlock_bh(&moxa_lock);
1610 /*****************************************************************************
1611 * Driver level functions: *
1612 *****************************************************************************/
1614 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1616 void __iomem *ofsAddr;
1617 if ((mode < 0) || (mode > 2))
1619 ofsAddr = port->tableAddr;
1620 moxafunc(ofsAddr, FC_FlushQueue, mode);
1622 port->lowChkFlag = 0;
1623 moxa_low_water_check(ofsAddr);
1627 /*****************************************************************************
1628 * Port level functions: *
1629 * 2. MoxaPortEnable(int port); *
1630 * 3. MoxaPortDisable(int port); *
1631 * 4. MoxaPortGetMaxBaud(int port); *
1632 * 6. MoxaPortSetBaud(int port, long baud); *
1633 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1634 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1635 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1636 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1637 * 12. MoxaPortLineStatus(int port); *
1638 * 15. MoxaPortFlushData(int port, int mode); *
1639 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1640 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1641 * 20. MoxaPortTxQueue(int port); *
1642 * 21. MoxaPortTxFree(int port); *
1643 * 22. MoxaPortRxQueue(int port); *
1644 * 24. MoxaPortTxDisable(int port); *
1645 * 25. MoxaPortTxEnable(int port); *
1646 * 27. MoxaPortResetBrkCnt(int port); *
1647 *****************************************************************************/
1649 * Moxa Port Number Description:
1651 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1652 * the port number using in MOXA driver functions will be 0 to 31 for
1653 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1654 * to 127 for fourth. For example, if you setup three MOXA boards,
1655 * first board is C218, second board is C320-16 and third board is
1656 * C320-32. The port number of first board (C218 - 8 ports) is from
1657 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1658 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1659 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1660 * 127 will be invalid.
1663 * Moxa Functions Description:
1665 * Function 1: Driver initialization routine, this routine must be
1666 * called when initialized driver.
1668 * void MoxaDriverInit();
1671 * Function 2: Moxa driver private IOCTL command processing.
1673 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1675 * unsigned int cmd : IOCTL command
1676 * unsigned long arg : IOCTL argument
1677 * int port : port number (0 - 127)
1684 * Function 6: Enable this port to start Tx/Rx data.
1686 * void MoxaPortEnable(int port);
1687 * int port : port number (0 - 127)
1690 * Function 7: Disable this port
1692 * void MoxaPortDisable(int port);
1693 * int port : port number (0 - 127)
1696 * Function 8: Get the maximun available baud rate of this port.
1698 * long MoxaPortGetMaxBaud(int port);
1699 * int port : port number (0 - 127)
1701 * return: 0 : this port is invalid
1702 * 38400/57600/115200 bps
1705 * Function 10: Setting baud rate of this port.
1707 * long MoxaPortSetBaud(int port, long baud);
1708 * int port : port number (0 - 127)
1709 * long baud : baud rate (50 - 115200)
1711 * return: 0 : this port is invalid or baud < 50
1712 * 50 - 115200 : the real baud rate set to the port, if
1713 * the argument baud is large than maximun
1714 * available baud rate, the real setting
1715 * baud rate will be the maximun baud rate.
1718 * Function 12: Configure the port.
1720 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1721 * int port : port number (0 - 127)
1722 * struct ktermios * termio : termio structure pointer
1723 * speed_t baud : baud rate
1725 * return: -1 : this port is invalid or termio == NULL
1729 * Function 13: Get the DTR/RTS state of this port.
1731 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1732 * int port : port number (0 - 127)
1733 * int * dtrState : pointer to INT to receive the current DTR
1734 * state. (if NULL, this function will not
1735 * write to this address)
1736 * int * rtsState : pointer to INT to receive the current RTS
1737 * state. (if NULL, this function will not
1738 * write to this address)
1740 * return: -1 : this port is invalid
1744 * Function 14: Setting the DTR/RTS output state of this port.
1746 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1747 * int port : port number (0 - 127)
1748 * int dtrState : DTR output state (0: off, 1: on)
1749 * int rtsState : RTS output state (0: off, 1: on)
1752 * Function 15: Setting the flow control of this port.
1754 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1755 * int txFlow,int xany);
1756 * int port : port number (0 - 127)
1757 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1758 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1759 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1760 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1761 * int xany : S/W XANY flow control (0: no, 1: yes)
1764 * Function 16: Get ths line status of this port
1766 * int MoxaPortLineStatus(int port);
1767 * int port : port number (0 - 127)
1769 * return: Bit 0 - CTS state (0: off, 1: on)
1770 * Bit 1 - DSR state (0: off, 1: on)
1771 * Bit 2 - DCD state (0: off, 1: on)
1774 * Function 19: Flush the Rx/Tx buffer data of this port.
1776 * void MoxaPortFlushData(int port, int mode);
1777 * int port : port number (0 - 127)
1779 * 0 : flush the Rx buffer
1780 * 1 : flush the Tx buffer
1781 * 2 : flush the Rx and Tx buffer
1784 * Function 20: Write data.
1786 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1787 * int port : port number (0 - 127)
1788 * unsigned char * buffer : pointer to write data buffer.
1789 * int length : write data length
1791 * return: 0 - length : real write data length
1794 * Function 21: Read data.
1796 * int MoxaPortReadData(int port, struct tty_struct *tty);
1797 * int port : port number (0 - 127)
1798 * struct tty_struct *tty : tty for data
1800 * return: 0 - length : real read data length
1803 * Function 24: Get the Tx buffer current queued data bytes
1805 * int MoxaPortTxQueue(int port);
1806 * int port : port number (0 - 127)
1808 * return: .. : Tx buffer current queued data bytes
1811 * Function 25: Get the Tx buffer current free space
1813 * int MoxaPortTxFree(int port);
1814 * int port : port number (0 - 127)
1816 * return: .. : Tx buffer current free space
1819 * Function 26: Get the Rx buffer current queued data bytes
1821 * int MoxaPortRxQueue(int port);
1822 * int port : port number (0 - 127)
1824 * return: .. : Rx buffer current queued data bytes
1827 * Function 28: Disable port data transmission.
1829 * void MoxaPortTxDisable(int port);
1830 * int port : port number (0 - 127)
1833 * Function 29: Enable port data transmission.
1835 * void MoxaPortTxEnable(int port);
1836 * int port : port number (0 - 127)
1839 * Function 31: Get the received BREAK signal count and reset it.
1841 * int MoxaPortResetBrkCnt(int port);
1842 * int port : port number (0 - 127)
1844 * return: 0 - .. : BREAK signal count
1849 static void MoxaPortEnable(struct moxa_port *port)
1851 void __iomem *ofsAddr;
1852 short lowwater = 512;
1854 ofsAddr = port->tableAddr;
1855 writew(lowwater, ofsAddr + Low_water);
1856 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1857 port->board->boardType == MOXA_BOARD_C320_PCI) {
1858 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1860 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1863 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1864 moxafunc(ofsAddr, FC_FlushQueue, 2);
1866 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1867 MoxaPortLineStatus(port);
1870 static void MoxaPortDisable(struct moxa_port *port)
1872 void __iomem *ofsAddr = port->tableAddr;
1874 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1875 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1876 writew(0, ofsAddr + HostStat);
1877 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1880 static long MoxaPortGetMaxBaud(struct moxa_port *port)
1882 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1883 port->board->boardType == MOXA_BOARD_C320_PCI)
1890 static long MoxaPortSetBaud(struct moxa_port *port, long baud)
1892 void __iomem *ofsAddr;
1896 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
1898 ofsAddr = port->tableAddr;
1902 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1903 else if (max == 57600L)
1904 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1906 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1908 moxafunc(ofsAddr, FC_SetBaud, val);
1913 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1916 void __iomem *ofsAddr;
1920 ofsAddr = port->tableAddr;
1921 cflag = termio->c_cflag; /* termio->c_cflag */
1923 mode = termio->c_cflag & CSIZE;
1926 else if (mode == CS6)
1928 else if (mode == CS7)
1930 else if (mode == CS8)
1933 if (termio->c_cflag & CSTOPB) {
1941 if (termio->c_cflag & PARENB) {
1942 if (termio->c_cflag & PARODD)
1949 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
1951 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1952 port->board->boardType == MOXA_BOARD_C320_PCI) {
1953 if (baud >= 921600L)
1956 baud = MoxaPortSetBaud(port, baud);
1958 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1959 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1960 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1961 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1962 moxa_wait_finish(ofsAddr);
1968 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1973 *dtrState = !!(port->lineCtrl & DTR_ON);
1975 *rtsState = !!(port->lineCtrl & RTS_ON);
1980 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1988 port->lineCtrl = mode;
1989 moxafunc(port->tableAddr, FC_LineControl, mode);
1992 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1993 int txflow, int rxflow, int txany)
1998 mode |= RTS_FlowCtl;
2000 mode |= CTS_FlowCtl;
2007 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
2010 static int MoxaPortLineStatus(struct moxa_port *port)
2012 void __iomem *ofsAddr;
2015 ofsAddr = port->tableAddr;
2016 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2017 port->board->boardType == MOXA_BOARD_C320_PCI) {
2018 moxafunc(ofsAddr, FC_LineStatus, 0);
2019 val = readw(ofsAddr + FuncArg);
2021 val = readw(ofsAddr + FlagStat) >> 4;
2026 spin_lock_bh(&moxa_lock);
2027 moxa_new_dcdstate(port, val & 8);
2028 spin_unlock_bh(&moxa_lock);
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);