1 /*****************************************************************************/
3 * moxa.c -- MOXA Intellio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com).
6 * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
8 * This code is loosely based on the Linux serial driver, written by
9 * Linus Torvalds, Theodore T'so and others.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
18 * MOXA Intellio Series Driver
24 #include <linux/module.h>
25 #include <linux/types.h>
27 #include <linux/ioport.h>
28 #include <linux/errno.h>
29 #include <linux/firmware.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/timer.h>
33 #include <linux/interrupt.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/major.h>
37 #include <linux/string.h>
38 #include <linux/fcntl.h>
39 #include <linux/ptrace.h>
40 #include <linux/serial.h>
41 #include <linux/tty_driver.h>
42 #include <linux/delay.h>
43 #include <linux/pci.h>
44 #include <linux/init.h>
45 #include <linux/bitops.h>
47 #include <asm/system.h>
49 #include <asm/uaccess.h>
53 #define MOXA_VERSION "6.0k"
55 #define MOXA_FW_HDRLEN 32
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)
63 #define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
64 (brd)->boardType == MOXA_BOARD_C320_PCI)
67 * Define the Moxa PCI vendor and device IDs.
69 #define MOXA_BUS_TYPE_ISA 0
70 #define MOXA_BUS_TYPE_PCI 1
73 MOXA_BOARD_C218_PCI = 1,
80 static char *moxa_brdname[] =
82 "C218 Turbo PCI series",
83 "C218 Turbo ISA series",
84 "C320 Turbo PCI series",
85 "C320 Turbo ISA series",
90 static struct pci_device_id moxa_pcibrds[] = {
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
92 .driver_data = MOXA_BOARD_C218_PCI },
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
94 .driver_data = MOXA_BOARD_C320_PCI },
95 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
96 .driver_data = MOXA_BOARD_CP204J },
99 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
100 #endif /* CONFIG_PCI */
104 static struct moxa_board_conf {
111 struct moxa_port *ports;
113 void __iomem *basemem;
114 void __iomem *intNdx;
115 void __iomem *intPend;
116 void __iomem *intTable;
117 } moxa_boards[MAX_BOARDS];
119 struct mxser_mstatus {
133 struct moxa_board_conf *board;
134 struct tty_struct *tty;
135 void __iomem *tableAddr;
142 unsigned long statusflags;
143 wait_queue_head_t open_wait;
152 int rxcnt[MAX_PORTS];
153 int txcnt[MAX_PORTS];
157 #define TXSTOPPED 0x1
159 #define EMPTYWAIT 0x4
162 #define SERIAL_DO_RESTART
164 #define WAKEUP_CHARS 256
166 static int ttymajor = MOXAMAJOR;
167 static struct mon_str moxaLog;
168 static unsigned int moxaFuncTout = HZ / 2;
169 static unsigned int moxaLowWaterChk;
170 static DEFINE_MUTEX(moxa_openlock);
171 /* Variables for insmod */
173 static unsigned long baseaddr[MAX_BOARDS];
174 static unsigned int type[MAX_BOARDS];
175 static unsigned int numports[MAX_BOARDS];
178 MODULE_AUTHOR("William Chen");
179 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
180 MODULE_LICENSE("GPL");
182 module_param_array(type, uint, NULL, 0);
183 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
184 module_param_array(baseaddr, ulong, NULL, 0);
185 MODULE_PARM_DESC(baseaddr, "base address");
186 module_param_array(numports, uint, NULL, 0);
187 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
189 module_param(ttymajor, int, 0);
194 static int moxa_open(struct tty_struct *, struct file *);
195 static void moxa_close(struct tty_struct *, struct file *);
196 static int moxa_write(struct tty_struct *, const unsigned char *, int);
197 static int moxa_write_room(struct tty_struct *);
198 static void moxa_flush_buffer(struct tty_struct *);
199 static int moxa_chars_in_buffer(struct tty_struct *);
200 static void moxa_throttle(struct tty_struct *);
201 static void moxa_unthrottle(struct tty_struct *);
202 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
203 static void moxa_stop(struct tty_struct *);
204 static void moxa_start(struct tty_struct *);
205 static void moxa_hangup(struct tty_struct *);
206 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
207 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
208 unsigned int set, unsigned int clear);
209 static void moxa_poll(unsigned long);
210 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
211 static void moxa_setup_empty_event(struct tty_struct *);
212 static void moxa_shut_down(struct moxa_port *);
214 * moxa board interface functions:
216 static void MoxaPortEnable(struct moxa_port *);
217 static void MoxaPortDisable(struct moxa_port *);
218 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
219 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
220 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
221 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
222 static int MoxaPortLineStatus(struct moxa_port *);
223 static void MoxaPortFlushData(struct moxa_port *, int);
224 static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int);
225 static int MoxaPortReadData(struct moxa_port *);
226 static int MoxaPortTxQueue(struct moxa_port *);
227 static int MoxaPortRxQueue(struct moxa_port *);
228 static int MoxaPortTxFree(struct moxa_port *);
229 static void MoxaPortTxDisable(struct moxa_port *);
230 static void MoxaPortTxEnable(struct moxa_port *);
231 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
232 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
233 static void MoxaSetFifo(struct moxa_port *port, int enable);
239 static void moxa_wait_finish(void __iomem *ofsAddr)
241 unsigned long end = jiffies + moxaFuncTout;
243 while (readw(ofsAddr + FuncCode) != 0)
244 if (time_after(jiffies, end))
246 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
247 printk(KERN_WARNING "moxa function expired\n");
250 static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
252 writew(arg, ofsAddr + FuncArg);
253 writew(cmd, ofsAddr + FuncCode);
254 moxa_wait_finish(ofsAddr);
257 static void moxa_low_water_check(void __iomem *ofsAddr)
259 u16 rptr, wptr, mask, len;
261 if (readb(ofsAddr + FlagStat) & Xoff_state) {
262 rptr = readw(ofsAddr + RXrptr);
263 wptr = readw(ofsAddr + RXwptr);
264 mask = readw(ofsAddr + RX_mask);
265 len = (wptr - rptr) & mask;
266 if (len <= Low_water)
267 moxafunc(ofsAddr, FC_SendXon, 0);
275 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
276 unsigned int cmd, unsigned long arg)
278 struct moxa_port *ch = tty->driver_data;
279 void __user *argp = (void __user *)arg;
282 if (tty->index == MAX_PORTS) {
283 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
284 cmd != MOXA_GETMSTATUS)
290 case MOXA_GETDATACOUNT:
291 moxaLog.tick = jiffies;
292 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
295 case MOXA_FLUSH_QUEUE:
296 MoxaPortFlushData(ch, arg);
298 case MOXA_GET_IOQUEUE: {
299 struct moxaq_str __user *argm = argp;
300 struct moxaq_str tmp;
304 mutex_lock(&moxa_openlock);
305 for (i = 0; i < MAX_BOARDS; i++) {
306 p = moxa_boards[i].ports;
307 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
308 memset(&tmp, 0, sizeof(tmp));
309 if (moxa_boards[i].ready) {
310 tmp.inq = MoxaPortRxQueue(p);
311 tmp.outq = MoxaPortTxQueue(p);
313 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
314 mutex_unlock(&moxa_openlock);
319 mutex_unlock(&moxa_openlock);
321 } case MOXA_GET_OQUEUE:
322 status = MoxaPortTxQueue(ch);
323 ret = put_user(status, (unsigned long __user *)argp);
325 case MOXA_GET_IQUEUE:
326 status = MoxaPortRxQueue(ch);
327 ret = put_user(status, (unsigned long __user *)argp);
329 case MOXA_GETMSTATUS: {
330 struct mxser_mstatus __user *argm = argp;
331 struct mxser_mstatus tmp;
335 mutex_lock(&moxa_openlock);
336 for (i = 0; i < MAX_BOARDS; i++) {
337 p = moxa_boards[i].ports;
338 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
339 memset(&tmp, 0, sizeof(tmp));
340 if (!moxa_boards[i].ready)
343 status = MoxaPortLineStatus(p);
351 if (!p->tty || !p->tty->termios)
352 tmp.cflag = p->cflag;
354 tmp.cflag = p->tty->termios->c_cflag;
356 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
357 mutex_unlock(&moxa_openlock);
362 mutex_unlock(&moxa_openlock);
366 mutex_lock(&moxa_openlock);
367 ret = moxa_get_serial_info(ch, argp);
368 mutex_unlock(&moxa_openlock);
371 mutex_lock(&moxa_openlock);
372 ret = moxa_set_serial_info(ch, argp);
373 mutex_unlock(&moxa_openlock);
381 static void moxa_break_ctl(struct tty_struct *tty, int state)
383 struct moxa_port *port = tty->driver_data;
385 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
389 static const struct tty_operations moxa_ops = {
393 .write_room = moxa_write_room,
394 .flush_buffer = moxa_flush_buffer,
395 .chars_in_buffer = moxa_chars_in_buffer,
397 .throttle = moxa_throttle,
398 .unthrottle = moxa_unthrottle,
399 .set_termios = moxa_set_termios,
402 .hangup = moxa_hangup,
403 .break_ctl = moxa_break_ctl,
404 .tiocmget = moxa_tiocmget,
405 .tiocmset = moxa_tiocmset,
408 static struct tty_driver *moxaDriver;
409 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
410 static DEFINE_SPINLOCK(moxa_lock);
416 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
418 switch (brd->boardType) {
419 case MOXA_BOARD_C218_ISA:
420 case MOXA_BOARD_C218_PCI:
424 case MOXA_BOARD_CP204J:
438 static int moxa_check_fw(const void *ptr)
440 const __le16 *lptr = ptr;
442 if (*lptr != cpu_to_le16(0x7980))
448 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
451 void __iomem *baseAddr = brd->basemem;
454 writeb(HW_reset, baseAddr + Control_reg); /* reset */
456 memset_io(baseAddr, 0, 4096);
457 memcpy_toio(baseAddr, buf, len); /* download BIOS */
458 writeb(0, baseAddr + Control_reg); /* restart */
462 switch (brd->boardType) {
463 case MOXA_BOARD_C218_ISA:
464 case MOXA_BOARD_C218_PCI:
465 tmp = readw(baseAddr + C218_key);
466 if (tmp != C218_KeyCode)
469 case MOXA_BOARD_CP204J:
470 tmp = readw(baseAddr + C218_key);
471 if (tmp != CP204J_KeyCode)
475 tmp = readw(baseAddr + C320_key);
476 if (tmp != C320_KeyCode)
478 tmp = readw(baseAddr + C320_status);
479 if (tmp != STS_init) {
480 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
481 "module not found\n");
489 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
493 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
496 void __iomem *baseAddr = brd->basemem;
499 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
503 writew(len - 7168 - 2, baseAddr + C320bapi_len);
504 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
505 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
506 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
507 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
512 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
515 void __iomem *baseAddr = brd->basemem;
516 const u16 *uptr = ptr;
517 size_t wlen, len2, j;
518 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
519 unsigned int i, retry;
522 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
525 switch (brd->boardType) {
526 case MOXA_BOARD_CP204J:
527 case MOXA_BOARD_C218_ISA:
528 case MOXA_BOARD_C218_PCI:
530 loadbuf = C218_LoadBuf;
531 loadlen = C218DLoad_len;
532 checksum = C218check_sum;
533 checksum_ok = C218chksum_ok;
537 keycode = C320_KeyCode;
538 loadbuf = C320_LoadBuf;
539 loadlen = C320DLoad_len;
540 checksum = C320check_sum;
541 checksum_ok = C320chksum_ok;
547 for (i = 0; i < wlen; i++)
548 usum += le16_to_cpu(uptr[i]);
554 len2 = (wlen > 2048) ? 2048 : wlen;
556 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
559 writew(len2, baseAddr + loadlen);
560 writew(0, baseAddr + key);
561 for (i = 0; i < 100; i++) {
562 if (readw(baseAddr + key) == keycode)
566 if (readw(baseAddr + key) != keycode)
569 writew(0, baseAddr + loadlen);
570 writew(usum, baseAddr + checksum);
571 writew(0, baseAddr + key);
572 for (i = 0; i < 100; i++) {
573 if (readw(baseAddr + key) == keycode)
578 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
579 if (readb(baseAddr + checksum_ok) != 1)
582 writew(0, baseAddr + key);
583 for (i = 0; i < 600; i++) {
584 if (readw(baseAddr + Magic_no) == Magic_code)
588 if (readw(baseAddr + Magic_no) != Magic_code)
591 if (MOXA_IS_320(brd)) {
592 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
593 writew(0x3800, baseAddr + TMS320_PORT1);
594 writew(0x3900, baseAddr + TMS320_PORT2);
595 writew(28499, baseAddr + TMS320_CLOCK);
597 writew(0x3200, baseAddr + TMS320_PORT1);
598 writew(0x3400, baseAddr + TMS320_PORT2);
599 writew(19999, baseAddr + TMS320_CLOCK);
602 writew(1, baseAddr + Disable_IRQ);
603 writew(0, baseAddr + Magic_no);
604 for (i = 0; i < 500; i++) {
605 if (readw(baseAddr + Magic_no) == Magic_code)
609 if (readw(baseAddr + Magic_no) != Magic_code)
612 if (MOXA_IS_320(brd)) {
613 j = readw(baseAddr + Module_cnt);
616 brd->numPorts = j * 8;
617 writew(j, baseAddr + Module_no);
618 writew(0, baseAddr + Magic_no);
619 for (i = 0; i < 600; i++) {
620 if (readw(baseAddr + Magic_no) == Magic_code)
624 if (readw(baseAddr + Magic_no) != Magic_code)
627 brd->intNdx = baseAddr + IRQindex;
628 brd->intPend = baseAddr + IRQpending;
629 brd->intTable = baseAddr + IRQtable;
634 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
637 void __iomem *ofsAddr, *baseAddr = brd->basemem;
638 struct moxa_port *port;
642 printk(KERN_ERR "MOXA: bios length is not even\n");
646 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
650 switch (brd->boardType) {
651 case MOXA_BOARD_C218_ISA:
652 case MOXA_BOARD_C218_PCI:
653 case MOXA_BOARD_CP204J:
655 for (i = 0; i < brd->numPorts; i++, port++) {
658 port->tableAddr = baseAddr + Extern_table +
660 ofsAddr = port->tableAddr;
661 writew(C218rx_mask, ofsAddr + RX_mask);
662 writew(C218tx_mask, ofsAddr + TX_mask);
663 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
664 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
666 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
667 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
673 for (i = 0; i < brd->numPorts; i++, port++) {
676 port->tableAddr = baseAddr + Extern_table +
678 ofsAddr = port->tableAddr;
679 switch (brd->numPorts) {
681 writew(C320p8rx_mask, ofsAddr + RX_mask);
682 writew(C320p8tx_mask, ofsAddr + TX_mask);
683 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
684 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
685 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
686 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
690 writew(C320p16rx_mask, ofsAddr + RX_mask);
691 writew(C320p16tx_mask, ofsAddr + TX_mask);
692 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
693 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
694 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
695 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
699 writew(C320p24rx_mask, ofsAddr + RX_mask);
700 writew(C320p24tx_mask, ofsAddr + TX_mask);
701 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
702 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
703 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
704 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
707 writew(C320p32rx_mask, ofsAddr + RX_mask);
708 writew(C320p32tx_mask, ofsAddr + TX_mask);
709 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
710 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
711 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
712 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
713 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
722 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
724 void *ptr = fw->data;
728 unsigned int a, lenp, lencnt;
731 __le32 magic; /* 0x34303430 */
733 u8 type; /* UNIX = 3 */
734 u8 model; /* C218T=1, C320T=2, CP204=3 */
739 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
741 if (fw->size < MOXA_FW_HDRLEN) {
742 strcpy(rsn, "too short (even header won't fit)");
745 if (hdr->magic != cpu_to_le32(0x30343034)) {
746 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
749 if (hdr->type != 3) {
750 sprintf(rsn, "not for linux, type is %u", hdr->type);
753 if (moxa_check_fw_model(brd, hdr->model)) {
754 sprintf(rsn, "not for this card, model is %u", hdr->model);
758 len = MOXA_FW_HDRLEN;
759 lencnt = hdr->model == 2 ? 5 : 3;
760 for (a = 0; a < ARRAY_SIZE(lens); a++) {
761 lens[a] = le16_to_cpu(hdr->len[a]);
762 if (lens[a] && len + lens[a] <= fw->size &&
763 moxa_check_fw(&fw->data[len]))
764 printk(KERN_WARNING "MOXA firmware: unexpected input "
765 "at offset %u, but going on\n", (u32)len);
766 if (!lens[a] && a < lencnt) {
767 sprintf(rsn, "too few entries in fw file");
773 if (len != fw->size) {
774 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
779 ptr += MOXA_FW_HDRLEN;
782 strcpy(rsn, "read above");
784 ret = moxa_load_bios(brd, ptr, lens[lenp]);
788 /* we skip the tty section (lens[1]), since we don't need it */
789 ptr += lens[lenp] + lens[lenp + 1];
790 lenp += 2; /* comm */
792 if (hdr->model == 2) {
793 ret = moxa_load_320b(brd, ptr, lens[lenp]);
796 /* skip another tty */
797 ptr += lens[lenp] + lens[lenp + 1];
801 ret = moxa_load_code(brd, ptr, lens[lenp]);
807 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
811 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
813 const struct firmware *fw;
819 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
821 if (brd->ports == NULL) {
822 printk(KERN_ERR "cannot allocate memory for ports\n");
827 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
828 p->type = PORT_16550A;
829 p->close_delay = 5 * HZ / 10;
830 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
831 init_waitqueue_head(&p->open_wait);
834 switch (brd->boardType) {
835 case MOXA_BOARD_C218_ISA:
836 case MOXA_BOARD_C218_PCI:
837 file = "c218tunx.cod";
839 case MOXA_BOARD_CP204J:
840 file = "cp204unx.cod";
843 file = "c320tunx.cod";
847 ret = request_firmware(&fw, file, dev);
849 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
850 "you've placed '%s' file into your firmware "
851 "loader directory (e.g. /lib/firmware)\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);
968 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
969 moxa_brdname[board_type - 1], board->numPorts);
973 iounmap(board->basemem);
974 board->basemem = NULL;
976 pci_release_region(pdev, 2);
981 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
983 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
985 moxa_board_deinit(brd);
987 pci_release_region(pdev, 2);
990 static struct pci_driver moxa_pci_driver = {
992 .id_table = moxa_pcibrds,
993 .probe = moxa_pci_probe,
994 .remove = __devexit_p(moxa_pci_remove)
996 #endif /* CONFIG_PCI */
998 static int __init moxa_init(void)
1000 unsigned int isabrds = 0;
1003 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1005 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1009 moxaDriver->owner = THIS_MODULE;
1010 moxaDriver->name = "ttyMX";
1011 moxaDriver->major = ttymajor;
1012 moxaDriver->minor_start = 0;
1013 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1014 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1015 moxaDriver->init_termios = tty_std_termios;
1016 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1017 moxaDriver->init_termios.c_ispeed = 9600;
1018 moxaDriver->init_termios.c_ospeed = 9600;
1019 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1020 tty_set_operations(moxaDriver, &moxa_ops);
1022 if (tty_register_driver(moxaDriver)) {
1023 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1024 put_tty_driver(moxaDriver);
1028 /* Find the boards defined from module args. */
1031 struct moxa_board_conf *brd = moxa_boards;
1033 for (i = 0; i < MAX_BOARDS; i++) {
1036 if (type[i] == MOXA_BOARD_C218_ISA ||
1037 type[i] == MOXA_BOARD_C320_ISA) {
1038 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1039 isabrds + 1, moxa_brdname[type[i] - 1],
1041 brd->boardType = type[i];
1042 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1044 brd->busType = MOXA_BUS_TYPE_ISA;
1045 brd->basemem = ioremap(baseaddr[i], 0x4000);
1046 if (!brd->basemem) {
1047 printk(KERN_ERR "MOXA: can't remap %lx\n",
1051 if (moxa_init_board(brd, NULL)) {
1052 iounmap(brd->basemem);
1053 brd->basemem = NULL;
1057 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1058 "ready (%u ports, firmware loaded)\n",
1059 baseaddr[i], brd->numPorts);
1069 retval = pci_register_driver(&moxa_pci_driver);
1071 printk(KERN_ERR "Can't register MOXA pci driver!\n");
1080 static void __exit moxa_exit(void)
1085 pci_unregister_driver(&moxa_pci_driver);
1088 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1089 if (moxa_boards[i].ready)
1090 moxa_board_deinit(&moxa_boards[i]);
1092 del_timer_sync(&moxaTimer);
1094 if (tty_unregister_driver(moxaDriver))
1095 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1097 put_tty_driver(moxaDriver);
1100 module_init(moxa_init);
1101 module_exit(moxa_exit);
1103 static void moxa_close_port(struct moxa_port *ch)
1106 MoxaPortFlushData(ch, 2);
1107 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1108 ch->tty->driver_data = NULL;
1112 static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1113 struct moxa_port *ch)
1120 prepare_to_wait(&ch->open_wait, &wait, TASK_INTERRUPTIBLE);
1121 if (tty_hung_up_p(filp)) {
1122 #ifdef SERIAL_DO_RESTART
1123 retval = -ERESTARTSYS;
1129 spin_lock_bh(&moxa_lock);
1131 spin_unlock_bh(&moxa_lock);
1135 if (signal_pending(current)) {
1136 retval = -ERESTARTSYS;
1141 finish_wait(&ch->open_wait, &wait);
1146 static int moxa_open(struct tty_struct *tty, struct file *filp)
1148 struct moxa_board_conf *brd;
1149 struct moxa_port *ch;
1154 if (port == MAX_PORTS) {
1155 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1157 if (mutex_lock_interruptible(&moxa_openlock))
1158 return -ERESTARTSYS;
1159 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1161 mutex_unlock(&moxa_openlock);
1165 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1167 tty->driver_data = ch;
1169 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
1170 ch->statusflags = 0;
1171 moxa_set_tty_param(tty, tty->termios);
1172 MoxaPortLineCtrl(ch, 1, 1);
1174 MoxaSetFifo(ch, ch->type == PORT_16550A);
1175 ch->asyncflags |= ASYNC_INITIALIZED;
1177 mutex_unlock(&moxa_openlock);
1180 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1181 retval = moxa_block_till_ready(tty, filp, ch);
1182 mutex_lock(&moxa_openlock);
1184 if (ch->count) /* 0 means already hung up... */
1185 if (--ch->count == 0)
1186 moxa_close_port(ch);
1188 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1189 mutex_unlock(&moxa_openlock);
1194 static void moxa_close(struct tty_struct *tty, struct file *filp)
1196 struct moxa_port *ch;
1200 if (port == MAX_PORTS || tty_hung_up_p(filp))
1203 mutex_lock(&moxa_openlock);
1204 ch = tty->driver_data;
1207 if (tty->count == 1 && ch->count != 1) {
1208 printk(KERN_WARNING "moxa_close: bad serial port count; "
1209 "tty->count is 1, ch->count is %d\n", ch->count);
1212 if (--ch->count < 0) {
1213 printk(KERN_WARNING "moxa_close: bad serial port count, "
1214 "device=%s\n", tty->name);
1220 ch->cflag = tty->termios->c_cflag;
1221 if (ch->asyncflags & ASYNC_INITIALIZED) {
1222 moxa_setup_empty_event(tty);
1223 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
1226 moxa_close_port(ch);
1228 mutex_unlock(&moxa_openlock);
1231 static int moxa_write(struct tty_struct *tty,
1232 const unsigned char *buf, int count)
1234 struct moxa_port *ch = tty->driver_data;
1240 spin_lock_bh(&moxa_lock);
1241 len = MoxaPortWriteData(ch, buf, count);
1242 spin_unlock_bh(&moxa_lock);
1244 ch->statusflags |= LOWWAIT;
1248 static int moxa_write_room(struct tty_struct *tty)
1250 struct moxa_port *ch;
1254 ch = tty->driver_data;
1257 return MoxaPortTxFree(ch);
1260 static void moxa_flush_buffer(struct tty_struct *tty)
1262 struct moxa_port *ch = tty->driver_data;
1266 MoxaPortFlushData(ch, 1);
1270 static int moxa_chars_in_buffer(struct tty_struct *tty)
1272 struct moxa_port *ch = tty->driver_data;
1276 * Sigh...I have to check if driver_data is NULL here, because
1277 * if an open() fails, the TTY subsystem eventually calls
1278 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1279 * routine. And since the open() failed, we return 0 here. TDJ
1283 chars = MoxaPortTxQueue(ch);
1286 * Make it possible to wakeup anything waiting for output
1287 * in tty_ioctl.c, etc.
1289 if (!(ch->statusflags & EMPTYWAIT))
1290 moxa_setup_empty_event(tty);
1295 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1297 struct moxa_port *ch;
1298 int flag = 0, dtr, rts;
1300 mutex_lock(&moxa_openlock);
1301 ch = tty->driver_data;
1303 mutex_unlock(&moxa_openlock);
1307 MoxaPortGetLineOut(ch, &dtr, &rts);
1312 dtr = MoxaPortLineStatus(ch);
1319 mutex_unlock(&moxa_openlock);
1323 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1324 unsigned int set, unsigned int clear)
1326 struct moxa_port *ch;
1331 mutex_lock(&moxa_openlock);
1332 ch = tty->driver_data;
1334 mutex_unlock(&moxa_openlock);
1338 MoxaPortGetLineOut(ch, &dtr, &rts);
1339 if (set & TIOCM_RTS)
1341 if (set & TIOCM_DTR)
1343 if (clear & TIOCM_RTS)
1345 if (clear & TIOCM_DTR)
1347 MoxaPortLineCtrl(ch, dtr, rts);
1348 mutex_unlock(&moxa_openlock);
1352 static void moxa_throttle(struct tty_struct *tty)
1354 struct moxa_port *ch = tty->driver_data;
1356 ch->statusflags |= THROTTLE;
1359 static void moxa_unthrottle(struct tty_struct *tty)
1361 struct moxa_port *ch = tty->driver_data;
1363 ch->statusflags &= ~THROTTLE;
1366 static void moxa_set_termios(struct tty_struct *tty,
1367 struct ktermios *old_termios)
1369 struct moxa_port *ch = tty->driver_data;
1373 moxa_set_tty_param(tty, old_termios);
1374 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1375 wake_up_interruptible(&ch->open_wait);
1378 static void moxa_stop(struct tty_struct *tty)
1380 struct moxa_port *ch = tty->driver_data;
1384 MoxaPortTxDisable(ch);
1385 ch->statusflags |= TXSTOPPED;
1389 static void moxa_start(struct tty_struct *tty)
1391 struct moxa_port *ch = tty->driver_data;
1396 if (!(ch->statusflags & TXSTOPPED))
1399 MoxaPortTxEnable(ch);
1400 ch->statusflags &= ~TXSTOPPED;
1403 static void moxa_hangup(struct tty_struct *tty)
1405 struct moxa_port *ch;
1407 mutex_lock(&moxa_openlock);
1408 ch = tty->driver_data;
1410 mutex_unlock(&moxa_openlock);
1414 moxa_close_port(ch);
1415 mutex_unlock(&moxa_openlock);
1417 wake_up_interruptible(&ch->open_wait);
1420 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1424 if (dcd != p->DCDState && p->tty && C_CLOCAL(p->tty)) {
1431 static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1434 struct tty_struct *tty = p->tty;
1435 void __iomem *ofsAddr;
1436 unsigned int inited = p->asyncflags & ASYNC_INITIALIZED;
1440 if ((p->statusflags & EMPTYWAIT) &&
1441 MoxaPortTxQueue(p) == 0) {
1442 p->statusflags &= ~EMPTYWAIT;
1445 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1446 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1447 p->statusflags &= ~LOWWAIT;
1451 if (inited && !(p->statusflags & THROTTLE) &&
1452 MoxaPortRxQueue(p) > 0) { /* RX */
1453 MoxaPortReadData(p);
1454 tty_schedule_flip(tty);
1457 p->statusflags &= ~EMPTYWAIT;
1458 MoxaPortFlushData(p, 0); /* flush RX */
1461 if (!handle) /* nothing else to do */
1464 intr = readw(ip); /* port irq status */
1468 writew(0, ip); /* ACK port */
1469 ofsAddr = p->tableAddr;
1470 if (intr & IntrTx) /* disable tx intr */
1471 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1472 ofsAddr + HostStat);
1477 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1478 tty_insert_flip_char(tty, 0, TTY_BREAK);
1479 tty_schedule_flip(tty);
1482 if (intr & IntrLine)
1483 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1488 static void moxa_poll(unsigned long ignored)
1490 struct moxa_board_conf *brd;
1492 unsigned int card, port, served = 0;
1494 spin_lock(&moxa_lock);
1495 for (card = 0; card < MAX_BOARDS; card++) {
1496 brd = &moxa_boards[card];
1503 if (readb(brd->intPend) == 0xff)
1504 ip = brd->intTable + readb(brd->intNdx);
1506 for (port = 0; port < brd->numPorts; port++)
1507 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1510 writeb(0, brd->intPend); /* ACK */
1512 if (moxaLowWaterChk) {
1513 struct moxa_port *p = brd->ports;
1514 for (port = 0; port < brd->numPorts; port++, p++)
1515 if (p->lowChkFlag) {
1517 moxa_low_water_check(p->tableAddr);
1521 moxaLowWaterChk = 0;
1524 mod_timer(&moxaTimer, jiffies + HZ / 50);
1525 spin_unlock(&moxa_lock);
1528 /******************************************************************************/
1530 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1532 register struct ktermios *ts = tty->termios;
1533 struct moxa_port *ch = tty->driver_data;
1534 int rts, cts, txflow, rxflow, xany, baud;
1536 rts = cts = txflow = rxflow = xany = 0;
1537 if (ts->c_cflag & CRTSCTS)
1539 if (ts->c_iflag & IXON)
1541 if (ts->c_iflag & IXOFF)
1543 if (ts->c_iflag & IXANY)
1546 /* Clear the features we don't support */
1547 ts->c_cflag &= ~CMSPAR;
1548 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1549 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1551 baud = tty_termios_baud_rate(old_termios);
1552 /* Not put the baud rate into the termios data */
1553 tty_encode_baud_rate(tty, baud, baud);
1556 static void moxa_setup_empty_event(struct tty_struct *tty)
1558 struct moxa_port *ch = tty->driver_data;
1560 spin_lock_bh(&moxa_lock);
1561 ch->statusflags |= EMPTYWAIT;
1562 spin_unlock_bh(&moxa_lock);
1565 static void moxa_shut_down(struct moxa_port *ch)
1567 struct tty_struct *tp = ch->tty;
1569 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1572 MoxaPortDisable(ch);
1575 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1578 MoxaPortLineCtrl(ch, 0, 0);
1580 spin_lock_bh(&moxa_lock);
1581 ch->asyncflags &= ~ASYNC_INITIALIZED;
1582 spin_unlock_bh(&moxa_lock);
1585 /*****************************************************************************
1586 * Driver level functions: *
1587 *****************************************************************************/
1589 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1591 void __iomem *ofsAddr;
1592 if (mode < 0 || mode > 2)
1594 ofsAddr = port->tableAddr;
1595 moxafunc(ofsAddr, FC_FlushQueue, mode);
1597 port->lowChkFlag = 0;
1598 moxa_low_water_check(ofsAddr);
1603 * Moxa Port Number Description:
1605 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1606 * the port number using in MOXA driver functions will be 0 to 31 for
1607 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1608 * to 127 for fourth. For example, if you setup three MOXA boards,
1609 * first board is C218, second board is C320-16 and third board is
1610 * C320-32. The port number of first board (C218 - 8 ports) is from
1611 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1612 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1613 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1614 * 127 will be invalid.
1617 * Moxa Functions Description:
1619 * Function 1: Driver initialization routine, this routine must be
1620 * called when initialized driver.
1622 * void MoxaDriverInit();
1625 * Function 2: Moxa driver private IOCTL command processing.
1627 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1629 * unsigned int cmd : IOCTL command
1630 * unsigned long arg : IOCTL argument
1631 * int port : port number (0 - 127)
1638 * Function 6: Enable this port to start Tx/Rx data.
1640 * void MoxaPortEnable(int port);
1641 * int port : port number (0 - 127)
1644 * Function 7: Disable this port
1646 * void MoxaPortDisable(int port);
1647 * int port : port number (0 - 127)
1650 * Function 10: Setting baud rate of this port.
1652 * speed_t MoxaPortSetBaud(int port, speed_t baud);
1653 * int port : port number (0 - 127)
1654 * long baud : baud rate (50 - 115200)
1656 * return: 0 : this port is invalid or baud < 50
1657 * 50 - 115200 : the real baud rate set to the port, if
1658 * the argument baud is large than maximun
1659 * available baud rate, the real setting
1660 * baud rate will be the maximun baud rate.
1663 * Function 12: Configure the port.
1665 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1666 * int port : port number (0 - 127)
1667 * struct ktermios * termio : termio structure pointer
1668 * speed_t baud : baud rate
1670 * return: -1 : this port is invalid or termio == NULL
1674 * Function 13: Get the DTR/RTS state of this port.
1676 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1677 * int port : port number (0 - 127)
1678 * int * dtrState : pointer to INT to receive the current DTR
1679 * state. (if NULL, this function will not
1680 * write to this address)
1681 * int * rtsState : pointer to INT to receive the current RTS
1682 * state. (if NULL, this function will not
1683 * write to this address)
1685 * return: -1 : this port is invalid
1689 * Function 14: Setting the DTR/RTS output state of this port.
1691 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1692 * int port : port number (0 - 127)
1693 * int dtrState : DTR output state (0: off, 1: on)
1694 * int rtsState : RTS output state (0: off, 1: on)
1697 * Function 15: Setting the flow control of this port.
1699 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1700 * int txFlow,int xany);
1701 * int port : port number (0 - 127)
1702 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1703 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1704 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1705 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1706 * int xany : S/W XANY flow control (0: no, 1: yes)
1709 * Function 16: Get ths line status of this port
1711 * int MoxaPortLineStatus(int port);
1712 * int port : port number (0 - 127)
1714 * return: Bit 0 - CTS state (0: off, 1: on)
1715 * Bit 1 - DSR state (0: off, 1: on)
1716 * Bit 2 - DCD state (0: off, 1: on)
1719 * Function 19: Flush the Rx/Tx buffer data of this port.
1721 * void MoxaPortFlushData(int port, int mode);
1722 * int port : port number (0 - 127)
1724 * 0 : flush the Rx buffer
1725 * 1 : flush the Tx buffer
1726 * 2 : flush the Rx and Tx buffer
1729 * Function 20: Write data.
1731 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1732 * int port : port number (0 - 127)
1733 * unsigned char * buffer : pointer to write data buffer.
1734 * int length : write data length
1736 * return: 0 - length : real write data length
1739 * Function 21: Read data.
1741 * int MoxaPortReadData(int port, struct tty_struct *tty);
1742 * int port : port number (0 - 127)
1743 * struct tty_struct *tty : tty for data
1745 * return: 0 - length : real read data length
1748 * Function 24: Get the Tx buffer current queued data bytes
1750 * int MoxaPortTxQueue(int port);
1751 * int port : port number (0 - 127)
1753 * return: .. : Tx buffer current queued data bytes
1756 * Function 25: Get the Tx buffer current free space
1758 * int MoxaPortTxFree(int port);
1759 * int port : port number (0 - 127)
1761 * return: .. : Tx buffer current free space
1764 * Function 26: Get the Rx buffer current queued data bytes
1766 * int MoxaPortRxQueue(int port);
1767 * int port : port number (0 - 127)
1769 * return: .. : Rx buffer current queued data bytes
1772 * Function 28: Disable port data transmission.
1774 * void MoxaPortTxDisable(int port);
1775 * int port : port number (0 - 127)
1778 * Function 29: Enable port data transmission.
1780 * void MoxaPortTxEnable(int port);
1781 * int port : port number (0 - 127)
1784 * Function 31: Get the received BREAK signal count and reset it.
1786 * int MoxaPortResetBrkCnt(int port);
1787 * int port : port number (0 - 127)
1789 * return: 0 - .. : BREAK signal count
1794 static void MoxaPortEnable(struct moxa_port *port)
1796 void __iomem *ofsAddr;
1799 ofsAddr = port->tableAddr;
1800 writew(lowwater, ofsAddr + Low_water);
1801 if (MOXA_IS_320(port->board))
1802 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1804 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1805 ofsAddr + HostStat);
1807 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1808 moxafunc(ofsAddr, FC_FlushQueue, 2);
1810 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1811 MoxaPortLineStatus(port);
1814 static void MoxaPortDisable(struct moxa_port *port)
1816 void __iomem *ofsAddr = port->tableAddr;
1818 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1819 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1820 writew(0, ofsAddr + HostStat);
1821 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1824 static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
1826 void __iomem *ofsAddr = port->tableAddr;
1827 unsigned int clock, val;
1830 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1837 moxafunc(ofsAddr, FC_SetBaud, val);
1842 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1845 void __iomem *ofsAddr;
1849 ofsAddr = port->tableAddr;
1850 cflag = termio->c_cflag; /* termio->c_cflag */
1852 mode = termio->c_cflag & CSIZE;
1855 else if (mode == CS6)
1857 else if (mode == CS7)
1859 else if (mode == CS8)
1862 if (termio->c_cflag & CSTOPB) {
1870 if (termio->c_cflag & PARENB) {
1871 if (termio->c_cflag & PARODD)
1878 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
1880 if (MOXA_IS_320(port->board) && baud >= 921600)
1883 baud = MoxaPortSetBaud(port, baud);
1885 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1886 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1887 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1888 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1889 moxa_wait_finish(ofsAddr);
1895 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1899 *dtrState = !!(port->lineCtrl & DTR_ON);
1901 *rtsState = !!(port->lineCtrl & RTS_ON);
1906 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1914 port->lineCtrl = mode;
1915 moxafunc(port->tableAddr, FC_LineControl, mode);
1918 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1919 int txflow, int rxflow, int txany)
1924 mode |= RTS_FlowCtl;
1926 mode |= CTS_FlowCtl;
1933 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
1936 static int MoxaPortLineStatus(struct moxa_port *port)
1938 void __iomem *ofsAddr;
1941 ofsAddr = port->tableAddr;
1942 if (MOXA_IS_320(port->board)) {
1943 moxafunc(ofsAddr, FC_LineStatus, 0);
1944 val = readw(ofsAddr + FuncArg);
1946 val = readw(ofsAddr + FlagStat) >> 4;
1951 spin_lock_bh(&moxa_lock);
1952 moxa_new_dcdstate(port, val & 8);
1953 spin_unlock_bh(&moxa_lock);
1958 static int MoxaPortWriteData(struct moxa_port *port,
1959 const unsigned char *buffer, int len)
1961 void __iomem *baseAddr, *ofsAddr, *ofs;
1962 unsigned int c, total;
1963 u16 head, tail, tx_mask, spage, epage;
1964 u16 pageno, pageofs, bufhead;
1966 ofsAddr = port->tableAddr;
1967 baseAddr = port->board->basemem;
1968 tx_mask = readw(ofsAddr + TX_mask);
1969 spage = readw(ofsAddr + Page_txb);
1970 epage = readw(ofsAddr + EndPage_txb);
1971 tail = readw(ofsAddr + TXwptr);
1972 head = readw(ofsAddr + TXrptr);
1973 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
1976 moxaLog.txcnt[port->tty->index] += c;
1978 if (spage == epage) {
1979 bufhead = readw(ofsAddr + Ofs_txb);
1980 writew(spage, baseAddr + Control_reg);
1983 len = head - tail - 1;
1985 len = tx_mask + 1 - tail;
1986 len = (c > len) ? len : c;
1987 ofs = baseAddr + DynPage_addr + bufhead + tail;
1988 memcpy_toio(ofs, buffer, len);
1990 tail = (tail + len) & tx_mask;
1994 pageno = spage + (tail >> 13);
1995 pageofs = tail & Page_mask;
1997 len = Page_size - pageofs;
2000 writeb(pageno, baseAddr + Control_reg);
2001 ofs = baseAddr + DynPage_addr + pageofs;
2002 memcpy_toio(ofs, buffer, len);
2004 if (++pageno == epage)
2009 tail = (tail + total) & tx_mask;
2011 writew(tail, ofsAddr + TXwptr);
2012 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2016 static int MoxaPortReadData(struct moxa_port *port)
2018 struct tty_struct *tty = port->tty;
2020 void __iomem *baseAddr, *ofsAddr, *ofs;
2021 unsigned int count, len, total;
2022 u16 tail, rx_mask, spage, epage;
2023 u16 pageno, pageofs, bufhead, head;
2025 ofsAddr = port->tableAddr;
2026 baseAddr = port->board->basemem;
2027 head = readw(ofsAddr + RXrptr);
2028 tail = readw(ofsAddr + RXwptr);
2029 rx_mask = readw(ofsAddr + RX_mask);
2030 spage = readw(ofsAddr + Page_rxb);
2031 epage = readw(ofsAddr + EndPage_rxb);
2032 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
2037 moxaLog.rxcnt[tty->index] += total;
2038 if (spage == epage) {
2039 bufhead = readw(ofsAddr + Ofs_rxb);
2040 writew(spage, baseAddr + Control_reg);
2042 ofs = baseAddr + DynPage_addr + bufhead + head;
2043 len = (tail >= head) ? (tail - head) :
2044 (rx_mask + 1 - head);
2045 len = tty_prepare_flip_string(tty, &dst,
2047 memcpy_fromio(dst, ofs, len);
2048 head = (head + len) & rx_mask;
2052 pageno = spage + (head >> 13);
2053 pageofs = head & Page_mask;
2055 writew(pageno, baseAddr + Control_reg);
2056 ofs = baseAddr + DynPage_addr + pageofs;
2057 len = tty_prepare_flip_string(tty, &dst,
2058 min(Page_size - pageofs, count));
2059 memcpy_fromio(dst, ofs, len);
2062 pageofs = (pageofs + len) & Page_mask;
2063 if (pageofs == 0 && ++pageno == epage)
2066 head = (head + total) & rx_mask;
2068 writew(head, ofsAddr + RXrptr);
2069 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2070 moxaLowWaterChk = 1;
2071 port->lowChkFlag = 1;
2077 static int MoxaPortTxQueue(struct moxa_port *port)
2079 void __iomem *ofsAddr = port->tableAddr;
2080 u16 rptr, wptr, mask;
2082 rptr = readw(ofsAddr + TXrptr);
2083 wptr = readw(ofsAddr + TXwptr);
2084 mask = readw(ofsAddr + TX_mask);
2085 return (wptr - rptr) & mask;
2088 static int MoxaPortTxFree(struct moxa_port *port)
2090 void __iomem *ofsAddr = port->tableAddr;
2091 u16 rptr, wptr, mask;
2093 rptr = readw(ofsAddr + TXrptr);
2094 wptr = readw(ofsAddr + TXwptr);
2095 mask = readw(ofsAddr + TX_mask);
2096 return mask - ((wptr - rptr) & mask);
2099 static int MoxaPortRxQueue(struct moxa_port *port)
2101 void __iomem *ofsAddr = port->tableAddr;
2102 u16 rptr, wptr, mask;
2104 rptr = readw(ofsAddr + RXrptr);
2105 wptr = readw(ofsAddr + RXwptr);
2106 mask = readw(ofsAddr + RX_mask);
2107 return (wptr - rptr) & mask;
2110 static void MoxaPortTxDisable(struct moxa_port *port)
2112 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2115 static void MoxaPortTxEnable(struct moxa_port *port)
2117 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2120 static int moxa_get_serial_info(struct moxa_port *info,
2121 struct serial_struct __user *retinfo)
2123 struct serial_struct tmp = {
2125 .line = info->tty->index,
2126 .flags = info->asyncflags,
2127 .baud_base = 921600,
2128 .close_delay = info->close_delay
2130 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2134 static int moxa_set_serial_info(struct moxa_port *info,
2135 struct serial_struct __user *new_info)
2137 struct serial_struct new_serial;
2139 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2142 if (new_serial.irq != 0 || new_serial.port != 0 ||
2143 new_serial.custom_divisor != 0 ||
2144 new_serial.baud_base != 921600)
2147 if (!capable(CAP_SYS_ADMIN)) {
2148 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2149 (info->asyncflags & ~ASYNC_USR_MASK)))
2152 info->close_delay = new_serial.close_delay * HZ / 100;
2154 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2155 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2157 MoxaSetFifo(info, new_serial.type == PORT_16550A);
2159 info->type = new_serial.type;
2165 /*****************************************************************************
2166 * Static local functions: *
2167 *****************************************************************************/
2169 static void MoxaSetFifo(struct moxa_port *port, int enable)
2171 void __iomem *ofsAddr = port->tableAddr;
2174 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2175 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2177 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2178 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);