1 /*****************************************************************************/
3 * moxa.c -- MOXA Intellio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
17 * MOXA Intellio Series Driver
23 #include <linux/module.h>
24 #include <linux/types.h>
26 #include <linux/ioport.h>
27 #include <linux/errno.h>
28 #include <linux/firmware.h>
29 #include <linux/signal.h>
30 #include <linux/sched.h>
31 #include <linux/timer.h>
32 #include <linux/interrupt.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
35 #include <linux/major.h>
36 #include <linux/string.h>
37 #include <linux/fcntl.h>
38 #include <linux/ptrace.h>
39 #include <linux/serial.h>
40 #include <linux/tty_driver.h>
41 #include <linux/delay.h>
42 #include <linux/pci.h>
43 #include <linux/init.h>
44 #include <linux/bitops.h>
45 #include <linux/completion.h>
47 #include <asm/system.h>
49 #include <asm/uaccess.h>
53 #define MOXA_VERSION "5.1k"
55 #define MOXA_FW_HDRLEN 32
58 #define MOXACUMAJOR 173
60 #define MAX_BOARDS 4 /* Don't change this value */
61 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
62 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
65 * Define the Moxa PCI vendor and device IDs.
67 #define MOXA_BUS_TYPE_ISA 0
68 #define MOXA_BUS_TYPE_PCI 1
71 MOXA_BOARD_C218_PCI = 1,
78 static char *moxa_brdname[] =
80 "C218 Turbo PCI series",
81 "C218 Turbo ISA series",
82 "C320 Turbo PCI series",
83 "C320 Turbo ISA series",
88 static struct pci_device_id moxa_pcibrds[] = {
89 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
90 .driver_data = MOXA_BOARD_C218_PCI },
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
92 .driver_data = MOXA_BOARD_C320_PCI },
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
94 .driver_data = MOXA_BOARD_CP204J },
97 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
98 #endif /* CONFIG_PCI */
102 static struct moxa_board_conf {
109 struct moxa_port *ports;
111 void __iomem *basemem;
112 void __iomem *intNdx;
113 void __iomem *intPend;
114 void __iomem *intTable;
115 } moxa_boards[MAX_BOARDS];
117 struct mxser_mstatus {
131 struct moxa_board_conf *board;
137 unsigned long statusflags;
138 struct tty_struct *tty;
140 wait_queue_head_t open_wait;
141 struct completion close_wait;
143 struct timer_list emptyTimer;
146 void __iomem *tableAddr;
155 int rxcnt[MAX_PORTS];
156 int txcnt[MAX_PORTS];
160 #define TXSTOPPED 0x1
162 #define EMPTYWAIT 0x4
165 #define SERIAL_DO_RESTART
167 #define WAKEUP_CHARS 256
169 static int ttymajor = MOXAMAJOR;
170 static struct mon_str moxaLog;
171 static unsigned int moxaFuncTout = HZ / 2;
172 /* Variables for insmod */
174 static unsigned long baseaddr[MAX_BOARDS];
175 static unsigned int type[MAX_BOARDS];
176 static unsigned int numports[MAX_BOARDS];
179 MODULE_AUTHOR("William Chen");
180 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
181 MODULE_LICENSE("GPL");
183 module_param_array(type, uint, NULL, 0);
184 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
185 module_param_array(baseaddr, ulong, NULL, 0);
186 MODULE_PARM_DESC(baseaddr, "base address");
187 module_param_array(numports, uint, NULL, 0);
188 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
190 module_param(ttymajor, int, 0);
195 static int moxa_open(struct tty_struct *, struct file *);
196 static void moxa_close(struct tty_struct *, struct file *);
197 static int moxa_write(struct tty_struct *, const unsigned char *, int);
198 static int moxa_write_room(struct tty_struct *);
199 static void moxa_flush_buffer(struct tty_struct *);
200 static int moxa_chars_in_buffer(struct tty_struct *);
201 static void moxa_flush_chars(struct tty_struct *);
202 static void moxa_put_char(struct tty_struct *, unsigned char);
203 static void moxa_throttle(struct tty_struct *);
204 static void moxa_unthrottle(struct tty_struct *);
205 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
206 static void moxa_stop(struct tty_struct *);
207 static void moxa_start(struct tty_struct *);
208 static void moxa_hangup(struct tty_struct *);
209 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
210 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
211 unsigned int set, unsigned int clear);
212 static void moxa_poll(unsigned long);
213 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
214 static int moxa_block_till_ready(struct tty_struct *, struct file *,
216 static void moxa_setup_empty_event(struct tty_struct *);
217 static void moxa_check_xmit_empty(unsigned long);
218 static void moxa_shut_down(struct moxa_port *);
219 static void moxa_receive_data(struct moxa_port *);
221 * moxa board interface functions:
223 static int MoxaDriverPoll(void);
224 static void MoxaPortEnable(struct moxa_port *);
225 static void MoxaPortDisable(struct moxa_port *);
226 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
227 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
228 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
229 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
230 static int MoxaPortLineStatus(struct moxa_port *);
231 static int MoxaPortDCDChange(struct moxa_port *);
232 static int MoxaPortDCDON(struct moxa_port *);
233 static void MoxaPortFlushData(struct moxa_port *, int);
234 static int MoxaPortWriteData(struct moxa_port *, unsigned char *, int);
235 static int MoxaPortReadData(struct moxa_port *, struct tty_struct *tty);
236 static int MoxaPortTxQueue(struct moxa_port *);
237 static int MoxaPortRxQueue(struct moxa_port *);
238 static int MoxaPortTxFree(struct moxa_port *);
239 static void MoxaPortTxDisable(struct moxa_port *);
240 static void MoxaPortTxEnable(struct moxa_port *);
241 static int MoxaPortResetBrkCnt(struct moxa_port *);
242 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
243 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
244 static void MoxaSetFifo(struct moxa_port *port, int enable);
250 static void moxa_wait_finish(void __iomem *ofsAddr)
252 unsigned long end = jiffies + moxaFuncTout;
254 while (readw(ofsAddr + FuncCode) != 0)
255 if (time_after(jiffies, end))
257 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
258 printk(KERN_WARNING "moxa function expired\n");
261 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
263 writew(arg, ofsAddr + FuncArg);
264 writew(cmd, ofsAddr + FuncCode);
265 moxa_wait_finish(ofsAddr);
272 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
273 unsigned int cmd, unsigned long arg)
275 struct moxa_port *ch = tty->driver_data;
276 void __user *argp = (void __user *)arg;
279 if (tty->index == MAX_PORTS) {
280 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
281 cmd != MOXA_GETMSTATUS)
287 case MOXA_GETDATACOUNT:
288 moxaLog.tick = jiffies;
289 return 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 for (i = 0; i < MAX_BOARDS; i++) {
301 p = moxa_boards[i].ports;
302 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
303 memset(&tmp, 0, sizeof(tmp));
304 if (moxa_boards[i].ready) {
305 tmp.inq = MoxaPortRxQueue(p);
306 tmp.outq = MoxaPortTxQueue(p);
308 if (copy_to_user(argm, &tmp, sizeof(tmp)))
313 } case MOXA_GET_OQUEUE:
314 status = MoxaPortTxQueue(ch);
315 return put_user(status, (unsigned long __user *)argp);
316 case MOXA_GET_IQUEUE:
317 status = MoxaPortRxQueue(ch);
318 return put_user(status, (unsigned long __user *)argp);
319 case MOXA_GETMSTATUS: {
320 struct mxser_mstatus __user *argm = argp;
321 struct mxser_mstatus tmp;
325 for (i = 0; i < MAX_BOARDS; i++) {
326 p = moxa_boards[i].ports;
327 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
328 memset(&tmp, 0, sizeof(tmp));
329 if (!moxa_boards[i].ready)
332 status = MoxaPortLineStatus(p);
340 if (!p->tty || !p->tty->termios)
341 tmp.cflag = p->cflag;
343 tmp.cflag = p->tty->termios->c_cflag;
345 if (copy_to_user(argm, &tmp, sizeof(tmp)))
352 return moxa_get_serial_info(ch, argp);
354 return moxa_set_serial_info(ch, argp);
359 static void moxa_break_ctl(struct tty_struct *tty, int state)
361 struct moxa_port *port = tty->driver_data;
363 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
367 static const struct tty_operations moxa_ops = {
371 .write_room = moxa_write_room,
372 .flush_buffer = moxa_flush_buffer,
373 .chars_in_buffer = moxa_chars_in_buffer,
374 .flush_chars = moxa_flush_chars,
375 .put_char = moxa_put_char,
377 .throttle = moxa_throttle,
378 .unthrottle = moxa_unthrottle,
379 .set_termios = moxa_set_termios,
382 .hangup = moxa_hangup,
383 .break_ctl = moxa_break_ctl,
384 .tiocmget = moxa_tiocmget,
385 .tiocmset = moxa_tiocmset,
388 static struct tty_driver *moxaDriver;
389 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
390 static DEFINE_SPINLOCK(moxa_lock);
396 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
398 switch (brd->boardType) {
399 case MOXA_BOARD_C218_ISA:
400 case MOXA_BOARD_C218_PCI:
404 case MOXA_BOARD_CP204J:
418 static int moxa_check_fw(const void *ptr)
420 const __le16 *lptr = ptr;
422 if (*lptr != cpu_to_le16(0x7980))
428 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
431 void __iomem *baseAddr = brd->basemem;
434 writeb(HW_reset, baseAddr + Control_reg); /* reset */
436 memset_io(baseAddr, 0, 4096);
437 memcpy_toio(baseAddr, buf, len); /* download BIOS */
438 writeb(0, baseAddr + Control_reg); /* restart */
442 switch (brd->boardType) {
443 case MOXA_BOARD_C218_ISA:
444 case MOXA_BOARD_C218_PCI:
445 tmp = readw(baseAddr + C218_key);
446 if (tmp != C218_KeyCode)
449 case MOXA_BOARD_CP204J:
450 tmp = readw(baseAddr + C218_key);
451 if (tmp != CP204J_KeyCode)
455 tmp = readw(baseAddr + C320_key);
456 if (tmp != C320_KeyCode)
458 tmp = readw(baseAddr + C320_status);
459 if (tmp != STS_init) {
460 printk(KERN_ERR "moxa: bios upload failed -- CPU/Basic "
461 "module not found\n");
469 printk(KERN_ERR "moxa: bios upload failed -- board not found\n");
473 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
476 void __iomem *baseAddr = brd->basemem;
479 printk(KERN_ERR "moxa: invalid 320 bios -- too short\n");
483 writew(len - 7168 - 2, baseAddr + C320bapi_len);
484 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
485 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
486 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
487 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
492 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
495 void __iomem *baseAddr = brd->basemem;
496 const u16 *uptr = ptr;
497 size_t wlen, len2, j;
498 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
499 unsigned int i, retry, c320;
502 c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
503 brd->boardType == MOXA_BOARD_C320_ISA;
504 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
507 switch (brd->boardType) {
508 case MOXA_BOARD_CP204J:
509 case MOXA_BOARD_C218_ISA:
510 case MOXA_BOARD_C218_PCI:
512 loadbuf = C218_LoadBuf;
513 loadlen = C218DLoad_len;
514 checksum = C218check_sum;
515 checksum_ok = C218chksum_ok;
519 keycode = C320_KeyCode;
520 loadbuf = C320_LoadBuf;
521 loadlen = C320DLoad_len;
522 checksum = C320check_sum;
523 checksum_ok = C320chksum_ok;
529 for (i = 0; i < wlen; i++)
530 usum += le16_to_cpu(uptr[i]);
536 len2 = (wlen > 2048) ? 2048 : wlen;
538 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
541 writew(len2, baseAddr + loadlen);
542 writew(0, baseAddr + key);
543 for (i = 0; i < 100; i++) {
544 if (readw(baseAddr + key) == keycode)
548 if (readw(baseAddr + key) != keycode)
551 writew(0, baseAddr + loadlen);
552 writew(usum, baseAddr + checksum);
553 writew(0, baseAddr + key);
554 for (i = 0; i < 100; i++) {
555 if (readw(baseAddr + key) == keycode)
560 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
561 if (readb(baseAddr + checksum_ok) != 1)
564 writew(0, baseAddr + key);
565 for (i = 0; i < 600; i++) {
566 if (readw(baseAddr + Magic_no) == Magic_code)
570 if (readw(baseAddr + Magic_no) != Magic_code)
574 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
575 writew(0x3800, baseAddr + TMS320_PORT1);
576 writew(0x3900, baseAddr + TMS320_PORT2);
577 writew(28499, baseAddr + TMS320_CLOCK);
579 writew(0x3200, baseAddr + TMS320_PORT1);
580 writew(0x3400, baseAddr + TMS320_PORT2);
581 writew(19999, baseAddr + TMS320_CLOCK);
584 writew(1, baseAddr + Disable_IRQ);
585 writew(0, baseAddr + Magic_no);
586 for (i = 0; i < 500; i++) {
587 if (readw(baseAddr + Magic_no) == Magic_code)
591 if (readw(baseAddr + Magic_no) != Magic_code)
595 j = readw(baseAddr + Module_cnt);
598 brd->numPorts = j * 8;
599 writew(j, baseAddr + Module_no);
600 writew(0, baseAddr + Magic_no);
601 for (i = 0; i < 600; i++) {
602 if (readw(baseAddr + Magic_no) == Magic_code)
606 if (readw(baseAddr + Magic_no) != Magic_code)
609 brd->intNdx = baseAddr + IRQindex;
610 brd->intPend = baseAddr + IRQpending;
611 brd->intTable = baseAddr + IRQtable;
616 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
619 void __iomem *ofsAddr, *baseAddr = brd->basemem;
620 struct moxa_port *port;
624 printk(KERN_ERR "moxa: bios length is not even\n");
628 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
632 switch (brd->boardType) {
633 case MOXA_BOARD_C218_ISA:
634 case MOXA_BOARD_C218_PCI:
635 case MOXA_BOARD_CP204J:
637 for (i = 0; i < brd->numPorts; i++, port++) {
640 port->tableAddr = baseAddr + Extern_table +
642 ofsAddr = port->tableAddr;
643 writew(C218rx_mask, ofsAddr + RX_mask);
644 writew(C218tx_mask, ofsAddr + TX_mask);
645 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
646 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
648 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
649 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
655 for (i = 0; i < brd->numPorts; i++, port++) {
658 port->tableAddr = baseAddr + Extern_table +
660 ofsAddr = port->tableAddr;
661 switch (brd->numPorts) {
663 writew(C320p8rx_mask, ofsAddr + RX_mask);
664 writew(C320p8tx_mask, ofsAddr + TX_mask);
665 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
666 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
667 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
668 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
672 writew(C320p16rx_mask, ofsAddr + RX_mask);
673 writew(C320p16tx_mask, ofsAddr + TX_mask);
674 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
675 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
676 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
677 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
681 writew(C320p24rx_mask, ofsAddr + RX_mask);
682 writew(C320p24tx_mask, ofsAddr + TX_mask);
683 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
684 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
685 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
686 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
689 writew(C320p32rx_mask, ofsAddr + RX_mask);
690 writew(C320p32tx_mask, ofsAddr + TX_mask);
691 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
692 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
693 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
694 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
695 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
704 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
706 void *ptr = fw->data;
710 unsigned int a, lenp, lencnt;
713 __le32 magic; /* 0x34303430 */
715 u8 type; /* UNIX = 3 */
716 u8 model; /* C218T=1, C320T=2, CP204=3 */
721 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
723 if (fw->size < MOXA_FW_HDRLEN) {
724 strcpy(rsn, "too short (even header won't fit)");
727 if (hdr->magic != cpu_to_le32(0x30343034)) {
728 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
731 if (hdr->type != 3) {
732 sprintf(rsn, "not for linux, type is %u", hdr->type);
735 if (moxa_check_fw_model(brd, hdr->model)) {
736 sprintf(rsn, "not for this card, model is %u", hdr->model);
740 len = MOXA_FW_HDRLEN;
741 lencnt = hdr->model == 2 ? 5 : 3;
742 for (a = 0; a < ARRAY_SIZE(lens); a++) {
743 lens[a] = le16_to_cpu(hdr->len[a]);
744 if (lens[a] && len + lens[a] <= fw->size &&
745 moxa_check_fw(&fw->data[len]))
746 printk(KERN_WARNING "moxa firmware: unexpected input "
747 "at offset %u, but going on\n", (u32)len);
748 if (!lens[a] && a < lencnt) {
749 sprintf(rsn, "too few entries in fw file");
755 if (len != fw->size) {
756 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
761 ptr += MOXA_FW_HDRLEN;
764 strcpy(rsn, "read above");
766 ret = moxa_load_bios(brd, ptr, lens[lenp]);
770 /* we skip the tty section (lens[1]), since we don't need it */
771 ptr += lens[lenp] + lens[lenp + 1];
772 lenp += 2; /* comm */
774 if (hdr->model == 2) {
775 ret = moxa_load_320b(brd, ptr, lens[lenp]);
778 /* skip another tty */
779 ptr += lens[lenp] + lens[lenp + 1];
783 ret = moxa_load_code(brd, ptr, lens[lenp]);
789 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
793 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
795 const struct firmware *fw;
801 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
803 if (brd->ports == NULL) {
804 printk(KERN_ERR "cannot allocate memory for ports\n");
809 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
810 p->type = PORT_16550A;
811 p->close_delay = 5 * HZ / 10;
812 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
813 init_waitqueue_head(&p->open_wait);
814 init_completion(&p->close_wait);
816 setup_timer(&p->emptyTimer, moxa_check_xmit_empty,
820 switch (brd->boardType) {
821 case MOXA_BOARD_C218_ISA:
822 case MOXA_BOARD_C218_PCI:
823 file = "c218tunx.cod";
825 case MOXA_BOARD_CP204J:
826 file = "cp204unx.cod";
829 file = "c320tunx.cod";
833 ret = request_firmware(&fw, file, dev);
835 printk(KERN_ERR "request_firmware failed\n");
839 ret = moxa_load_fw(brd, fw);
841 release_firmware(fw);
848 if (!timer_pending(&moxaTimer))
849 mod_timer(&moxaTimer, jiffies + HZ / 50);
858 static void moxa_board_deinit(struct moxa_board_conf *brd)
863 for (i = 0; i < MAX_PORTS_PER_BOARD; i++)
864 del_timer_sync(&brd->ports[i].emptyTimer);
866 iounmap(brd->basemem);
872 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
873 const struct pci_device_id *ent)
875 struct moxa_board_conf *board;
877 int board_type = ent->driver_data;
880 retval = pci_enable_device(pdev);
882 dev_err(&pdev->dev, "can't enable pci device\n");
886 for (i = 0; i < MAX_BOARDS; i++)
887 if (moxa_boards[i].basemem == NULL)
891 if (i >= MAX_BOARDS) {
892 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
893 "found. Board is ignored.\n", MAX_BOARDS);
897 board = &moxa_boards[i];
899 retval = pci_request_region(pdev, 2, "moxa-base");
901 dev_err(&pdev->dev, "can't request pci region 2\n");
905 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
906 if (board->basemem == NULL) {
907 dev_err(&pdev->dev, "can't remap io space 2\n");
911 board->boardType = board_type;
912 switch (board_type) {
913 case MOXA_BOARD_C218_ISA:
914 case MOXA_BOARD_C218_PCI:
918 case MOXA_BOARD_CP204J:
925 board->busType = MOXA_BUS_TYPE_PCI;
927 retval = moxa_init_board(board, &pdev->dev);
931 pci_set_drvdata(pdev, board);
935 iounmap(board->basemem);
936 board->basemem = NULL;
938 pci_release_region(pdev, 2);
943 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
945 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
947 moxa_board_deinit(brd);
949 pci_release_region(pdev, 2);
952 static struct pci_driver moxa_pci_driver = {
954 .id_table = moxa_pcibrds,
955 .probe = moxa_pci_probe,
956 .remove = __devexit_p(moxa_pci_remove)
958 #endif /* CONFIG_PCI */
960 static int __init moxa_init(void)
962 unsigned int isabrds = 0;
965 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
967 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
971 moxaDriver->owner = THIS_MODULE;
972 moxaDriver->name = "ttyMX";
973 moxaDriver->major = ttymajor;
974 moxaDriver->minor_start = 0;
975 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
976 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
977 moxaDriver->init_termios = tty_std_termios;
978 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
979 moxaDriver->init_termios.c_ispeed = 9600;
980 moxaDriver->init_termios.c_ospeed = 9600;
981 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
982 tty_set_operations(moxaDriver, &moxa_ops);
984 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
986 if (tty_register_driver(moxaDriver)) {
987 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
988 put_tty_driver(moxaDriver);
992 /* Find the boards defined from module args. */
995 struct moxa_board_conf *brd = moxa_boards;
997 for (i = 0; i < MAX_BOARDS; i++) {
1000 if (type[i] == MOXA_BOARD_C218_ISA ||
1001 type[i] == MOXA_BOARD_C320_ISA) {
1002 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1003 isabrds + 1, moxa_brdname[type[i] - 1],
1005 brd->boardType = type[i];
1006 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1008 brd->busType = MOXA_BUS_TYPE_ISA;
1009 brd->basemem = ioremap(baseaddr[i], 0x4000);
1010 if (!brd->basemem) {
1011 printk(KERN_ERR "moxa: can't remap %lx\n",
1015 if (moxa_init_board(brd, NULL)) {
1016 iounmap(brd->basemem);
1017 brd->basemem = NULL;
1029 retval = pci_register_driver(&moxa_pci_driver);
1031 printk(KERN_ERR "Can't register moxa pci driver!\n");
1040 static void __exit moxa_exit(void)
1044 del_timer_sync(&moxaTimer);
1046 if (tty_unregister_driver(moxaDriver))
1047 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1049 put_tty_driver(moxaDriver);
1052 pci_unregister_driver(&moxa_pci_driver);
1055 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1056 if (moxa_boards[i].ready)
1057 moxa_board_deinit(&moxa_boards[i]);
1060 module_init(moxa_init);
1061 module_exit(moxa_exit);
1063 static int moxa_open(struct tty_struct *tty, struct file *filp)
1065 struct moxa_board_conf *brd;
1066 struct moxa_port *ch;
1071 if (port == MAX_PORTS) {
1072 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1074 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1078 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1080 tty->driver_data = ch;
1082 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
1083 ch->statusflags = 0;
1084 moxa_set_tty_param(tty, tty->termios);
1085 MoxaPortLineCtrl(ch, 1, 1);
1087 ch->asyncflags |= ASYNC_INITIALIZED;
1089 retval = moxa_block_till_ready(tty, filp, ch);
1091 moxa_unthrottle(tty);
1093 if (ch->type == PORT_16550A) {
1102 static void moxa_close(struct tty_struct *tty, struct file *filp)
1104 struct moxa_port *ch;
1108 if (port == MAX_PORTS) {
1111 if (tty->driver_data == NULL) {
1114 if (tty_hung_up_p(filp)) {
1117 ch = (struct moxa_port *) tty->driver_data;
1119 if ((tty->count == 1) && (ch->count != 1)) {
1120 printk(KERN_WARNING "moxa_close: bad serial port count; "
1121 "tty->count is 1, ch->count is %d\n", ch->count);
1124 if (--ch->count < 0) {
1125 printk(KERN_WARNING "moxa_close: bad serial port count, "
1126 "device=%s\n", tty->name);
1132 ch->asyncflags |= ASYNC_CLOSING;
1134 ch->cflag = tty->termios->c_cflag;
1135 if (ch->asyncflags & ASYNC_INITIALIZED) {
1136 moxa_setup_empty_event(tty);
1137 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
1138 del_timer_sync(&ch->emptyTimer);
1141 MoxaPortFlushData(ch, 2);
1143 if (tty->driver->flush_buffer)
1144 tty->driver->flush_buffer(tty);
1145 tty_ldisc_flush(tty);
1149 if (ch->blocked_open) {
1150 if (ch->close_delay) {
1151 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
1153 wake_up_interruptible(&ch->open_wait);
1155 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1156 complete_all(&ch->close_wait);
1159 static int moxa_write(struct tty_struct *tty,
1160 const unsigned char *buf, int count)
1162 struct moxa_port *ch = tty->driver_data;
1163 unsigned long flags;
1169 spin_lock_irqsave(&moxa_lock, flags);
1170 len = MoxaPortWriteData(ch, (unsigned char *) buf, count);
1171 spin_unlock_irqrestore(&moxa_lock, flags);
1173 /*********************************************
1174 if ( !(ch->statusflags & LOWWAIT) &&
1175 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1176 ************************************************/
1177 ch->statusflags |= LOWWAIT;
1181 static int moxa_write_room(struct tty_struct *tty)
1183 struct moxa_port *ch;
1187 ch = tty->driver_data;
1190 return MoxaPortTxFree(ch);
1193 static void moxa_flush_buffer(struct tty_struct *tty)
1195 struct moxa_port *ch = tty->driver_data;
1199 MoxaPortFlushData(ch, 1);
1203 static int moxa_chars_in_buffer(struct tty_struct *tty)
1205 struct moxa_port *ch = tty->driver_data;
1209 * Sigh...I have to check if driver_data is NULL here, because
1210 * if an open() fails, the TTY subsystem eventually calls
1211 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1212 * routine. And since the open() failed, we return 0 here. TDJ
1216 chars = MoxaPortTxQueue(ch);
1219 * Make it possible to wakeup anything waiting for output
1220 * in tty_ioctl.c, etc.
1222 if (!(ch->statusflags & EMPTYWAIT))
1223 moxa_setup_empty_event(tty);
1228 static void moxa_flush_chars(struct tty_struct *tty)
1231 * Don't think I need this, because this is called to empty the TX
1232 * buffer for the 16450, 16550, etc.
1236 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
1238 struct moxa_port *ch = tty->driver_data;
1239 unsigned long flags;
1243 spin_lock_irqsave(&moxa_lock, flags);
1244 MoxaPortWriteData(ch, &c, 1);
1245 spin_unlock_irqrestore(&moxa_lock, flags);
1246 /************************************************
1247 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1248 *************************************************/
1249 ch->statusflags |= LOWWAIT;
1252 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1254 struct moxa_port *ch = tty->driver_data;
1255 int flag = 0, dtr, rts;
1260 MoxaPortGetLineOut(ch, &dtr, &rts);
1265 dtr = MoxaPortLineStatus(ch);
1275 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1276 unsigned int set, unsigned int clear)
1278 struct moxa_port *ch = tty->driver_data;
1286 MoxaPortGetLineOut(ch, &dtr, &rts);
1287 if (set & TIOCM_RTS)
1289 if (set & TIOCM_DTR)
1291 if (clear & TIOCM_RTS)
1293 if (clear & TIOCM_DTR)
1295 MoxaPortLineCtrl(ch, dtr, rts);
1299 static void moxa_throttle(struct tty_struct *tty)
1301 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1303 ch->statusflags |= THROTTLE;
1306 static void moxa_unthrottle(struct tty_struct *tty)
1308 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1310 ch->statusflags &= ~THROTTLE;
1313 static void moxa_set_termios(struct tty_struct *tty,
1314 struct ktermios *old_termios)
1316 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1320 moxa_set_tty_param(tty, old_termios);
1321 if (!(old_termios->c_cflag & CLOCAL) &&
1322 (tty->termios->c_cflag & CLOCAL))
1323 wake_up_interruptible(&ch->open_wait);
1326 static void moxa_stop(struct tty_struct *tty)
1328 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1332 MoxaPortTxDisable(ch);
1333 ch->statusflags |= TXSTOPPED;
1337 static void moxa_start(struct tty_struct *tty)
1339 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1344 if (!(ch->statusflags & TXSTOPPED))
1347 MoxaPortTxEnable(ch);
1348 ch->statusflags &= ~TXSTOPPED;
1351 static void moxa_hangup(struct tty_struct *tty)
1353 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1355 moxa_flush_buffer(tty);
1358 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1360 wake_up_interruptible(&ch->open_wait);
1363 static void moxa_poll(unsigned long ignored)
1365 struct moxa_port *ch;
1366 struct tty_struct *tty;
1370 del_timer(&moxaTimer);
1372 if (MoxaDriverPoll() < 0) {
1373 mod_timer(&moxaTimer, jiffies + HZ / 50);
1377 for (card = 0; card < MAX_BOARDS; card++) {
1378 if (!moxa_boards[card].ready)
1380 ch = moxa_boards[card].ports;
1381 for (i = 0; i < moxa_boards[card].numPorts; i++, ch++) {
1382 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
1384 if (!(ch->statusflags & THROTTLE) &&
1385 (MoxaPortRxQueue(ch) > 0))
1386 moxa_receive_data(ch);
1390 if (ch->statusflags & LOWWAIT) {
1391 if (MoxaPortTxQueue(ch) <= WAKEUP_CHARS) {
1392 if (!tty->stopped) {
1393 ch->statusflags &= ~LOWWAIT;
1398 if (!I_IGNBRK(tty) && (MoxaPortResetBrkCnt(ch) > 0)) {
1399 tty_insert_flip_char(tty, 0, TTY_BREAK);
1400 tty_schedule_flip(tty);
1402 if (MoxaPortDCDChange(ch)) {
1403 if (ch->asyncflags & ASYNC_CHECK_CD) {
1404 if (MoxaPortDCDON(ch))
1405 wake_up_interruptible(&ch->open_wait);
1408 wake_up_interruptible(&ch->open_wait);
1409 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1416 mod_timer(&moxaTimer, jiffies + HZ / 50);
1419 /******************************************************************************/
1421 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1423 register struct ktermios *ts;
1424 struct moxa_port *ch;
1425 int rts, cts, txflow, rxflow, xany, baud;
1427 ch = (struct moxa_port *) tty->driver_data;
1429 if (ts->c_cflag & CLOCAL)
1430 ch->asyncflags &= ~ASYNC_CHECK_CD;
1432 ch->asyncflags |= ASYNC_CHECK_CD;
1433 rts = cts = txflow = rxflow = xany = 0;
1434 if (ts->c_cflag & CRTSCTS)
1436 if (ts->c_iflag & IXON)
1438 if (ts->c_iflag & IXOFF)
1440 if (ts->c_iflag & IXANY)
1443 /* Clear the features we don't support */
1444 ts->c_cflag &= ~CMSPAR;
1445 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1446 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1448 baud = tty_termios_baud_rate(old_termios);
1449 /* Not put the baud rate into the termios data */
1450 tty_encode_baud_rate(tty, baud, baud);
1453 static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1454 struct moxa_port *ch)
1456 DECLARE_WAITQUEUE(wait,current);
1457 unsigned long flags;
1459 int do_clocal = C_CLOCAL(tty);
1462 * If the device is in the middle of being closed, then block
1463 * until it's done, and then try again.
1465 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1466 if (ch->asyncflags & ASYNC_CLOSING)
1467 wait_for_completion_interruptible(&ch->close_wait);
1468 #ifdef SERIAL_DO_RESTART
1469 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1472 return (-ERESTARTSYS);
1478 * If non-blocking mode is set, then make the check up front
1481 if (filp->f_flags & O_NONBLOCK) {
1482 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1486 * Block waiting for the carrier detect and the line to become free
1489 add_wait_queue(&ch->open_wait, &wait);
1490 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
1491 tty->index, ch->count);
1492 spin_lock_irqsave(&moxa_lock, flags);
1493 if (!tty_hung_up_p(filp))
1496 spin_unlock_irqrestore(&moxa_lock, flags);
1499 set_current_state(TASK_INTERRUPTIBLE);
1500 if (tty_hung_up_p(filp) ||
1501 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1502 #ifdef SERIAL_DO_RESTART
1503 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1506 retval = -ERESTARTSYS;
1512 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1516 if (signal_pending(current)) {
1517 retval = -ERESTARTSYS;
1522 set_current_state(TASK_RUNNING);
1523 remove_wait_queue(&ch->open_wait, &wait);
1525 spin_lock_irqsave(&moxa_lock, flags);
1526 if (!tty_hung_up_p(filp))
1529 spin_unlock_irqrestore(&moxa_lock, flags);
1530 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
1531 tty->index, ch->count);
1534 /* FIXME: review to see if we need to use set_bit on these */
1535 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1539 static void moxa_setup_empty_event(struct tty_struct *tty)
1541 struct moxa_port *ch = tty->driver_data;
1542 unsigned long flags;
1544 spin_lock_irqsave(&moxa_lock, flags);
1545 ch->statusflags |= EMPTYWAIT;
1546 mod_timer(&ch->emptyTimer, jiffies + HZ);
1547 spin_unlock_irqrestore(&moxa_lock, flags);
1550 static void moxa_check_xmit_empty(unsigned long data)
1552 struct moxa_port *ch;
1554 ch = (struct moxa_port *) data;
1555 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1556 if (MoxaPortTxQueue(ch) == 0) {
1557 ch->statusflags &= ~EMPTYWAIT;
1558 tty_wakeup(ch->tty);
1561 mod_timer(&ch->emptyTimer, round_jiffies(jiffies + HZ));
1563 ch->statusflags &= ~EMPTYWAIT;
1566 static void moxa_shut_down(struct moxa_port *ch)
1568 struct tty_struct *tp;
1570 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1575 MoxaPortDisable(ch);
1578 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1580 if (tp->termios->c_cflag & HUPCL)
1581 MoxaPortLineCtrl(ch, 0, 0);
1583 ch->asyncflags &= ~ASYNC_INITIALIZED;
1586 static void moxa_receive_data(struct moxa_port *ch)
1588 struct tty_struct *tp;
1589 struct ktermios *ts;
1590 unsigned long flags;
1596 /**************************************************
1597 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1598 *****************************************************/
1600 MoxaPortFlushData(ch, 0);
1603 spin_lock_irqsave(&moxa_lock, flags);
1604 MoxaPortReadData(ch, tp);
1605 spin_unlock_irqrestore(&moxa_lock, flags);
1606 tty_schedule_flip(tp);
1613 #define DCD_changed 0x01
1614 #define DCD_oldstate 0x80
1616 static int moxaLowWaterChk;
1618 static void moxa_low_water_check(void __iomem *);
1620 /*****************************************************************************
1621 * Driver level functions: *
1622 * 3. MoxaDriverPoll(void); *
1623 *****************************************************************************/
1625 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1627 void __iomem *ofsAddr;
1628 if ((mode < 0) || (mode > 2))
1630 ofsAddr = port->tableAddr;
1631 moxafunc(ofsAddr, FC_FlushQueue, mode);
1633 port->lowChkFlag = 0;
1634 moxa_low_water_check(ofsAddr);
1638 static int MoxaDriverPoll(void)
1640 struct moxa_board_conf *brd;
1641 struct moxa_port *p;
1642 void __iomem *ofsAddr;
1644 unsigned int port, ports, card;
1647 for (card = 0; card < MAX_BOARDS; card++) {
1648 brd = &moxa_boards[card];
1649 if (brd->ready == 0)
1651 if ((ports = brd->numPorts) == 0)
1653 if (readb(brd->intPend) == 0xff) {
1654 ip = brd->intTable + readb(brd->intNdx);
1657 for (port = 0; port < ports; port += 2, p++) {
1658 temp = readw(ip + port);
1662 writew(0, ip + port);
1663 ofsAddr = p->tableAddr;
1665 writew(readw(ofsAddr + HostStat) &
1666 ~WakeupTx, ofsAddr + HostStat);
1667 if (temp & IntrBreak)
1670 if (temp & IntrLine) {
1671 if (readb(ofsAddr + FlagStat) & DCD_state) {
1672 if ((p->DCDState & DCD_oldstate) == 0)
1673 p->DCDState = (DCD_oldstate |
1676 if (p->DCDState & DCD_oldstate)
1677 p->DCDState = DCD_changed;
1681 writeb(0, brd->intPend);
1683 if (moxaLowWaterChk) {
1685 for (port = 0; port < ports; port++, p++) {
1686 if (p->lowChkFlag) {
1688 ofsAddr = p->tableAddr;
1689 moxa_low_water_check(ofsAddr);
1694 moxaLowWaterChk = 0;
1699 /*****************************************************************************
1700 * Port level functions: *
1701 * 2. MoxaPortEnable(int port); *
1702 * 3. MoxaPortDisable(int port); *
1703 * 4. MoxaPortGetMaxBaud(int port); *
1704 * 6. MoxaPortSetBaud(int port, long baud); *
1705 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1706 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1707 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1708 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1709 * 12. MoxaPortLineStatus(int port); *
1710 * 13. MoxaPortDCDChange(int port); *
1711 * 14. MoxaPortDCDON(int port); *
1712 * 15. MoxaPortFlushData(int port, int mode); *
1713 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1714 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1715 * 20. MoxaPortTxQueue(int port); *
1716 * 21. MoxaPortTxFree(int port); *
1717 * 22. MoxaPortRxQueue(int port); *
1718 * 24. MoxaPortTxDisable(int port); *
1719 * 25. MoxaPortTxEnable(int port); *
1720 * 27. MoxaPortResetBrkCnt(int port); *
1721 *****************************************************************************/
1723 * Moxa Port Number Description:
1725 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1726 * the port number using in MOXA driver functions will be 0 to 31 for
1727 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1728 * to 127 for fourth. For example, if you setup three MOXA boards,
1729 * first board is C218, second board is C320-16 and third board is
1730 * C320-32. The port number of first board (C218 - 8 ports) is from
1731 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1732 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1733 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1734 * 127 will be invalid.
1737 * Moxa Functions Description:
1739 * Function 1: Driver initialization routine, this routine must be
1740 * called when initialized driver.
1742 * void MoxaDriverInit();
1745 * Function 2: Moxa driver private IOCTL command processing.
1747 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1749 * unsigned int cmd : IOCTL command
1750 * unsigned long arg : IOCTL argument
1751 * int port : port number (0 - 127)
1758 * Function 3: Moxa driver polling process routine.
1760 * int MoxaDriverPoll(void);
1762 * return: 0 ; polling O.K.
1763 * -1 : no any Moxa card.
1766 * Function 6: Enable this port to start Tx/Rx data.
1768 * void MoxaPortEnable(int port);
1769 * int port : port number (0 - 127)
1772 * Function 7: Disable this port
1774 * void MoxaPortDisable(int port);
1775 * int port : port number (0 - 127)
1778 * Function 8: Get the maximun available baud rate of this port.
1780 * long MoxaPortGetMaxBaud(int port);
1781 * int port : port number (0 - 127)
1783 * return: 0 : this port is invalid
1784 * 38400/57600/115200 bps
1787 * Function 10: Setting baud rate of this port.
1789 * long MoxaPortSetBaud(int port, long baud);
1790 * int port : port number (0 - 127)
1791 * long baud : baud rate (50 - 115200)
1793 * return: 0 : this port is invalid or baud < 50
1794 * 50 - 115200 : the real baud rate set to the port, if
1795 * the argument baud is large than maximun
1796 * available baud rate, the real setting
1797 * baud rate will be the maximun baud rate.
1800 * Function 12: Configure the port.
1802 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1803 * int port : port number (0 - 127)
1804 * struct ktermios * termio : termio structure pointer
1805 * speed_t baud : baud rate
1807 * return: -1 : this port is invalid or termio == NULL
1811 * Function 13: Get the DTR/RTS state of this port.
1813 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1814 * int port : port number (0 - 127)
1815 * int * dtrState : pointer to INT to receive the current DTR
1816 * state. (if NULL, this function will not
1817 * write to this address)
1818 * int * rtsState : pointer to INT to receive the current RTS
1819 * state. (if NULL, this function will not
1820 * write to this address)
1822 * return: -1 : this port is invalid
1826 * Function 14: Setting the DTR/RTS output state of this port.
1828 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1829 * int port : port number (0 - 127)
1830 * int dtrState : DTR output state (0: off, 1: on)
1831 * int rtsState : RTS output state (0: off, 1: on)
1834 * Function 15: Setting the flow control of this port.
1836 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1837 * int txFlow,int xany);
1838 * int port : port number (0 - 127)
1839 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1840 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1841 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1842 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1843 * int xany : S/W XANY flow control (0: no, 1: yes)
1846 * Function 16: Get ths line status of this port
1848 * int MoxaPortLineStatus(int port);
1849 * int port : port number (0 - 127)
1851 * return: Bit 0 - CTS state (0: off, 1: on)
1852 * Bit 1 - DSR state (0: off, 1: on)
1853 * Bit 2 - DCD state (0: off, 1: on)
1856 * Function 17: Check the DCD state has changed since the last read
1859 * int MoxaPortDCDChange(int port);
1860 * int port : port number (0 - 127)
1862 * return: 0 : no changed
1863 * 1 : DCD has changed
1866 * Function 18: Check ths current DCD state is ON or not.
1868 * int MoxaPortDCDON(int port);
1869 * int port : port number (0 - 127)
1871 * return: 0 : DCD off
1875 * Function 19: Flush the Rx/Tx buffer data of this port.
1877 * void MoxaPortFlushData(int port, int mode);
1878 * int port : port number (0 - 127)
1880 * 0 : flush the Rx buffer
1881 * 1 : flush the Tx buffer
1882 * 2 : flush the Rx and Tx buffer
1885 * Function 20: Write data.
1887 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1888 * int port : port number (0 - 127)
1889 * unsigned char * buffer : pointer to write data buffer.
1890 * int length : write data length
1892 * return: 0 - length : real write data length
1895 * Function 21: Read data.
1897 * int MoxaPortReadData(int port, struct tty_struct *tty);
1898 * int port : port number (0 - 127)
1899 * struct tty_struct *tty : tty for data
1901 * return: 0 - length : real read data length
1904 * Function 24: Get the Tx buffer current queued data bytes
1906 * int MoxaPortTxQueue(int port);
1907 * int port : port number (0 - 127)
1909 * return: .. : Tx buffer current queued data bytes
1912 * Function 25: Get the Tx buffer current free space
1914 * int MoxaPortTxFree(int port);
1915 * int port : port number (0 - 127)
1917 * return: .. : Tx buffer current free space
1920 * Function 26: Get the Rx buffer current queued data bytes
1922 * int MoxaPortRxQueue(int port);
1923 * int port : port number (0 - 127)
1925 * return: .. : Rx buffer current queued data bytes
1928 * Function 28: Disable port data transmission.
1930 * void MoxaPortTxDisable(int port);
1931 * int port : port number (0 - 127)
1934 * Function 29: Enable port data transmission.
1936 * void MoxaPortTxEnable(int port);
1937 * int port : port number (0 - 127)
1940 * Function 31: Get the received BREAK signal count and reset it.
1942 * int MoxaPortResetBrkCnt(int port);
1943 * int port : port number (0 - 127)
1945 * return: 0 - .. : BREAK signal count
1950 static void MoxaPortEnable(struct moxa_port *port)
1952 void __iomem *ofsAddr;
1953 short lowwater = 512;
1955 ofsAddr = port->tableAddr;
1956 writew(lowwater, ofsAddr + Low_water);
1958 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1959 port->board->boardType == MOXA_BOARD_C320_PCI) {
1960 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1962 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1965 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1966 moxafunc(ofsAddr, FC_FlushQueue, 2);
1968 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1969 MoxaPortLineStatus(port);
1972 static void MoxaPortDisable(struct moxa_port *port)
1974 void __iomem *ofsAddr = port->tableAddr;
1976 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1977 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1978 writew(0, ofsAddr + HostStat);
1979 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1982 static long MoxaPortGetMaxBaud(struct moxa_port *port)
1984 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1985 port->board->boardType == MOXA_BOARD_C320_PCI)
1992 static long MoxaPortSetBaud(struct moxa_port *port, long baud)
1994 void __iomem *ofsAddr;
1998 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2000 ofsAddr = port->tableAddr;
2004 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2005 else if (max == 57600L)
2006 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2008 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2010 moxafunc(ofsAddr, FC_SetBaud, val);
2015 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
2018 void __iomem *ofsAddr;
2022 ofsAddr = port->tableAddr;
2023 cflag = termio->c_cflag; /* termio->c_cflag */
2025 mode = termio->c_cflag & CSIZE;
2028 else if (mode == CS6)
2030 else if (mode == CS7)
2032 else if (mode == CS8)
2035 if (termio->c_cflag & CSTOPB) {
2043 if (termio->c_cflag & PARENB) {
2044 if (termio->c_cflag & PARODD)
2051 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2053 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2054 port->board->boardType == MOXA_BOARD_C320_PCI) {
2055 if (baud >= 921600L)
2058 baud = MoxaPortSetBaud(port, baud);
2060 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2061 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2062 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2063 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2064 moxa_wait_finish(ofsAddr);
2070 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
2075 *dtrState = !!(port->lineCtrl & DTR_ON);
2077 *rtsState = !!(port->lineCtrl & RTS_ON);
2082 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
2090 port->lineCtrl = mode;
2091 moxafunc(port->tableAddr, FC_LineControl, mode);
2094 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
2095 int txflow, int rxflow, int txany)
2100 mode |= RTS_FlowCtl;
2102 mode |= CTS_FlowCtl;
2109 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
2112 static int MoxaPortLineStatus(struct moxa_port *port)
2114 void __iomem *ofsAddr;
2117 ofsAddr = port->tableAddr;
2118 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2119 port->board->boardType == MOXA_BOARD_C320_PCI) {
2120 moxafunc(ofsAddr, FC_LineStatus, 0);
2121 val = readw(ofsAddr + FuncArg);
2123 val = readw(ofsAddr + FlagStat) >> 4;
2128 if ((port->DCDState & DCD_oldstate) == 0)
2129 port->DCDState = (DCD_oldstate | DCD_changed);
2131 if (port->DCDState & DCD_oldstate)
2132 port->DCDState = DCD_changed;
2138 static int MoxaPortDCDChange(struct moxa_port *port)
2143 port->DCDState &= ~DCD_changed;
2148 static int MoxaPortDCDON(struct moxa_port *port)
2152 if (port->DCDState & DCD_oldstate)
2159 static int MoxaPortWriteData(struct moxa_port *port, unsigned char *buffer,
2165 ushort head, tx_mask, spage, epage;
2166 ushort pageno, pageofs, bufhead;
2167 void __iomem *baseAddr, *ofsAddr, *ofs;
2169 ofsAddr = port->tableAddr;
2170 baseAddr = port->board->basemem;
2171 tx_mask = readw(ofsAddr + TX_mask);
2172 spage = readw(ofsAddr + Page_txb);
2173 epage = readw(ofsAddr + EndPage_txb);
2174 tail = readw(ofsAddr + TXwptr);
2175 head = readw(ofsAddr + TXrptr);
2176 c = (head > tail) ? (head - tail - 1)
2177 : (head - tail + tx_mask);
2180 moxaLog.txcnt[port->tty->index] += c;
2182 if (spage == epage) {
2183 bufhead = readw(ofsAddr + Ofs_txb);
2184 writew(spage, baseAddr + Control_reg);
2187 len = head - tail - 1;
2189 len = tx_mask + 1 - tail;
2190 len = (c > len) ? len : c;
2191 ofs = baseAddr + DynPage_addr + bufhead + tail;
2192 for (i = 0; i < len; i++)
2193 writeb(*buffer++, ofs + i);
2194 tail = (tail + len) & tx_mask;
2197 writew(tail, ofsAddr + TXwptr);
2200 pageno = spage + (tail >> 13);
2201 pageofs = tail & Page_mask;
2203 cnt = Page_size - pageofs;
2207 writeb(pageno, baseAddr + Control_reg);
2208 ofs = baseAddr + DynPage_addr + pageofs;
2209 for (i = 0; i < cnt; i++)
2210 writeb(*buffer++, ofs + i);
2212 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2215 if (++pageno == epage)
2220 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2224 static int MoxaPortReadData(struct moxa_port *port, struct tty_struct *tty)
2226 register ushort head, pageofs;
2227 int i, count, cnt, len, total, remain;
2228 ushort tail, rx_mask, spage, epage;
2229 ushort pageno, bufhead;
2230 void __iomem *baseAddr, *ofsAddr, *ofs;
2232 ofsAddr = port->tableAddr;
2233 baseAddr = port->board->basemem;
2234 head = readw(ofsAddr + RXrptr);
2235 tail = readw(ofsAddr + RXwptr);
2236 rx_mask = readw(ofsAddr + RX_mask);
2237 spage = readw(ofsAddr + Page_rxb);
2238 epage = readw(ofsAddr + EndPage_rxb);
2239 count = (tail >= head) ? (tail - head)
2240 : (tail - head + rx_mask + 1);
2245 remain = count - total;
2246 moxaLog.rxcnt[port->tty->index] += total;
2248 if (spage == epage) {
2249 bufhead = readw(ofsAddr + Ofs_rxb);
2250 writew(spage, baseAddr + Control_reg);
2255 len = rx_mask + 1 - head;
2256 len = (count > len) ? len : count;
2257 ofs = baseAddr + DynPage_addr + bufhead + head;
2258 for (i = 0; i < len; i++)
2259 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2260 head = (head + len) & rx_mask;
2263 writew(head, ofsAddr + RXrptr);
2266 pageno = spage + (head >> 13);
2267 pageofs = head & Page_mask;
2269 cnt = Page_size - pageofs;
2273 writew(pageno, baseAddr + Control_reg);
2274 ofs = baseAddr + DynPage_addr + pageofs;
2275 for (i = 0; i < cnt; i++)
2276 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2278 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2281 if (++pageno == epage)
2286 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2287 moxaLowWaterChk = 1;
2288 port->lowChkFlag = 1;
2294 static int MoxaPortTxQueue(struct moxa_port *port)
2296 void __iomem *ofsAddr = port->tableAddr;
2297 ushort rptr, wptr, mask;
2300 rptr = readw(ofsAddr + TXrptr);
2301 wptr = readw(ofsAddr + TXwptr);
2302 mask = readw(ofsAddr + TX_mask);
2303 len = (wptr - rptr) & mask;
2307 static int MoxaPortTxFree(struct moxa_port *port)
2309 void __iomem *ofsAddr = port->tableAddr;
2310 ushort rptr, wptr, mask;
2313 rptr = readw(ofsAddr + TXrptr);
2314 wptr = readw(ofsAddr + TXwptr);
2315 mask = readw(ofsAddr + TX_mask);
2316 len = mask - ((wptr - rptr) & mask);
2320 static int MoxaPortRxQueue(struct moxa_port *port)
2322 void __iomem *ofsAddr = port->tableAddr;
2323 ushort rptr, wptr, mask;
2326 rptr = readw(ofsAddr + RXrptr);
2327 wptr = readw(ofsAddr + RXwptr);
2328 mask = readw(ofsAddr + RX_mask);
2329 len = (wptr - rptr) & mask;
2334 static void MoxaPortTxDisable(struct moxa_port *port)
2336 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2339 static void MoxaPortTxEnable(struct moxa_port *port)
2341 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2345 static int MoxaPortResetBrkCnt(struct moxa_port *port)
2348 cnt = port->breakCnt;
2353 static int moxa_get_serial_info(struct moxa_port *info,
2354 struct serial_struct __user *retinfo)
2356 struct serial_struct tmp;
2358 memset(&tmp, 0, sizeof(tmp));
2359 tmp.type = info->type;
2360 tmp.line = info->tty->index;
2363 tmp.flags = info->asyncflags;
2364 tmp.baud_base = 921600;
2365 tmp.close_delay = info->close_delay;
2366 tmp.custom_divisor = 0;
2368 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2374 static int moxa_set_serial_info(struct moxa_port *info,
2375 struct serial_struct __user *new_info)
2377 struct serial_struct new_serial;
2379 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2382 if ((new_serial.irq != 0) ||
2383 (new_serial.port != 0) ||
2384 // (new_serial.type != info->type) ||
2385 (new_serial.custom_divisor != 0) ||
2386 (new_serial.baud_base != 921600))
2389 if (!capable(CAP_SYS_ADMIN)) {
2390 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2391 (info->asyncflags & ~ASYNC_USR_MASK)))
2394 info->close_delay = new_serial.close_delay * HZ / 100;
2397 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2398 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2400 if (new_serial.type == PORT_16550A) {
2401 MoxaSetFifo(info, 1);
2403 MoxaSetFifo(info, 0);
2406 info->type = new_serial.type;
2412 /*****************************************************************************
2413 * Static local functions: *
2414 *****************************************************************************/
2416 static void moxa_low_water_check(void __iomem *ofsAddr)
2419 ushort rptr, wptr, mask;
2421 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2422 rptr = readw(ofsAddr + RXrptr);
2423 wptr = readw(ofsAddr + RXwptr);
2424 mask = readw(ofsAddr + RX_mask);
2425 len = (wptr - rptr) & mask;
2426 if (len <= Low_water)
2427 moxafunc(ofsAddr, FC_SendXon, 0);
2431 static void MoxaSetFifo(struct moxa_port *port, int enable)
2433 void __iomem *ofsAddr = port->tableAddr;
2436 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2437 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2439 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2440 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);